Skip to content

Commit cf2247f

Browse files
committed
adapt for symfony 4.1 i18n routes
1 parent 552c00c commit cf2247f

File tree

8 files changed

+95
-29
lines changed

8 files changed

+95
-29
lines changed

Resources/js/router.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Router {
1919
* @param {Object.<string, Router.Route>=} routes
2020
*/
2121
constructor(context, routes) {
22-
this.context_ = context || {base_url: '', prefix: '', host: '', port: '', scheme: ''};
22+
this.context_ = context || {base_url: '', prefix: '', host: '', port: '', scheme: '', locale: ''};
2323
this.setRoutes(routes || {});
2424
}
2525

@@ -55,6 +55,9 @@ class Router {
5555
if ('port' in data) {
5656
this.setPort(data['port']);
5757
}
58+
if ('locale' in data) {
59+
this.setLocale(data['locale']);
60+
}
5861

5962
this.setHost(data['host']);
6063
this.setScheme(data['scheme']);
@@ -137,6 +140,20 @@ class Router {
137140
return this.context_.port;
138141
};
139142

143+
/**
144+
* @param {string} locale
145+
*/
146+
setLocale(locale) {
147+
this.context_.locale = locale;
148+
}
149+
150+
/**
151+
* @return {string}
152+
*/
153+
getLocale() {
154+
return this.context_.locale;
155+
};
156+
140157
/**
141158
* Builds query string params added to a URL.
142159
* Port of jQuery's $.param() function, so credit is due there.
@@ -174,17 +191,17 @@ class Router {
174191
*/
175192
getRoute(name) {
176193
let prefixedName = this.context_.prefix + name;
194+
let sf41i18nName = name + '.' + this.context_.locale;
195+
let prefixedSf41i18nName = this.context_.prefix + name + '.' + this.context_.locale;
196+
let variants = [prefixedName, sf41i18nName, prefixedSf41i18nName, name];
177197

178-
if (!(prefixedName in this.routes_)) {
179-
// Check first for default route before failing
180-
if (!(name in this.routes_)) {
181-
throw new Error('The route "' + name + '" does not exist.');
198+
for (let i in variants) {
199+
if (variants[i] in this.routes_) {
200+
return this.routes_[variants[i]];
182201
}
183-
} else {
184-
name = prefixedName;
185202
}
186203

187-
return this.routes_[name];
204+
throw new Error('The route "' + name + '" does not exist.');
188205
}
189206

190207
/**

Resources/js/router.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ function testGetBaseUrl() {
327327
}
328328

329329
function testGeti18n() {
330-
var router = new fos.Router({base_url: '/foo', prefix: 'en__RG__'}, {
330+
var router = new fos.Router({base_url: '/foo', prefix: 'en__RG__', locale: 'en'}, {
331331
en__RG__homepage: {
332332
tokens: [['text', '/bar']],
333333
defaults: {},
@@ -345,14 +345,29 @@ function testGeti18n() {
345345
defaults: {},
346346
requirements: {},
347347
hosttokens: []
348+
},
349+
"login.en": {
350+
tokens: [['text', '/en/login']],
351+
defaults: {},
352+
requirements: {},
353+
hosttokens: []
354+
},
355+
"login.es": {
356+
tokens: [['text', '/es/login']],
357+
defaults: {},
358+
requirements: {},
359+
hosttokens: []
348360
}
349361
});
350362

351363
assertEquals('/foo/bar', router.generate('homepage'));
352364
assertEquals('/foo/admin', router.generate('_admin'));
365+
assertEquals('/foo/en/login', router.generate('login'));
353366

354367
router.setPrefix('es__RG__');
368+
router.setLocale('es');
355369
assertEquals('/foo/es/bar', router.generate('homepage'));
370+
assertEquals('/foo/es/login', router.generate('login'));
356371
}
357372

358373
function testGetRoute() {

Resources/public/js/router.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var Router = function () {
4848
function Router(context, routes) {
4949
_classCallCheck(this, Router);
5050

51-
this.context_ = context || { base_url: '', prefix: '', host: '', port: '', scheme: '' };
51+
this.context_ = context || { base_url: '', prefix: '', host: '', port: '', scheme: '', locale: '' };
5252
this.setRoutes(routes || {});
5353
}
5454

@@ -76,6 +76,9 @@ var Router = function () {
7676
if ('port' in data) {
7777
this.setPort(data['port']);
7878
}
79+
if ('locale' in data) {
80+
this.setLocale(data['locale']);
81+
}
7982

8083
this.setHost(data['host']);
8184
this.setScheme(data['scheme']);
@@ -190,6 +193,26 @@ var Router = function () {
190193
value: function getPort() {
191194
return this.context_.port;
192195
}
196+
}, {
197+
key: 'setLocale',
198+
199+
200+
/**
201+
* @param {string} locale
202+
*/
203+
value: function setLocale(locale) {
204+
this.context_.locale = locale;
205+
}
206+
207+
/**
208+
* @return {string}
209+
*/
210+
211+
}, {
212+
key: 'getLocale',
213+
value: function getLocale() {
214+
return this.context_.locale;
215+
}
193216
}, {
194217
key: 'buildQueryParams',
195218

@@ -236,17 +259,17 @@ var Router = function () {
236259
key: 'getRoute',
237260
value: function getRoute(name) {
238261
var prefixedName = this.context_.prefix + name;
262+
var sf41i18nName = name + '.' + this.context_.locale;
263+
var prefixedSf41i18nName = this.context_.prefix + name + '.' + this.context_.locale;
264+
var variants = [prefixedName, sf41i18nName, prefixedSf41i18nName, name];
239265

240-
if (!(prefixedName in this.routes_)) {
241-
// Check first for default route before failing
242-
if (!(name in this.routes_)) {
243-
throw new Error('The route "' + name + '" does not exist.');
266+
for (var i in variants) {
267+
if (variants[i] in this.routes_) {
268+
return this.routes_[variants[i]];
244269
}
245-
} else {
246-
name = prefixedName;
247270
}
248271

249-
return this.routes_[name];
272+
throw new Error('The route "' + name + '" does not exist.');
250273
}
251274

252275
/**

Resources/public/js/router.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Response/RoutesResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,9 @@ public function getScheme()
105105
{
106106
return $this->scheme;
107107
}
108+
109+
public function getLocale()
110+
{
111+
return $this->locale;
112+
}
108113
}

Serializer/Normalizer/RoutesResponseNormalizer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function normalize($data, $format = null, array $context = array())
3131
'host' => $data->getHost(),
3232
'port' => $data->getPort(),
3333
'scheme' => $data->getScheme(),
34+
'locale' => $data->getLocale(),
3435
);
3536
}
3637

0 commit comments

Comments
 (0)