-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(framework): allow using a custom i18n library (#4119)
- Loading branch information
1 parent
0d153ef
commit 56f366f
Showing
4 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
e4x+vuGaRUwBlCgujSarJaLpsDk= | ||
nonWS/k2fnX+vVo1+Pr6wnqqGJk= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { registerCustomI18nBundleGetter } from "@ui5/webcomponents-base/dist/i18nBundle.js"; | ||
|
||
// Third-party i18n library that has an async "load" function that fetches the i18n bundle and returns an object with a "get" method | ||
const i18n = { | ||
load: async () => { | ||
await Promise.resolve(); // fetch the data here | ||
return { | ||
get: (key, paramsArr, defaultText) => `key: ${key}, params: ${paramsArr.join(", ")}, defaultText: ${defaultText}`, | ||
}; | ||
}, | ||
}; | ||
|
||
let bundle; | ||
registerCustomI18nBundleGetter(async packageName => { | ||
if (!bundle) { | ||
bundle = await i18n.load(); // potentially use the packageName parameter too | ||
} | ||
return { | ||
getText: (keyObj, ...params) => { | ||
const key = typeof keyObj === "string" ? keyObj : keyObj.key; | ||
return bundle.get(key, params, keyObj.defaultText); | ||
}, | ||
}; | ||
}); | ||
|
||
/* | ||
// Example with the i18next library | ||
import i18next from "i18next"; | ||
registerCustomI18nBundleGetter(async packageName => { | ||
await i18next.init({ | ||
lng: "en", // pass the desired language | ||
debug: true, | ||
resources: { // potentially use packageName to determine which translations to load - hardcoded for this example | ||
en: { | ||
translation: { | ||
"key": "hello world", | ||
}, | ||
}, | ||
}, | ||
}); | ||
return { | ||
getText: (keyObj, ...params) => { | ||
let key = typeof keyObj === "string" ? keyObj : keyObj.key; | ||
key = key.replace(/{{/g, "{").replace(/}}/g, "}"); // the key will be with {{0}}, {{1}}, etc. placeholders | ||
return i18next.t(key); // also pass the parameters, with keys "0", "1", etc. | ||
}, | ||
}; | ||
}); | ||
*/ |