fallback properly with pluralized strings (#7495)
Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
parent
368085982f
commit
5cfb046816
2 changed files with 69 additions and 26 deletions
|
@ -41,6 +41,7 @@ counterpart.setSeparator('|');
|
|||
|
||||
// see `translateWithFallback` for an explanation of fallback handling
|
||||
const FALLBACK_LOCALE = 'en';
|
||||
counterpart.setFallbackLocale(FALLBACK_LOCALE);
|
||||
|
||||
interface ITranslatableError extends Error {
|
||||
translatedMessage: string;
|
||||
|
@ -79,12 +80,12 @@ export function _td(s: string): string { // eslint-disable-line @typescript-esli
|
|||
* should be wrapped with an appropriate `lang='en'` attribute
|
||||
* counterpart's `translate` doesn't expose a way to determine if the resulting translation
|
||||
* is in the target locale or a fallback locale
|
||||
* for this reason, we do not set a fallback via `counterpart.setFallbackLocale`
|
||||
* for this reason, force fallbackLocale === locale in the first call to translate
|
||||
* and fallback 'manually' so we can mark fallback strings appropriately
|
||||
* */
|
||||
const translateWithFallback = (text: string, options?: object): { translated?: string, isFallback?: boolean } => {
|
||||
const translated = counterpart.translate(text, options);
|
||||
if (/^missing translation:/.test(translated)) {
|
||||
const translated = counterpart.translate(text, { ...options, fallbackLocale: counterpart.getLocale() });
|
||||
if (!translated || /^missing translation:/.test(translated)) {
|
||||
const fallbackTranslated = counterpart.translate(text, { ...options, locale: FALLBACK_LOCALE });
|
||||
return { translated: fallbackTranslated, isFallback: true };
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue