Skip to content

Commit

Permalink
use fallback lang if browser language has no translations
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadariaga committed Mar 4, 2024
1 parent b37c917 commit 9ce8d24
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
15 changes: 14 additions & 1 deletion demo/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import sprintf from 'i18next-sprintf-postprocessor';
import { initReactI18next } from 'react-i18next';
import translations from 'translations/index';

const fallbackLng = 'en-US';

i18n
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
Expand All @@ -16,14 +18,25 @@ i18n
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
debug: false,
fallbackLng: 'en',
fallbackLng,
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
overloadTranslationOptionHandler: sprintf.overloadTranslationOptionHandler,
resources: {
...translations,
},
detection: {
convertDetectedLanguage: (lng) => {
const availableLangKeys = Object.keys(translations);
const currentLangKey = lng.substring(0, 2).toLocaleLowerCase();
if (!availableLangKeys.includes(currentLangKey)) {
return fallbackLng;
}

return lng;
}
},
});

export default i18n;
6 changes: 3 additions & 3 deletions library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@irontec/ivoz-ui",
"version": "1.1.21",
"version": "1.1.22",
"description": "UI library used in ivozprovider",
"license": "GPL-3.0",
"main": "index.js",
Expand Down Expand Up @@ -52,8 +52,8 @@
"file-saver": "^2.0.5",
"formik": "^2.2.6",
"hoist-non-react-statics": "^3.3.2",
"i18next": "^23.2.1",
"i18next-browser-languagedetector": "^7.0.2",
"i18next": "^23.10.0",
"i18next-browser-languagedetector": "^7.2.0",
"i18next-sprintf-postprocessor": "^0.2.2",
"query-string": "^7.0.0",
"react-audio-player": "^0.17.0",
Expand Down
44 changes: 28 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,26 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@^7.19.4", "@babel/runtime@^7.22.5":
"@babel/runtime@^7.20.6", "@babel/runtime@^7.21.0", "@babel/runtime@^7.3.1", "@babel/runtime@^7.8.3":
version "7.21.0"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.0.tgz#5b55c9d394e5fcf304909a8b00c07dc217b56673"
integrity sha512-xwII0//EObnq89Ji5AKYQaRYiW/nZ3llSv29d49IuxPhKbtJoLP+9QUUZ4nVragQVtaVGeZrpB+ZtG/Pdy/POw==
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.22.5":
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.5.tgz#8564dd588182ce0047d55d7a75e93921107b57ec"
integrity sha512-ecjvYlnAaZ/KVneE/OdKYBYfgXV3Ptu6zQWmgEF7vwKhQnvVS6bjMD2XYgj+SNvQ1GfK/pjgokfPkC/2CO8CuA==
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.20.6", "@babel/runtime@^7.21.0", "@babel/runtime@^7.3.1", "@babel/runtime@^7.8.3":
version "7.21.0"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.0.tgz#5b55c9d394e5fcf304909a8b00c07dc217b56673"
integrity sha512-xwII0//EObnq89Ji5AKYQaRYiW/nZ3llSv29d49IuxPhKbtJoLP+9QUUZ4nVragQVtaVGeZrpB+ZtG/Pdy/POw==
"@babel/runtime@^7.23.2":
version "7.24.0"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.24.0.tgz#584c450063ffda59697021430cb47101b085951e"
integrity sha512-Chk32uHMg6TnQdvw2e9IlqPpFX/6NLuK0Ys2PqLb7/gL5uFn9mXvK715FGLlOLQrcO4qIkNHkvPGktzzXexsFw==
dependencies:
regenerator-runtime "^0.13.11"
regenerator-runtime "^0.14.0"

"@babel/template@^7.20.7":
version "7.20.7"
Expand Down Expand Up @@ -4276,12 +4283,12 @@ hyphenate-style-name@^1.0.3:
resolved "https://registry.yarnpkg.com/hyphenate-style-name/-/hyphenate-style-name-1.0.4.tgz#691879af8e220aea5750e8827db4ef62a54e361d"
integrity sha512-ygGZLjmXfPHj+ZWh6LwbC37l43MhfztxetbFCoYTM2VjkIUpeHgSNn7QIyVFj7YQ1Wl9Cbw5sholVJPzWvC2MQ==

i18next-browser-languagedetector@^7.0.2:
version "7.0.2"
resolved "https://registry.yarnpkg.com/i18next-browser-languagedetector/-/i18next-browser-languagedetector-7.0.2.tgz#22d8ed48411750c1a1828ac2031c816a5108e55f"
integrity sha512-5ViaK+gikxfqZ9M3jJ7gJkUzzu/p3HwiqfLoL1bdiL7CUb0IylcTyVLdPaTU3pH5VFWFCiGFuJDg3VkLUikWgg==
i18next-browser-languagedetector@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/i18next-browser-languagedetector/-/i18next-browser-languagedetector-7.2.0.tgz#de0321cba6881be37d82e20e4d6f05aa75f6e37f"
integrity sha512-U00DbDtFIYD3wkWsr2aVGfXGAj2TgnELzOX9qv8bT0aJtvPV9CRO77h+vgmHFBMe7LAxdwvT/7VkCWGya6L3tA==
dependencies:
"@babel/runtime" "^7.19.4"
"@babel/runtime" "^7.23.2"

i18next-parser@^7.7.0:
version "7.7.0"
Expand Down Expand Up @@ -4321,12 +4328,12 @@ i18next@^22.0.4:
dependencies:
"@babel/runtime" "^7.20.6"

i18next@^23.2.1:
version "23.2.1"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-23.2.1.tgz#1117d0fdeb10e5db68f5f404d31e68b11e1196cf"
integrity sha512-BAWWqXYM1OtgA2hkGvnXWTs9wg0g3qx0SoiJoVTiL5Ia2EDZK0Veyuq4m2YtZeSs9D/9cwrNFxCKJi2/gdjZ4A==
i18next@^23.10.0:
version "23.10.0"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-23.10.0.tgz#fb328794ae692e6fdde0564259e421f4203c4a2c"
integrity sha512-/TgHOqsa7/9abUKJjdPeydoyDc0oTi/7u9F8lMSj6ufg4cbC1Oj3f/Jja7zj7WRIhEQKB7Q4eN6y68I9RDxxGQ==
dependencies:
"@babel/runtime" "^7.22.5"
"@babel/runtime" "^7.23.2"

[email protected]:
version "0.4.24"
Expand Down Expand Up @@ -6258,6 +6265,11 @@ regenerator-runtime@^0.13.4:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52"
integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==

regenerator-runtime@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==

regexp.prototype.flags@^1.4.1, regexp.prototype.flags@^1.4.3:
version "1.4.3"
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.4.3.tgz#87cab30f80f66660181a3bb7bf5981a872b367ac"
Expand Down

0 comments on commit 9ce8d24

Please sign in to comment.