Skip to content

Commit 53301fc

Browse files
committed
refactor translation utils
1 parent fc22ff4 commit 53301fc

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/shared/languageSpecificTranslation.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
import bcp47 from 'bcp47'
2+
import translations from '../../lib/language_specific_translation/translations.js'
3+
4+
const translationMap = new Map(
5+
Object.entries(translations.translation).map(([key, value]) => [
6+
key,
7+
new Map(Object.entries(value)),
8+
])
9+
)
10+
111
/**
212
* Checks if the document language is specified and not English
313
*
@@ -9,13 +19,11 @@ export function isLangSpecifiedAndNotEnglish(language) {
919
!!language && !(bcp47.parse(language)?.langtag.language.language === 'en')
1020
)
1121
}
12-
import bcp47 from 'bcp47'
13-
import translations from '../../lib/language_specific_translation/translations.js'
1422

1523
/**
1624
* test whether exactly one item in document notes exists that has the given title.
1725
* and the given category.
18-
* @param {({} & { category?: string | undefined; title?: string | undefined; } & Record<string, unknown>)[]} notes
26+
* @param {Array<{ category?: string | undefined; title?: string | undefined; }>} notes
1927
* @param {string} titleToFind
2028
* @param {string} category
2129
* @returns {boolean} True if the language is valid, false otherwise
@@ -36,19 +44,14 @@ export function containsOneNoteWithTitleAndCategory(
3644
* Get the language specific translation of the given i18nKey
3745
* @param {{ document: { lang?: string; }; }} doc
3846
* @param {string} i18nKey
39-
* @return {string | undefined}
4047
*/
4148
export function getTranslationInDocumentLang(doc, i18nKey) {
4249
if (!doc.document.lang) {
4350
return undefined
4451
}
4552
const language = bcp47.parse(doc.document.lang)?.langtag.language.language
46-
47-
/** @type {Record<string, Record <string,string>>}*/
48-
const translationByLang = translations.translation
49-
if (!language || !translationByLang[language]) {
53+
if (!language) {
5054
return undefined
51-
} else {
52-
return translationByLang[language][i18nKey]
5355
}
56+
return translationMap.get(language)?.get(i18nKey)
5457
}

0 commit comments

Comments
 (0)