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

Setting locale manually in r.js build is not working #18

Open
trsrm opened this issue Apr 25, 2014 · 7 comments
Open

Setting locale manually in r.js build is not working #18

trsrm opened this issue Apr 25, 2014 · 7 comments

Comments

@trsrm
Copy link

trsrm commented Apr 25, 2014

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.

@trsrm trsrm changed the title Seting locale manually in r.js build is not working Setting locale manually in r.js build is not working Apr 25, 2014
@jrburke
Copy link
Member

jrburke commented May 1, 2014

Do you have an example project I can try, to see the problem? That will also help me to construct a test case to make sure it stays fixed in the future.

@crebuh
Copy link

crebuh commented Oct 5, 2015

I've got a similar problem. When I'm in development mode i set the locale in the config.js file like this

    locale: localStorage.getItem('locale') || 'en_US',

very interesting is that the following piece of code wont work:

    config: {
        i18n: {
            locale: localStorage.getItem('locale') || 'en_US'
        },
    },

my root labels files looks like this:

 define({
  'root': false,
  'de_DE': true,
  'en_US': true
});

Everything is fine, but when I try to build a optimized version with the r.js optimizer then i got the following message:

  { [Error: Error: The config in mainConfigFile /home/user/sample-app/app/scripts/general/config/config.js cannot be used because it cannot be evaluated correctly while running in the optimizer. Try only using a config that is also valid JSON, or do not use mainConfigFile and instead copy the config values needed into a build file or command line arguments given to the optimizer.
Source error from parsing: /home/user/sample-app/app/scripts/general/config/config.js: ReferenceError: localStorage is not defined
    at /home/user/sample-app/node_modules/grunt-contrib-requirejs/node_modules/requirejs/bin/r.js:29446:27
]
  originalError: 
   [Error: The config in mainConfigFile /home/user/sample-app/app/scripts/general/config/config.js cannot be used because it cannot be evaluated correctly while running in the optimizer. Try only using a config that is also valid JSON, or do not use mainConfigFile and instead copy the config values needed into a build file or command line arguments given to the optimizer.
   Source error from parsing: /home/user/sample-app/app/scripts/general/config/config.js: ReferenceError: localStorage is not defined] }

in my build.config.file I also inlcude an empty path for the nls folder

                paths: {
                    nls: 'empty:', // set to empty to not include lang files into build
                },

I see the point with the valid JSON, but I'm wondering how to built a minified version of my application (with and without including the labels/locale files into the built, depending on the env web or mobile) and still keep going with the localstorage method to store the current locale variable.

Every tip for a solution is appreciated!!

@jrburke
Copy link
Member

jrburke commented Oct 5, 2015

requirejs.config can be called multiple times, with the most recent call taking priority. For mainConfigFile it only finds the first requirejs.config call in a build, so you could do something like this:

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

var storedLocaled = localStorage.getItem('locale');
if (storedLocale) {
  requirejs.config({
    config: {
        i18n: {
            locale: storedLocale
        },
    }
  });
}

// Then load the app
require(['app'], ...);

This assumes that the first requirejs.config() call does not specify "deps" in it, the loading should be triggered after the second requirejs.config() call.

@crebuh
Copy link

crebuh commented Oct 6, 2015

@jrburke

thx it works now in the development mode!

but i still having trouble combining/minifying the project. I am not sure which way to go and which one is the better solution (and as well not how to do this :D):

  1. include both languages into the build file --> if number of translations grow maybe a bad solution but files dont have to be loaded separately (bad on web, ok on mobile)

  2. exlude it from the build and load it separately (good on web, not sure on mobile)

For example I tried the following settings in my build.config.file:

                paths: {
                    nls: 'empty:', // set to empty to not include lang files into build
                },

but then when i try to load the app, i got the following error:

 Uncaught Error: undefined missing nls/labels

How i have to config the build-file to not include the labels-files into the build and load them separately?

Update

The build process works when i change the optimize-modus from uglify2 to none. also uglify doesnt work. but if this doesnt work, where is the point of minifying :-( ... does anybody have an idea what could cause the problem here?

@ianjamieson
Copy link

I have the same issue.

My build config sets locale parameter as below:

locale: 'en-us'

A typical i18n module would contain:

root: {
  defaultSettings: 'values',
  another: 'value'
},
'en-us': true

My en-us module then returns:

{
  defaultSettings: 'values overriden',
  another: 'value overriden'
}

However, when it comes to running the r.js optimizer, both files are included in the compiled source and the browser settings are used to determine the locale - this is not what I want. As I pass the locale config value into r.js optimizer I am expecting it to only compile the en-us language file.

Any support would be helpful

@Txabs
Copy link

Txabs commented May 23, 2016

+1

1 similar comment
@ntpthinh
Copy link

ntpthinh commented Oct 3, 2018

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants