i18n,javascript - 有没有办法使用第二个文件来解决差异?

i18n, javascript - is there a way to use a 2nd file for the differences?

我想像现在一样使用我的主要语言的一个文件和英语的第二个文件。 这很好用。

现在我想再添加两个文件。一种仅与主要语言相比有所变化,另一种是针对另一种语言。

换句话说..第一个文件是一个包含所有词典的大文件,但对于特定客户,我需要对一些词进行不同的翻译。我只想写不同的词,而不是写另一个 99% 等于原始文件的文件。

我该怎么做?

这是我的代码:

var i18nextInstance = i18next
  .use(i18nextXHRBackend)
  .use(i18nextBrowserLanguageDetector)
  .init(
    {
      detection: {
        // Escludo localStorage. Praticamente ricorda l'ultima lingua usata.
        //order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
        order: ['querystring', 'cookie', 'navigator', 'htmlTag'],
      },
      fallbackLng: 'en',
      lng: lingua_attiva,
      debug: false,
      ns: ['common'],
      defaultNS: 'common',
      backend: {
        loadPath: './i18n/{{lng}}/{{ns}}.json',
        crossDomain: true,
        parse: function (data) {
          // queste 3 righe sotto mi fanno utilizzare il json che include anche la lingua.
          // infatti, nel caso di un file per lingua, la lingua non andrebbe messa. Solo che ci sono delle estensioni
          // come quella di PHP che la vogliono lo stesso. Per questo la lascio e la escludo.
          try {
            var json = JSON.parse(data); // --> { en: {  ... } }
            var m = Object.keys(json); // --> ['en']
            return json[m[0]]; // --> { common: { ... } }
          } catch (e) {
            alert(e); // error in the above string (in this case, yes)!
          }
        },
      },
    },
    function (err, t) {
      // initialized and ready to go!

      // Se in ingresso non avevo passato nessuna lingua, la imposto adesso con quella rilevata
      if (lingua_attiva == undefined) {
        lingua_attiva = i18nextInstance.language.substr(0, 2);
      }

      // Se la lingua non è tra quelle abilitate, forzo inglese
      if (lingua_attiva != 'it' && lingua_attiva != 'en') {
        lingua_attiva = 'en';
      }

      ConfiguraMainWebsite();
      AggiornaTraduzioni();
    }
  );

// Configuro le opzioni per utilizzare jquery nelle traduzioni
jqueryI18next.init(i18nextInstance, $, {
  tName: 't', // --> appends $.t = i18next.t
  i18nName: 'i18n', // --> appends $.i18n = i18next
  handleName: 'localize', // --> appends $(selector).localize(opts);
  selectorAttr: 'data-i18n', // selector for translating elements
  targetAttr: 'i18n-target', // data-() attribute to grab target element to translate (if diffrent then itself)
  optionsAttr: 'i18n-options', // data-() attribute that contains options, will load/set if useOptionsAttr = true
  useOptionsAttr: true, // see optionsAttr
  parseDefaultValueFromContent: true, // parses default values from content ele.val or ele.text
});

您可以创建一个只有差异的新文件(这意味着密钥与 common 文件中的相同),我们将其命名为 diffs 并使用 i18next namespace fallback功能。

在特定位置,您可以使用 diffs 命名空间(这将加载 diffs.json 文件)并回退到您的 common 命名空间以查找缺少的键。

由于您的配置已被定义为 common 作为 defaultNS,您需要做的只是针对特定用户将命名空间更改为 diffs.

<div class="outer" data-i18n="diffs:key"></div>
// -----------------------------^

$(".outer").localize();

您可以为特定的翻译区域定义命名空间

$(".outer").localize({ns: 'diffs'});
// this will call translation on the `.outer` div with a specific namespace without the need to attach it as I've showed before