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

Codecov Report

Base: 94.08% // Head: 94.08% // No change to project coverage :thumbsup:

Coverage data is based on head (2353dae) compared to base (432cb66). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #763   +/-   ##
=======================================
  Coverage   94.08%   94.08%           
=======================================
  Files          11       11           
  Lines        1742     1742           
=======================================
  Hits         1639     1639           
  Misses        103      103           
Impacted Files Coverage Δ
src/helpers.js 92.17% <100.00%> (ø)
src/transform.js 88.25% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov-commenter avatar Jan 18 '23 09:01 codecov-commenter