locize / i18next-node-locize-backend

[deprecated] can be replaced with i18next-locize-backend
https://github.com/locize/i18next-locize-backend
MIT License
11 stars 4 forks source link

How to get localize to update missing keys. #2

Open cmosboss opened 6 years ago

cmosboss commented 6 years ago

I am having some trouble figuring out how to add new keys to locize via node. Old developer had setup everything, and I have since added ~40 keys I need to add to the locize service. Below is our locize setup / init. loadFromLocal is false and it is running the "Backend" section of the code.

import i18next from 'i18next';
import { handle, LanguageDetector } from 'i18next-express-middleware';
import config from '../config/app';

const { loadFromLocal, projectId, apiKey } = config.locize;
let backend;
if (loadFromLocal) {
  // eslint-disable-next-line
  console.info('Loading i18next from local files');
  const path = require('path');
  const localDirectory = path.join(__dirname, '../', 'locales');
  const Backend = require('i18next-node-fs-backend');
  backend = new Backend(null, {
    loadPath: `${localDirectory}/{{lng}}/{{ns}}.json`,
    addPath: path.join('../', process.cwd(), '/{{lng}}/{{ns}}.missing.json'),
    // addPath: `${localDirectory}/{{lng}}/{{ns}}.missing.json`,
  });
} else {
  // eslint-disable-next-line
  console.info('Loading i18next from Locize API');
  const Backend = require('i18next-node-locize-backend');
  backend = new Backend(null, {
    projectId,
    apiKey,
    referenceLng: 'en',
  });
}

i18next
  .use(backend)
  .use(LanguageDetector)
  .init({
    debug: false,
    fallbackLng: 'en',
    preload: ['en', 'es', 'zh-Hans', 'zh-Hant'],
    ns: ['common', 'events', 'validation'],
    saveMissing: true,
    missingKeyHandler: (lng, ns, key, fallbackValue) => {
      // eslint-disable-next-line
      console.error('Missing key in translations', lng, ns, key, fallbackValue);
    },
    load: 'languageOnly', // Remove if you want to use localization (en-US, en-GB)
  }, error => {
    if (error) {
      // eslint-disable-next-line
      console.error('Error loading i18next', error);
      throw error;
    }
  });

export const handler = handle(i18next);

const i18n = i18next.cloneInstance({ initImmediate: false });

export const t = i18n.t.bind(i18n);
export const exists = i18n.exists.bind(i18n);

export default i18n;

As you can see we have a "Missing Key Handler" and it is indeed firing on all of my new keys, but none of this is going to locize. What am I missing?

jamuhl commented 6 years ago

@zocheyado that missingKeyHandler is i18next "land" means if set you take care of handling missing keys yourself and the backend won't be triggered.

So i would suggest this change:

// ...
i18next
  .use(backend)
  .use(LanguageDetector)
  .init({
    debug: false,
    fallbackLng: 'en',
    preload: ['en', 'es', 'zh-Hans', 'zh-Hant'],
    ns: ['common', 'events', 'validation'],
    saveMissing: true,

    missingKeyHandler: loadFromLocal ? (lng, ns, key, fallbackValue) => {
      // eslint-disable-next-line
      console.error('Missing key in translations', lng, ns, key, fallbackValue);
    } : false,

    load: 'languageOnly', // Remove if you want to use localization (en-US, en-GB)
  }, error => {
    if (error) {
      // eslint-disable-next-line
      console.error('Error loading i18next', error);
      throw error;
    }
  });
// ...

this way that missingKeyHandler gets only set if not using loadFromLocal...

cmosboss commented 6 years ago

I seem to have found my issue. I added that in during testing to confirm that keys are missing. If I remove that it still does not add the key unless I actually get my code to execute that error message. This is fine I will just have to find a way to get the error messages pre uploaded so they can be translated ahead of time. Thanks!

jamuhl commented 6 years ago

yes...it's not a translation extraction tool...missing works by actually calling that t function for that key. Alternatively you might have a look into:

https://github.com/i18next/i18next-parser

or

https://github.com/i18next/i18next-scanner

to extract translations and import those to locize.