Chapter 10
Considering this strategy a user might target our sample application with following
types of URLs:
- http://host.com/fr-ca/admin/users/list: Here a user is specifying a
desired locale (fr-ca). We should cross check the requested locale with a list
of supported ones and redirect a user to a different URL, if we don't happen
to support the wished one. For example, we could redirect users to a URL
with a default locale (say en-us), if the fr-ca is not supported: http://host.
com/en-us/admin/users/list - http://host.com/admin/users/list: Here a desired locale was not
specified. We can try to guess a locale for a user based on the Accept-
Language HTTP request header, and redirect a user to a URL with an
identified locale. Obviously, we should cross check a locale extracted from
request headers with a set of supported locales.
In practice you are likely to have a more sophisticated algorithm
combining settings from different sources with a robust fallback strategy
and defaults. The exact strategy to determine user's locale based on his
request will largely depend on your application requirements.
As soon a locale for a given user is identified we can send to a browser a page that
bootstraps the whole application. The initial page (index.html or similar) needs to
be generated dynamically on the server side. In the sample SCRUM application the
index.html file is a template shown as follows:
<head>
<meta charset="utf-8">
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-locale_<%= locale %>.js"></script>
</head>
Here the target locale is identified according to the described algorithm and a
template is processed on the server-side before being served to a browser.
Consequences of including locales as part of URLs
As soon as we add a new path element to the application's URLs we need to take
care of two new issues: reconfiguring routes and downloading partials.
Normally a new path element should be handled as a part of routes definition. Of
course we could go back to our application and re-define all the routes, for example:
/admin/users/list would become /:locale/admin/users/list.