I am using the RequireJS i18n plugin to upload translations to my application. I struggle with the concept of determining the runtime of a preferred user language .
The plugin works well if you use navigator.language to determine your preferred user language, but in my application the user language is stored in the database on the server. Therefore, I need to set the locale at runtime:
require.config({ config: { i18n: { locale: userLocale } } });
So, I need a smart way to install userLocale before RequireJS loaded my application. Does anyone know what would be the best way to achieve this? Features include:
1) Installing userLocale outside of my application in non-AMD mode:
//run Ajax call to determine user localization preferencess var Localization = Localization || getUserLocalization(); //and then... require.config({ config: { i18n: { locale: Localization.userLocale } } }); require(['app']);
This makes me a little sad, as it means that part of my application will be outside of RequireJS and therefore unkempt. It also means that in the global namespace all user localization parameters (language time zone, date format, number format) will be saved.
2) A separate call is required to call localization settings
I am not sure how this works, but it is possible:
var Localization = require(['localization']); require.config({ config: { i18n: { locale: Localization.userLocale } } }); require(['app']);
Perhaps this will not work due to asynchrony? In addition, the app will not have access to the Localization object, so it will still need to be stored as a global variable.
Can anyone see a good solution to this problem? Has anyone used the RequireJS i18n plugin to do something like this?
javascript requirejs internationalization
Simon adcock
source share