MediaWiki:Gadget-I18n-js.js: Difference between revisions

From Tardis Wiki, the free Doctor Who reference
add option to cache some messages for all languages when using optimised cache; restore `inLang()` support
(remember fallback chain and switch to `en` if loop is found)
(add option to cache some messages for all languages when using optimised cache; restore `inLang()` support)
Line 6: Line 6:
  * @author OneTwoThreeFall <https://dev.fandom.com/User:OneTwoThreeFall>
  * @author OneTwoThreeFall <https://dev.fandom.com/User:OneTwoThreeFall>
  *
  *
  * @version 0.6.3
  * @version 0.6.4
  *
  *
  * @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page)
  * @notes Also used by VSTF wiki for their reporting forms (with a non-dev i18n.json page)
Line 574: Line 574:
             * Set the language for the next msg call.
             * Set the language for the next msg call.
             *
             *
             * @deprecated since v0.6 (2020-08-25), no longer supported.
             * @param lang The language code to use for the next `msg` call.
             *
             *
             * @return The current object for use in chaining.
             * @return The current object for use in chaining.
             */
             */
             inLang: function () {
             inLang: function (lang) {
                 console.warn('[I18n-js] “inLang()” is no longer supported by I18n-js (used in “' + name + '”) - using user language.');
                 if (!options.cacheAll) {
                 return this.inUserLang();
                    console.warn('[I18n-js] “inLang()” is not supported without configuring `options.cacheAll` (used in “' + name + '”) - using user language.');
                    lang = options.language;
                }
                tempLang = lang;
                 return this;
             },
             },


Line 682: Line 686:
             });
             });
         });
         });
        // `cacheAll` is an array of message names for which translations
        //  should not be optimised
        // Enumerate over the original messages and save translations
        //  for these messages for each existing language
        if (Array.isArray(options.cacheAll)) {
            Object.keys(messages).forEach(function (lang) {
                if (optimised[lang]) {
                    // already exists
                    return;
                }
                optimised[lang] = {};
                options.cacheAll.forEach(function (msgName) {
                    var msg = messages[lang][msgName];
                    if (msg) {
                        optimised[lang][msgName] = msg;
                    }
                });
            });
        }


         optimised._isOptimised = langs;
         optimised._isOptimised = langs;
Line 817: Line 843:
         }
         }


         if (options.useCache) {
         if (options.useCache && options.cacheAll !== true) {
             json = optimiseMessages(name, json, options);
             json = optimiseMessages(name, json, options);
         }
         }
Line 863: Line 889:
     *    https://dev.fandom.com/wiki/MediaWiki:Custom-name/i18n.json.
     *    https://dev.fandom.com/wiki/MediaWiki:Custom-name/i18n.json.
     * @param options Options set by the loading script:
     * @param options Options set by the loading script:
    *    cacheAll: Either an array of message names for which translations should not be optimised, or `true` to disable the optimised cache.
     *    cacheVersion: Minimum cache version requested by the loading script.
     *    cacheVersion: Minimum cache version requested by the loading script.
     *    language: Set a default language for the script to use, instead of wgUserLanguage.
     *    language: Set a default language for the script to use, instead of wgUserLanguage.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.