| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | // @lint-ignore-every SPELL |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import React, {createContext, useContext, useEffect, useState} from 'react'; |
| b69ab31 | | | 13 | import {TypedEventEmitter} from 'shared/TypedEventEmitter'; |
| b69ab31 | | | 14 | import * as en from './en/common.json'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | /** |
| b69ab31 | | | 17 | * ISO 639-3 language code used to control which translation we use |
| b69ab31 | | | 18 | */ |
| b69ab31 | | | 19 | type LanguageId = string; |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | // TODO: these language files should be lazilly loaded rather than bundled directly |
| b69ab31 | | | 22 | const langs: {[key: string]: {[key: string]: string}} = { |
| b69ab31 | | | 23 | en, |
| b69ab31 | | | 24 | // Add other languages here! |
| b69ab31 | | | 25 | }; |
| b69ab31 | | | 26 | |
| b69ab31 | | | 27 | declare global { |
| b69ab31 | | | 28 | interface Window { |
| b69ab31 | | | 29 | // language may be pre-defined ahead of time by the HTML in the window |
| b69ab31 | | | 30 | saplingLanguage?: string; |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | } |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | let currentLanguage: LanguageId = |
| b69ab31 | | | 35 | (typeof window !== 'undefined' ? window.saplingLanguage : null) ?? 'en'; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | const I18nContext = createContext(currentLanguage); |
| b69ab31 | | | 38 | export const onChangeLanguage = new TypedEventEmitter<'change', string>(); |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | /** |
| b69ab31 | | | 41 | * We need to re-render translated components when the language is changed. |
| b69ab31 | | | 42 | * React context lets us easily re-render any component using the language. |
| b69ab31 | | | 43 | */ |
| b69ab31 | | | 44 | export function I18nSupport({children}: {children: React.ReactNode}) { |
| b69ab31 | | | 45 | const [lang, setLang] = useState(currentLanguage); |
| b69ab31 | | | 46 | useEffect(() => { |
| b69ab31 | | | 47 | onChangeLanguage.on('change', setLang); |
| b69ab31 | | | 48 | return () => void onChangeLanguage.off('change', setLang); |
| b69ab31 | | | 49 | }, []); |
| b69ab31 | | | 50 | return <I18nContext.Provider value={lang}>{children}</I18nContext.Provider>; |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | export function getCurrentLanguage(): LanguageId { |
| b69ab31 | | | 54 | return currentLanguage; |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | export function setCurrentLanguage(lang: LanguageId) { |
| b69ab31 | | | 57 | currentLanguage = lang; |
| b69ab31 | | | 58 | onChangeLanguage.emit('change', currentLanguage); |
| b69ab31 | | | 59 | } |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | /** |
| b69ab31 | | | 62 | * what key suffixes to use for count-based translations |
| b69ab31 | | | 63 | * e.g., in en, myStringKey_one = 'open $count file', myStringKey_other = 'open $count files' |
| b69ab31 | | | 64 | * so when you call t('myStringKey', {count: 4}) it uses the right plural. |
| b69ab31 | | | 65 | * Different languages have different rules for plurals. |
| b69ab31 | | | 66 | */ |
| b69ab31 | | | 67 | const pluralizers: {[key in keyof typeof langs]: (n: number) => string} = { |
| b69ab31 | | | 68 | en: (n: number) => (n == 1 ? 'one' : 'other'), |
| b69ab31 | | | 69 | }; |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | /** |
| b69ab31 | | | 72 | * Translate provided en-language string. All user-visible strings should use t() or <T></T>, including |
| b69ab31 | | | 73 | * title texts, tooltips, and error messages. |
| b69ab31 | | | 74 | * Generally, the parameter is taken to be the english translation directly. |
| b69ab31 | | | 75 | * You can also use a generic key and define an en translation. |
| b69ab31 | | | 76 | * ``` |
| b69ab31 | | | 77 | * t('Cancel') -> 'Cancel' if current language is 'en', |
| b69ab31 | | | 78 | * t('Cancel') -> 'Abbrechen' if current language is 'de', etc |
| b69ab31 | | | 79 | * ``` |
| b69ab31 | | | 80 | * To pluralize, pass a `count` option. Then define translations with keys according to the pluralization rules |
| b69ab31 | | | 81 | * in {@link pluralizers}. |
| b69ab31 | | | 82 | * ``` |
| b69ab31 | | | 83 | * t('confirmFilesSave', {count: 1}) -> lookup en 'confirmFilesSave_one' -> 'Save 1 file' if current language is 'en' |
| b69ab31 | | | 84 | * t('confirmFilesSave', {count: 4}) -> lookup en 'confirmFilesSave_other' -> 'Save 4 files' if current language is 'en' |
| b69ab31 | | | 85 | * t('confirmFilesSave', {count: 4}) -> lookup de 'confirmFilesSave_other' -> 'Speichern Sie 4 Dateien' if current language is 'de' |
| b69ab31 | | | 86 | * ``` |
| b69ab31 | | | 87 | * To include arbitrary opaque contents that are not translated, you can provide a replacer: |
| b69ab31 | | | 88 | * ``` |
| b69ab31 | | | 89 | * t('Hello, my name is $name.', {replace: {$name: getName()}}) |
| b69ab31 | | | 90 | * ``` |
| b69ab31 | | | 91 | * {@link T See also <T> React component} |
| b69ab31 | | | 92 | */ |
| b69ab31 | | | 93 | export function t( |
| b69ab31 | | | 94 | i18nKeyOrEnText: string, |
| b69ab31 | | | 95 | options?: {count?: number; replace?: {[key: string]: string}}, |
| b69ab31 | | | 96 | ) { |
| b69ab31 | | | 97 | return translate(i18nKeyOrEnText, options).join(''); |
| b69ab31 | | | 98 | } |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | /** |
| b69ab31 | | | 101 | * Translates contents. Re-renders when language is updated. |
| b69ab31 | | | 102 | * {@link t See t() function documentation} |
| b69ab31 | | | 103 | * |
| b69ab31 | | | 104 | * Unlike `t()`, `options.replace` can include arbitrary `ReactNode` contents |
| b69ab31 | | | 105 | * ``` |
| b69ab31 | | | 106 | * <T replace={{$name: <b>{getName()}</b>}}>Hello, my name is $name.</T> |
| b69ab31 | | | 107 | * ``` |
| b69ab31 | | | 108 | */ |
| b69ab31 | | | 109 | export function T({ |
| b69ab31 | | | 110 | children, |
| b69ab31 | | | 111 | count, |
| b69ab31 | | | 112 | replace, |
| b69ab31 | | | 113 | }: { |
| b69ab31 | | | 114 | children: string; |
| b69ab31 | | | 115 | count?: number; |
| b69ab31 | | | 116 | opaque?: ReactNode; |
| b69ab31 | | | 117 | replace?: {[key: string]: string | ReactNode}; |
| b69ab31 | | | 118 | }): JSX.Element { |
| b69ab31 | | | 119 | // trigger re-render if the language is changed |
| b69ab31 | | | 120 | useContext(I18nContext); |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | return <>{translate(children, {count, replace})}</>; |
| b69ab31 | | | 123 | } |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | function translate( |
| b69ab31 | | | 126 | i18nKeyOrEnText: string, |
| b69ab31 | | | 127 | options?: {count?: number; replace?: {[key: string]: string | ReactNode}}, |
| b69ab31 | | | 128 | ): Array<string | ReactNode> { |
| b69ab31 | | | 129 | let result; |
| b69ab31 | | | 130 | if (options?.count != null) { |
| b69ab31 | | | 131 | const pluralized = |
| b69ab31 | | | 132 | getPlural(i18nKeyOrEnText, options.count, currentLanguage) ?? |
| b69ab31 | | | 133 | // fallback to pluralized en if the currentLanguage doesn't have this key |
| b69ab31 | | | 134 | getPlural(i18nKeyOrEnText, options.count, 'en') ?? |
| b69ab31 | | | 135 | // last resort is to use the key directly |
| b69ab31 | | | 136 | i18nKeyOrEnText; |
| b69ab31 | | | 137 | // replace number into the appropriate location |
| b69ab31 | | | 138 | result = pluralized.replace(/\$count/g, String(options.count)); |
| b69ab31 | | | 139 | } |
| b69ab31 | | | 140 | if (!result) { |
| b69ab31 | | | 141 | result = |
| b69ab31 | | | 142 | langs[currentLanguage]?.[i18nKeyOrEnText] ?? langs.en[i18nKeyOrEnText] ?? i18nKeyOrEnText; |
| b69ab31 | | | 143 | } |
| b69ab31 | | | 144 | if (options?.replace) { |
| b69ab31 | | | 145 | // if we split with a regexp match group, the value will stay in the array, |
| b69ab31 | | | 146 | // so it can be replaced later |
| b69ab31 | | | 147 | const regex = new RegExp( |
| b69ab31 | | | 148 | // this requires escaping so special characters like $ can be used |
| b69ab31 | | | 149 | '(' + Object.keys(options.replace).map(escapeForRegExp).join('|') + ')', |
| b69ab31 | | | 150 | 'g', |
| b69ab31 | | | 151 | ); |
| b69ab31 | | | 152 | const parts = result.split(regex); |
| b69ab31 | | | 153 | return ( |
| b69ab31 | | | 154 | parts |
| b69ab31 | | | 155 | .map(part => options.replace?.[part] ?? part) |
| b69ab31 | | | 156 | // if we replace with a component, we need to set a key or react will complain |
| b69ab31 | | | 157 | .map((part, i) => |
| b69ab31 | | | 158 | typeof part === 'object' ? ({...part, key: String(i)} as ReactNode) : part, |
| b69ab31 | | | 159 | ) |
| b69ab31 | | | 160 | ); |
| b69ab31 | | | 161 | } |
| b69ab31 | | | 162 | return [result]; |
| b69ab31 | | | 163 | } |
| b69ab31 | | | 164 | |
| b69ab31 | | | 165 | function escapeForRegExp(s: string) { |
| b69ab31 | | | 166 | return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string |
| b69ab31 | | | 167 | } |
| b69ab31 | | | 168 | |
| b69ab31 | | | 169 | /** |
| b69ab31 | | | 170 | * Returns current language. Also triggers re-renders if the language is changed. |
| b69ab31 | | | 171 | */ |
| b69ab31 | | | 172 | export function useCurrentLang(): LanguageId { |
| b69ab31 | | | 173 | useContext(I18nContext); |
| b69ab31 | | | 174 | return currentLanguage; |
| b69ab31 | | | 175 | } |
| b69ab31 | | | 176 | |
| b69ab31 | | | 177 | function getPlural(i18nKeyOrEnText: string, count: number, lang: LanguageId): string | undefined { |
| b69ab31 | | | 178 | const pluralizer = pluralizers[lang]; |
| b69ab31 | | | 179 | if (pluralizer == null) { |
| b69ab31 | | | 180 | return undefined; |
| b69ab31 | | | 181 | } |
| b69ab31 | | | 182 | |
| b69ab31 | | | 183 | const key = i18nKeyOrEnText + '_' + pluralizer(count); |
| b69ab31 | | | 184 | return langs[lang]?.[key]; |
| b69ab31 | | | 185 | } |