Skip to content

Instantly share code, notes, and snippets.

@GendelfLugansk
Last active July 5, 2016 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GendelfLugansk/49be3c0c8ee91c2fb0b0 to your computer and use it in GitHub Desktop.
Save GendelfLugansk/49be3c0c8ee91c2fb0b0 to your computer and use it in GitHub Desktop.
Ember.js i18n with locale in URLs
//../config/environment.js
/* jshint node: true */
module.exports = function (environment)
{
var ENV = {
modulePrefix: 'realtor',
environment: environment,
baseURL: null,
locationType: 'hash',
EmberENV: {
FEATURES: {
// Here you can enable experimental features on an ember canary build
// e.g. 'with-controller': true
}
},
APP: {
// Here you can pass flags/options to your application instance
// when it is created
},
i18n: {
defaultLocale: 'ru',
allowedLocales: ['ru', 'uk']
}
};
if (environment === 'development') {
// ENV.APP.LOG_RESOLVER = true;
// ENV.APP.LOG_ACTIVE_GENERATION = true;
// ENV.APP.LOG_TRANSITIONS = true;
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
// ENV.APP.LOG_VIEW_LOOKUPS = true;
}
if (environment === 'test') {
// Testem prefers this...
ENV.baseURL = '/';
ENV.locationType = 'none';
// keep test console output quieter
ENV.APP.LOG_ACTIVE_GENERATION = false;
ENV.APP.LOG_VIEW_LOOKUPS = false;
ENV.APP.rootElement = '#ember-testing';
}
if (environment === 'production') {
}
return ENV;
};
//../app/initializers/i18n.js
export default {
name: 'i18n',
after: 'ember-i18n',
initialize: function (_, app)
{
app.inject('route', 'i18n', 'service:i18n');
app.inject('controller', 'i18n', 'service:i18n');
}
};
//../app/routes/index.js
import config from '../config/environment';
export default Ember.Route.extend(
{
beforeModel: function ()
{
var allowedLanguages = config.i18n.allowedLocales;
var language = config.i18n.defaultLocale;
if (navigator.languages) {
for (let lang of navigator.languages) {
if (allowedLanguages.indexOf(lang) > -1) {
language = lang;
break;
}
}
} else {
if (navigator.language) {
language = navigator.language;
} else {
if (navigator.userLanguage) {
language = navigator.userLanguage;
}
}
}
this.transitionTo('lang.index', { lang: language });
}
}
);
//../app/routes/lang.js
import config from '../config/environment';
export default Ember.Route.extend(
{
afterModel: function (params)
{
var allowedLocales = config.i18n.allowedLocales;
var defaultLocale = config.i18n.defaultLocale;
this.set(
'i18n.locale',
params && params.lang && allowedLocales.indexOf(params.lang) > -1 ? params.lang : defaultLocale
);
}
}
);
//../app/router.js
import Ember from 'ember';
import config from './config/environment';
var Router = Ember.Router.extend(
{
location: config.locationType
}
);
Router.map(
function ()
{
this.route(
"lang", { path: '/:lang' }, function ()
{
this.route("index", { path: '/' });
this.route('404', { path: '/*wildcard' });
}
);
}
);
export default Router;
@GendelfLugansk
Copy link
Author

@rafatrace
Copy link

rafatrace commented Jul 5, 2016

Does the templates need to be store in template/lang/ folder?
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment