Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt for symfony 4.1 i18n routes #334

Merged
merged 1 commit into from Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 25 additions & 8 deletions Resources/js/router.js
Expand Up @@ -19,7 +19,7 @@ class Router {
* @param {Object.<string, Router.Route>=} routes
*/
constructor(context, routes) {
this.context_ = context || {base_url: '', prefix: '', host: '', port: '', scheme: ''};
this.context_ = context || {base_url: '', prefix: '', host: '', port: '', scheme: '', locale: ''};
this.setRoutes(routes || {});
}

Expand Down Expand Up @@ -55,6 +55,9 @@ class Router {
if ('port' in data) {
this.setPort(data['port']);
}
if ('locale' in data) {
this.setLocale(data['locale']);
}

this.setHost(data['host']);
this.setScheme(data['scheme']);
Expand Down Expand Up @@ -137,6 +140,20 @@ class Router {
return this.context_.port;
};

/**
* @param {string} locale
*/
setLocale(locale) {
this.context_.locale = locale;
}

/**
* @return {string}
*/
getLocale() {
return this.context_.locale;
};

/**
* Builds query string params added to a URL.
* Port of jQuery's $.param() function, so credit is due there.
Expand Down Expand Up @@ -174,17 +191,17 @@ class Router {
*/
getRoute(name) {
let prefixedName = this.context_.prefix + name;
let sf41i18nName = name + '.' + this.context_.locale;
let prefixedSf41i18nName = this.context_.prefix + name + '.' + this.context_.locale;
let variants = [prefixedName, sf41i18nName, prefixedSf41i18nName, name];

if (!(prefixedName in this.routes_)) {
// Check first for default route before failing
if (!(name in this.routes_)) {
throw new Error('The route "' + name + '" does not exist.');
for (let i in variants) {
if (variants[i] in this.routes_) {
return this.routes_[variants[i]];
}
} else {
name = prefixedName;
}

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

/**
Expand Down
17 changes: 16 additions & 1 deletion Resources/js/router.test.js
Expand Up @@ -327,7 +327,7 @@ function testGetBaseUrl() {
}

function testGeti18n() {
var router = new fos.Router({base_url: '/foo', prefix: 'en__RG__'}, {
var router = new fos.Router({base_url: '/foo', prefix: 'en__RG__', locale: 'en'}, {
en__RG__homepage: {
tokens: [['text', '/bar']],
defaults: {},
Expand All @@ -345,14 +345,29 @@ function testGeti18n() {
defaults: {},
requirements: {},
hosttokens: []
},
"login.en": {
tokens: [['text', '/en/login']],
defaults: {},
requirements: {},
hosttokens: []
},
"login.es": {
tokens: [['text', '/es/login']],
defaults: {},
requirements: {},
hosttokens: []
}
});

assertEquals('/foo/bar', router.generate('homepage'));
assertEquals('/foo/admin', router.generate('_admin'));
assertEquals('/foo/en/login', router.generate('login'));

router.setPrefix('es__RG__');
router.setLocale('es');
assertEquals('/foo/es/bar', router.generate('homepage'));
assertEquals('/foo/es/login', router.generate('login'));
}

function testGetRoute() {
Expand Down
39 changes: 31 additions & 8 deletions Resources/public/js/router.js
Expand Up @@ -48,7 +48,7 @@ var Router = function () {
function Router(context, routes) {
_classCallCheck(this, Router);

this.context_ = context || { base_url: '', prefix: '', host: '', port: '', scheme: '' };
this.context_ = context || { base_url: '', prefix: '', host: '', port: '', scheme: '', locale: '' };
this.setRoutes(routes || {});
}

Expand Down Expand Up @@ -76,6 +76,9 @@ var Router = function () {
if ('port' in data) {
this.setPort(data['port']);
}
if ('locale' in data) {
this.setLocale(data['locale']);
}

this.setHost(data['host']);
this.setScheme(data['scheme']);
Expand Down Expand Up @@ -190,6 +193,26 @@ var Router = function () {
value: function getPort() {
return this.context_.port;
}
}, {
key: 'setLocale',


/**
* @param {string} locale
*/
value: function setLocale(locale) {
this.context_.locale = locale;
}

/**
* @return {string}
*/

}, {
key: 'getLocale',
value: function getLocale() {
return this.context_.locale;
}
}, {
key: 'buildQueryParams',

Expand Down Expand Up @@ -236,17 +259,17 @@ var Router = function () {
key: 'getRoute',
value: function getRoute(name) {
var prefixedName = this.context_.prefix + name;
var sf41i18nName = name + '.' + this.context_.locale;
var prefixedSf41i18nName = this.context_.prefix + name + '.' + this.context_.locale;
var variants = [prefixedName, sf41i18nName, prefixedSf41i18nName, name];

if (!(prefixedName in this.routes_)) {
// Check first for default route before failing
if (!(name in this.routes_)) {
throw new Error('The route "' + name + '" does not exist.');
for (var i in variants) {
if (variants[i] in this.routes_) {
return this.routes_[variants[i]];
}
} else {
name = prefixedName;
}

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

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/js/router.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Response/RoutesResponse.php
Expand Up @@ -105,4 +105,9 @@ public function getScheme()
{
return $this->scheme;
}

public function getLocale()
{
return $this->locale;
}
}
1 change: 1 addition & 0 deletions Serializer/Normalizer/RoutesResponseNormalizer.php
Expand Up @@ -31,6 +31,7 @@ public function normalize($data, $format = null, array $context = array())
'host' => $data->getHost(),
'port' => $data->getPort(),
'scheme' => $data->getScheme(),
'locale' => $data->getLocale(),
);
}

Expand Down