i18next-parser icon indicating copy to clipboard operation
i18next-parser copied to clipboard

defaultValue support async function

Open guoyunhe opened this issue 2 years ago • 1 comments

Why am I submitting this PR

With async defaultValue config, users can call Google/Bing Translate API to initialize translations:

import bingTranslateApi from 'bing-translate-api';
import { languageCodes } from './src/config/i18n';

export default {
  locales: languageCodes,
  input: ['src/**/*.{js,jsx,ts,tsx}'],
  output: 'public/locales/$LOCALE/$NAMESPACE.json',
  indentation: 2,
  keySeparator: false,
  namespaceSeparator: false,
  defaultValue: async (locale: string, namespace: string, key: string) => {
    if (locale === 'en') {
      return key;
    } else {
      try {
        const result = await bingTranslateApi.translate(key, 'en', locale);
        return result.translation;
      } catch (e) {
        console.log(e);
        return '';
      }
    }
  },
};

Does it fix an existing ticket?

Yes/No #000

Checklist

  • [x] only relevant code is changed (make a diff before you submit the PR)
  • [x] tests are included and pass: yarn test (see details here)
  • [x] documentation is changed or added

guoyunhe avatar Jan 18 '23 09:01 guoyunhe