Taras Romaniv
Taras Romaniv

Reputation: 436

Setting locale manually in r.js build of require.js based app is not working

I'm developing an application using i18n plugin. And I'm setting locale manually in main.js file according to user preferences. Like this:

requirejs.config({
    config: {
        i18n: { locale: 'some_locale' }
    }
});

And it works pretty well. But I can't set locale manually for r.js build.

According to documentation I'm setting locale in r.js config file and r.js should inline following i18n resources into the build:

{
    ...
    optimize: 'uglify2',
    config: {
        i18n: { locale: 'some_locale' }
    }
}

And it works, i18n resources are actually inlined into the build file. But i18n plugin still detects locale automatically. So nothing works if detected locale and bundled resources don't matched. It happens because i18n plugin is trying to load not existing i18n resources for automatically detected locale.

Upvotes: 1

Views: 335

Answers (1)

SubtlyDisruptive
SubtlyDisruptive

Reputation: 21

Would be nice to know how you are loading the resource, and how your resources are structured, and how you specified the root resource file, but the following is my suggestion:

First of all, by specifying the loader config:

requirejs.config({
    config: {
        i18n: { locale: 'some_locale' }
    }
});

enforces the i18n plug-in to load all resources in 'some_locale'

define(['app/nls/myResource'], function (myResource) {
    // myResource: myapp/nls/some_locale/myResource is loaded,
    // even if the actual locale is something else
    // If some_locale does not exist,
    // the root resource (myapp/nls/myResource) will be loaded
});

In order to specify a locale resource to be inlined in the build, you can specify the 'locale' option in your build configuration (https://github.com/jrburke/r.js/blob/master/build/example.build.js#L92):

locale: 'ko'

Then the built module will include the specified resource:

app/main.js
----------------
bower_components/requirejs-i18n/i18n.js
app/nls/common.js
app/nls/ko/common.js
i18n!app/nls/common
app/main.js

Also, make sure your root resource file specifies which locale resources are available:

define({
    'root': {
        'appName': 'My Application'
    },
    'ko': true
});

With this, I am able to enforce loaded resource locale to Korean ('ko') and grabbing the resource module from the built bundle instead of making an additional HTTP request for the Korean resource module.

Upvotes: 1

Related Questions