Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/translations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@
},
"dependencies": {
"i18next-async-storage-backend2": "^2.0.0",
"i18next-chained-backend": "^3.0.0",
"i18next-fetch-backend": "^3.0.0",
"i18next-localstorage-backend": "^3.1.3",
"i18next-multiload-backend-adapter": "^1.0.0"
"i18next-chained-backend": "^4",
"i18next-fetch-backend": "^4",
"i18next-localstorage-backend": "^4",
"i18next-multiload-backend-adapter": "^2"
},
"peerDependencies": {
"i18next": "^20.0.0",
"i18next": "^22",
"react": ">=16.x"
},
"devDependencies": {
"@types/node": "^16.11.6",
"@types/react": "~17.0.44",
"i18next": "^20.0.0",
"@types/node": "^20",
"@types/react": "^18",
"i18next": "^22",
"parcel": "^2.8.3",
"tslib": "^2.3.1",
"typescript": "~4.6.4"
Expand Down
28 changes: 15 additions & 13 deletions packages/translations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,34 @@ import type {
TranslationRow,
} from './types'

let initializing = false
let initializing: string | undefined

/**
* A hook which initializes i18next and loads the languages from an external data source
* @param initParams takes fetch options cache expiration time and all i18next params
* @returns boolean which indicates if the languages are loaded
* @returns string or undefined which indicates the language that was loaded
*/
export function useTranslations(
initParameters: TranslationInitParameters
): boolean {
const [initialized, setInitialized] = useState(i18next.isInitialized || false)
export function useTranslations(initParameters: TranslationInitParameters) {
const [initialized, setInitialized] = useState(
i18next.isInitialized ? initParameters.lng : undefined
)

const lng = initParameters.lng
useEffect(() => {
if (!initializing && !i18next.isInitialized) {
initializing = true
initTranslations(initParameters).then(() => {
setInitialized(true)
initializing = false
})
initializing = lng
initTranslations(initParameters).then(() => setInitialized(initializing))
} else if (initializing !== lng) {
initializing = lng
i18next.changeLanguage(lng).then(() => setInitialized(initializing))
}
}, [])
}, [lng])

return initialized
}

export function initTranslations({
storagePrefix = 'i18n_',
expirationTime,
fetchOptions,
...options
Expand All @@ -60,7 +62,7 @@ export function initTranslations({
backend: {
backends: [StorageBackend, MultiloadAdapter],
backendOptions: [
{ prefix: 'i18n_', expirationTime },
{ prefix: storagePrefix, expirationTime },
{ backend: Fetch, backendOption: fetchOptions },
],
},
Expand Down
2 changes: 2 additions & 0 deletions packages/translations/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface TranslationRow {
}

export interface TranslationInitParameters extends InitOptions {
/** allows to bust the cache in a declarative way, warning: previous cached versions are kept in storage forever */
storagePrefix?: string
/** expirationTime time between between revalidation intervals, defaults to 1 week */
expirationTime?: number
/** Configuration for 'i18next-fetch-backend' */
Expand Down
63 changes: 34 additions & 29 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1123,10 +1123,10 @@
resolved "https://registry.yarnpkg.com/@babel/regjsgen/-/regjsgen-0.8.0.tgz#f0ba69b075e1f05fb2825b7fad991e7adbb18310"
integrity sha512-x/rqGMdzj+fWZvCOYForTghzbtqPDZ5gPwaoNGHdgDfF2QA/XZbCBp4Moo5scrkAMPhB7z26XM/AaHuIJdgauA==

"@babel/runtime@^7.12.0", "@babel/runtime@^7.14.6", "@babel/runtime@^7.15.4", "@babel/runtime@^7.16.0", "@babel/runtime@^7.18.9", "@babel/runtime@^7.20.7", "@babel/runtime@^7.8.4":
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.15.4", "@babel/runtime@^7.16.0", "@babel/runtime@^7.19.4", "@babel/runtime@^7.20.6", "@babel/runtime@^7.20.7", "@babel/runtime@^7.8.4":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200"
integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q==
dependencies:
regenerator-runtime "^0.13.11"

Expand Down Expand Up @@ -3493,11 +3493,16 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.12.tgz#833756634e78c829e1254db006468dadbb0c696b"
integrity sha512-Wha1UwsB3CYdqUm2PPzh/1gujGCNtWVUYF0mB00fJFoR4gTyWTDPjSm+zBF787Ahw8vSGgBja90MkgFwvB86Dg==

"@types/node@^16.11.6", "@types/node@^16.9.0":
"@types/node@^16.9.0":
version "16.18.24"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.18.24.tgz#f21925dd56cd3467b4e1e0c5071d0f2af5e9a316"
integrity sha512-zvSN2Esek1aeLdKDYuntKAYjti9Z2oT4I8bfkLLhIxHlv3dwZ5vvATxOc31820iYm4hQRCwjUgDpwSMFjfTUnw==

"@types/node@^20":
version "20.1.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.1.1.tgz#afc492e8dbe7f672dd3a13674823522b467a45ad"
integrity sha512-uKBEevTNb+l6/aCQaKVnUModfEMjAl98lw2Si9P5y4hLu9tm6AlX2ZIoXZX6Wh9lJueYPrGPKk5WMCNHg/u6/A==

"@types/node@~16.11.38":
version "16.11.68"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.68.tgz#30ee923f4d940793e0380f5ce61c0bd4b7196b6c"
Expand Down Expand Up @@ -3530,7 +3535,7 @@
dependencies:
"@types/react" "^17"

"@types/react@*", "@types/react@^17", "@types/react@~17.0.44":
"@types/react@*", "@types/react@^17", "@types/react@^18", "@types/react@~17.0.44":
version "17.0.58"
resolved "https://registry.yarnpkg.com/@types/react/-/react-17.0.58.tgz#c8bbc82114e5c29001548ebe8ed6c4ba4d3c9fb0"
integrity sha512-c1GzVY97P0fGxwGxhYq989j4XwlcHQoto6wQISOC2v6wm3h0PORRWJFHlkRjfGsiG3y1609WdQ+J+tKxvrEd6A==
Expand Down Expand Up @@ -7688,36 +7693,36 @@ i18next-async-storage-backend2@^2.0.0:
resolved "https://registry.yarnpkg.com/i18next-async-storage-backend2/-/i18next-async-storage-backend2-2.0.1.tgz#cd1fed4cc426106ecf957c7a10f45509a6002bfa"
integrity sha512-VIGKPr4bpe7K07GG+kS3mu8nNmttYbY2aty8xEMNtNIEoSVspo+HAOzPVbApV41zjnj/+driHD805wS9PyHDoA==

i18next-chained-backend@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/i18next-chained-backend/-/i18next-chained-backend-3.1.0.tgz#b37d2980598deb8ec5656eba0df8fe15d3d62f1f"
integrity sha512-ltWy0fPMrtXjq0rSLo7s4ZF92LGvVsO47hhV3czaZXzMzhpFva6LVKMyDT7x82R8vAyB7VAeyGLBvMiW4W543A==
i18next-chained-backend@^4:
version "4.2.0"
resolved "https://registry.yarnpkg.com/i18next-chained-backend/-/i18next-chained-backend-4.2.0.tgz#8d2ccba6f2faca14d2bc5023ba61c759215b99a9"
integrity sha512-p3qhv6GMouhpcFPnOBqSDsQyVrwZu+PKr+kXaEAGE+w9O39t4pbpBsFYjxh4Nvhn8MUEXstaDx4ZuQvsNlI5fg==
dependencies:
"@babel/runtime" "^7.18.9"
"@babel/runtime" "^7.19.4"

i18next-fetch-backend@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/i18next-fetch-backend/-/i18next-fetch-backend-3.0.0.tgz#1f6a4c23c81141c8db52c751e989500db4c8d038"
integrity sha512-UiK9dI81qzEtnhgpKcRPAo67ChYaUaIi1kfRfN+vuFEoP3SPttuIY1RJAnVh7qaYzS573cZtG/7KE+qq5Dn1YQ==
i18next-fetch-backend@^4:
version "4.1.3"
resolved "https://registry.yarnpkg.com/i18next-fetch-backend/-/i18next-fetch-backend-4.1.3.tgz#738ab985f8c77aff85319d0bd57e0d40f0b28888"
integrity sha512-y2QBzUpMlF6oDUGfpUbaXp2OeTrKiPTk2OmTSN4ys/HpZ8NTPpXeBgLhrGAir9Jmq3UFabvsqn+BUWZ65jedbQ==

i18next-localstorage-backend@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/i18next-localstorage-backend/-/i18next-localstorage-backend-3.1.3.tgz#5eaad25a515bdadebeb13e1486acfa6fa1686cbe"
integrity sha512-tx8dxQTEsTnRC654IrXPFr94c3NH7bIVHGKHnGvbgefpLz13/uFT5ITsmhqhg/gOza0TIj8e5jTsGnQytIhh+A==
i18next-localstorage-backend@^4:
version "4.1.1"
resolved "https://registry.yarnpkg.com/i18next-localstorage-backend/-/i18next-localstorage-backend-4.1.1.tgz#a27d38b3e933f911c03bfabc03ebd889b90eaf2b"
integrity sha512-gz3OP6m0jygF2SAzarDSFpaziYzF8mS8DP/pEUze+kDquzHTz2sy/eshcSkmFEYyGXMC037xUtlT97bNGlpZPA==
dependencies:
"@babel/runtime" "^7.14.6"
"@babel/runtime" "^7.20.6"

i18next-multiload-backend-adapter@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/i18next-multiload-backend-adapter/-/i18next-multiload-backend-adapter-1.0.0.tgz#3cc3ea102814273bb9059a317d04a3b6e4316121"
integrity sha512-rZd/Qmr7KkGktVgJa78GPLXEnd51OyB2I9qmbI/mXKPm3MWbXwplIApqmZgxkPC9ce+b8Jnk227qX62W9SaLPQ==
i18next-multiload-backend-adapter@^2:
version "2.2.0"
resolved "https://registry.yarnpkg.com/i18next-multiload-backend-adapter/-/i18next-multiload-backend-adapter-2.2.0.tgz#79fc716f3027aaec2fdacb5f566b1caf4d18ce46"
integrity sha512-V7or5zeiCMjWC45vFrlkNgef+y4PH9B2PGcoRZnypjnPIn9cFoEEaJoqHXIFnKsZeyjzFDrhAwE57GFyNVyW1g==

i18next@^20.0.0:
version "20.6.1"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-20.6.1.tgz#535e5f6e5baeb685c7d25df70db63bf3cc0aa345"
integrity sha512-yCMYTMEJ9ihCwEQQ3phLo7I/Pwycf8uAx+sRHwwk5U9Aui/IZYgQRyMqXafQOw5QQ7DM1Z+WyEXWIqSuJHhG2A==
i18next@^22:
version "22.4.15"
resolved "https://registry.yarnpkg.com/i18next/-/i18next-22.4.15.tgz#951882b751872994f8502b5a6ef6f796e6a7d7f8"
integrity sha512-yYudtbFrrmWKLEhl6jvKUYyYunj4bTBCe2qIUYAxbXoPusY7YmdwPvOE6fx6UIfWvmlbCWDItr7wIs8KEBZ5Zg==
dependencies:
"@babel/runtime" "^7.12.0"
"@babel/runtime" "^7.20.6"

iconv-lite@0.4.24, iconv-lite@^0.4.24:
version "0.4.24"
Expand Down