i18next / i18next-parser

Parse your code to extract translation keys/values and manage your catalog files
MIT License
488 stars 199 forks source link

defaultValue support async function #763

Open guoyunhe opened 1 year ago

guoyunhe commented 1 year ago

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

codecov-commenter commented 1 year ago

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 ```diff @@ Coverage Diff @@ ## master #763 +/- ## ======================================= Coverage 94.08% 94.08% ======================================= Files 11 11 Lines 1742 1742 ======================================= Hits 1639 1639 Misses 103 103 ``` | [Impacted Files](https://codecov.io/gh/i18next/i18next-parser/pull/763?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next) | Coverage Δ | | |---|---|---| | [src/helpers.js](https://codecov.io/gh/i18next/i18next-parser/pull/763?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next#diff-c3JjL2hlbHBlcnMuanM=) | `92.17% <100.00%> (ø)` | | | [src/transform.js](https://codecov.io/gh/i18next/i18next-parser/pull/763?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next#diff-c3JjL3RyYW5zZm9ybS5qcw==) | `88.25% <100.00%> (ø)` | | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=i18next)

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