Skip to content

Commit

Permalink
adapt for symfony 4.1 i18n routes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanza committed Oct 10, 2018
1 parent fce90c6 commit 92dd1dd
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 24 deletions.
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 @@ -85,4 +85,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
12 changes: 6 additions & 6 deletions Tests/Controller/ControllerTest.php
Expand Up @@ -49,7 +49,7 @@ public function testIndexAction()

$response = $controller->indexAction($this->getRequest('/'), 'json');

$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":""}', $response->getContent());
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]},"blog":{"tokens":[["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":[],"requirements":[],"hosttokens":[["text","localhost"]],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());
}

public function testIndexActionWithLocalizedRoutes()
Expand All @@ -65,7 +65,7 @@ public function testIndexActionWithLocalizedRoutes()

$response = $controller->indexAction($this->getRequest('/'), 'json');

$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]},"blog":{"tokens":[["variable","\/","[^\/]++","_locale"],["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":{"_locale":"en"},"requirements":[],"hosttokens":[["text","localhost"]],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":""}', $response->getContent());
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]},"blog":{"tokens":[["variable","\/","[^\/]++","_locale"],["variable","\/","[^\/]++","slug"],["text","\/blog-post"]],"defaults":{"_locale":"en"},"requirements":[],"hosttokens":[["text","localhost"]],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());
}

public function testConfigCache()
Expand All @@ -79,11 +79,11 @@ public function testConfigCache()
);

$response = $controller->indexAction($this->getRequest('/'), 'json');
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":""}', $response->getContent());
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());

// second call should serve the cached content
$response = $controller->indexAction($this->getRequest('/'), 'json');
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":""}', $response->getContent());
$this->assertEquals('{"base_url":"","routes":{"literal":{"tokens":[["text","\/homepage"]],"defaults":[],"requirements":[],"hosttokens":[],"methods":[],"schemes":[]}},"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());
}

/**
Expand All @@ -95,7 +95,7 @@ public function testGenerateWithCallback($callback)
$response = $controller->indexAction($this->getRequest('/', 'GET', array('callback' => $callback)), 'json');

$this->assertEquals(
sprintf('/**/%s({"base_url":"","routes":[],"prefix":"","host":"","port":null,"scheme":""});', $callback),
sprintf('/**/%s({"base_url":"","routes":[],"prefix":"","host":"","port":null,"scheme":"","locale":"en"});', $callback),
$response->getContent()
);
}
Expand All @@ -122,7 +122,7 @@ public function testIndexActionWithoutRoutes()
$controller = new Controller($this->getSerializer(), $this->getExtractor(), array(), sys_get_temp_dir());
$response = $controller->indexAction($this->getRequest('/'), 'json');

$this->assertEquals('{"base_url":"","routes":[],"prefix":"","host":"","port":null,"scheme":""}', $response->getContent());
$this->assertEquals('{"base_url":"","routes":[],"prefix":"","host":"","port":null,"scheme":"","locale":"en"}', $response->getContent());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('application/json', $response->headers->get('Content-Type'));

Expand Down

0 comments on commit 92dd1dd

Please sign in to comment.