-
Couldn't load subscription status.
- Fork 71
Description
As requested by @Rob--W, splitting this proposal off from #258
Why
- To be able to fetch messages from a different language than the browser UI language. This can for example be useful if you want to display your extension in a different language.
- Fetching the language files manually is not really a doable alternative as it does not handle fallbacks to less specific language tags and English while adding a lot of boilerplate.
Proposal A
Introduce a i18n.getLanguageDictionary method. In which the first and only parameter is the language tag as string. Returning a promise with the languageDictionary allowing you to request messages from as follows:
browser.i18n.getLanguageDictionary('pt-BR').then((api) => {
let someMessage = api.getMessage('some_id');
});Or with async/await:
const brazilApi = await browser.i18n.getLanguageDictionary('pt-BR');
let someMessage = brazilApi.getMessage('some_id');The name of this method can be worked on. Potential alternatives are createLanguageInstance and getLanguageInstance.
A variant on this is directly passing the getMessage method as suggested by @hanguokai in #274 (comment) .
browser.i18n.getLanguageDictionary('pt-BR').then((getMessage) => {
let someMessage = getMessage('some_id');
});Or with async/await:
const getPtBrMessage = await browser.i18n.getLanguageDictionary('pt-BR');
let someMessage = getPtBrMessage('some_id');Proposal B
Introduce a i18n.getLanguageDictionary method. In which the first and only parameter is the language tag as string. Returning a promise with the languageDictionary allowing you to request messages from as follows:
browser.i18n.getLanguageDictionary('pt-BR').then((languageDictionary) => {
let someMessage = browser.i18n.getMessage('some_id', substitutions?, {
dictionary: languageDictionary,
});
});Proposal C
Introduce an asynci18n.initLanguage method. In which the first and only parameter is the language tag as string. This would initialise the messagedictionary on the background. The messages can then be fetched sync by passing the language parameter as option.
browser.i18n.initLanguage('pt-BR').then(() => {
let someMessage = browser.i18n.getMessage('some_id', substitutions?, {
language:'pt-BR',
});
});Proposal D
Allow passing the requested language synchronously. Most convenient for developers however potentially tricky implementation-wise.
let someMessage = browser.i18n.getMessage('some_id', substitutions?, {
language:'pt-BR',
});Proposal E
(from @hanguokai comment #274 (comment))
// set language to fr
await browser.i18n.setLanguage('fr');
browser.i18n.getMessage('hello'); // return France message: "Bonjour"
// set language to zh-CN
await browser.i18n.setLanguage('zh-CN');
browser.i18n.getMessage('hello'); // return Simplified Chinese message: "你好"This is similar to #258 . The difference is that #258 change the default language globally (all contexts) and persistently, but this proposal only change the language in the current context. For example, it doesn't affect another extension page.
Edge cases
- If the passed language parameter is not of type string, it should throw an exception.
- If the language tag can not be found, just like the normal
getMessagemethod, it attempt to check for other available files. In the above example, it would first check pt-BR, then pt, then the default_locale (often english). Firefox skips the pt if the message is not present in pt-BR, yet is available in pt. We might want to raise a separate issue for this.