-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(framework): allow using a custom i18n library #4119
Conversation
@vladitasev thanks for your quick action after we have a discussion. I will also verify it in my local environment soon. |
@vladitasev looks good for me. one quick question. for the function of interface MessageBundle {
getText(textObj: string | {key: string, defaultText?: string}, ...params: (string|number)[])
} Maybe there is little different with @xweb/i18n interface MessageBundle {
getText(key: string, values?: FormatTextValue | FormatTextValue[] | null, defaultText?: string): string;
} I think the @xweb/i18n will keep the compatible with the ui5-webcomponent as well. |
I also check the
I think the getText("someKey", [], []) is incorrect here right? i'm not sure of that. |
@alex-zhang Yes, I also noticed that the API of As to your other question: yes, the usage is wrong. The correct usage is |
@vladitasev hi, I‘m not sure this is reasonable, maybe we can discuss about it. const getI18nBundle = packageName => {
if (I18nBundleInstances.has(packageName)) {
return I18nBundleInstances.get(packageName);
}
if (customFetchI18nBundle) {
const customI18nBundle = customFetchI18nBundle(packageName);
I18nBundleInstances.set(packageName, customI18nBundle);
return customI18nBundle;
}
const i18nBundle = new I18nBundle(packageName);
I18nBundleInstances.set(packageName, i18nBundle);
return i18nBundle;
} Because if we call registerCustomI18nHandlers and the customFetchI18nBundle will call every time when we call getI18nBundle, I think it will good for performance. |
Hi @alex-zhang First of all, while working on this topic, we decided to simplify our own i18n implementation by merging To reflect this, I have also updated this PR. As to your comment, yes, the custom function will be called very often, and it is expected to cache the result and not make a network request for the same thing every time. I have updated the sample implementation to reflect this: let bundle;
registerCustomGetI18nBundle(async packageName => {
if (!bundle) {
bundle = await i18n.load(); // potentially use the packageName parameter too
}
return {
getText: (keyObj, ...params) => {
return bundle.get(keyObj.key, params, keyObj.defaultText);
},
};
}); |
hi, @vladitasev thanks~, It's looks good for me. |
This reverts commit e7805da.
Allow using a custom i18n library
This feature allows to plug in a custom
i18n
library and use it for all components transparentlyBackground
All components use 2 APIs for the purpose of
i18n
:async getI18nBundle
to make the request (during thedefine
phase) and get the bundle instancegetText
- to get the texts (the only method of the instance, acquired in the previous step)The method above is imported from the following module:
Proposal
Allow using a third-party i18n library by providing a breakout for
async getI18nBundle
:If the app registers a custom implementation,it will completely replace the stock one (see the change in
i18nBundle.js
).The only requirement is that the
get
function must return an object with agetText
method with the expected signature, because this is what all components will be calling.See the example in :
packages/main/customI18n.js
for a full replacement implementation. If this module is imported in the bundle, all texts for all components become what is defined by the mock implementation ofgetText
.