Load Locale File Dynamically using Requirejs

后端 未结 2 568
猫巷女王i
猫巷女王i 2021-01-18 18:07

I have a single page Marionette app built on RequireJS which needs to support translations.

My goal is to have a dictionary file for each language, and based on the

2条回答
  •  误落风尘
    2021-01-18 18:39

    You should be able to check user settings, construct a dependency string, pass it to Translator and then use it instead of localeData — r.js will ignore the dynamic dependency but should bundle EN locale.

    if ( userLocale && userLocale !== 'en_US' ) {
    
        var localePath = 'json!locales/' + userLocale + '.json';
        require([ localePath ], function( locale ) {
            var translator = new Translator( locale );
        });
    
    }
    

    and inside Translator: "locale_data": passedData || englishData.

    (or do the same inside the Translator module, like if ( userLocale !== 'en_US' ) { require([path], function(locale) {...}))

    In theory it should work, though you cannot use simplified CommonJS here and should use callback-require, otherwise you'll get Module name ... has not been loaded yet for context error.

提交回复
热议问题