From 0e021e438820b9be84e43522f5f7aa2e19dcc41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Tue, 24 Jan 2023 23:35:26 +0100 Subject: [PATCH] chore(i18n): initialize spanish properly (detect es-419) (#1428) --- CONTRIBUTING.md | 6 +++--- config/i18n.ts | 1 + utils/language.ts | 9 ++++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b79022b3..becf1f9d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -93,7 +93,7 @@ We are using [vue-i18n](https://vue-i18n.intlify.dev/) via [nuxt-i18n](https://i 1. Add a new file in [locales](./locales) folder with the language code as the filename. 2. Copy [en-US](./locales/en-US.json) and translate the strings. -3. Add the language to the `locales` array in [config/i18n.ts](./config/i18n.ts#L60), below `en` and `ar`: +3. Add the language to the `locales` array in [config/i18n.ts](./config/i18n.ts#L61), below `en` and `ar`: - If your language have multiple country variants, add the generic one for language only (only if there are a lot of common entries, you can always add it as a new one) - Add all country variants in [country variants object](./config/i18n.ts#L12) - Add all country variants files with empty `messages` object: `{}` @@ -102,8 +102,8 @@ We are using [vue-i18n](https://vue-i18n.intlify.dev/) via [nuxt-i18n](https://i - If the generic language already exists: - If the translation doesn't differ from the generic language, then add the corresponding translations in the corresponding file - If the translation differs from the generic language, then add the corresponding translations in the corresponding file and remove it from the country variants entry -4. If the language is `right-to-left`, add `dir` option with `rtl` value, for example, for [ar](./config/i18n.ts#L70) -5. If the language requires special pluralization rules, add `pluralRule` callback option, for example, for [ar](./config/i18n.ts#L71) +4. If the language is `right-to-left`, add `dir` option with `rtl` value, for example, for [ar](./config/i18n.ts#L71) +5. If the language requires special pluralization rules, add `pluralRule` callback option, for example, for [ar](./config/i18n.ts#L72) Check [Pluralization rule callback](https://vue-i18n.intlify.dev/guide/essentials/pluralization.html#custom-pluralization) for more info. diff --git a/config/i18n.ts b/config/i18n.ts index 104258c9..ad706ae5 100644 --- a/config/i18n.ts +++ b/config/i18n.ts @@ -42,6 +42,7 @@ const countryLocaleVariants: Record = { // { code: 'es-DO', name: 'Español (República Dominicana)' }, // { code: 'es-EC', name: 'Español (Ecuador)' }, { code: 'es-ES', name: 'Español (España)' }, + // TODO: Support es-419, if we include spanish country variants remove also fix on utils/language.ts module { code: 'es-419', name: 'Español (Latinoamérica)' }, // { code: 'es-GT', name: 'Español (Guatemala)' }, // { code: 'es-HN', name: 'Español (Honduras)' }, diff --git a/utils/language.ts b/utils/language.ts index 1b41d715..3e26aee9 100644 --- a/utils/language.ts +++ b/utils/language.ts @@ -1,6 +1,13 @@ export function matchLanguages(languages: string[], acceptLanguages: readonly string[]): string | null { { - const lang = acceptLanguages.map(userLang => languages.find(lang => lang.startsWith(userLang))).filter(v => !!v)[0] + // const lang = acceptLanguages.map(userLang => languages.find(lang => lang.startsWith(userLang))).filter(v => !!v)[0] + // TODO: Support es-419, remove this code if we include spanish country variants + const lang = acceptLanguages.map(userLang => languages.find((lang) => { + if (userLang.startsWith('es-') && userLang !== 'es-ES') + return lang === 'es-419' + + return lang.startsWith(userLang) + })).filter(v => !!v)[0] if (lang) return lang }