mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-20 17:55:55 +00:00
Merge pull request #3315 from overleaf/jpa-i18n-transform-locales
[misc] i18n: transform locales GitOrigin-RevId: 2306682eab2b10a69c14520cb22338d8537397cd
This commit is contained in:
parent
120df0bfa2
commit
f2eb4893fe
3 changed files with 121 additions and 0 deletions
37
services/web/scripts/translations/insertHTMLFragments.js
Normal file
37
services/web/scripts/translations/insertHTMLFragments.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
This script will aid the process of inserting HTML fragments into all the
|
||||
locales.
|
||||
We are migrating from
|
||||
locale: 'PRE __key1__ POST'
|
||||
pug: translate(localeKey, { key1: '<b>VALUE</b>' })
|
||||
to
|
||||
locale: 'PRE <0>__key1__</0> POST'
|
||||
pug: translate(localeKey, { key1: 'VALUE' }, ['b'])
|
||||
|
||||
|
||||
MAPPING entries:
|
||||
localeKey: ['key1', 'key2']
|
||||
click_here_to_view_sl_in_lng: ['lngName']
|
||||
*/
|
||||
const MAPPING = {}
|
||||
|
||||
const { transformLocales } = require('./transformLocales')
|
||||
|
||||
function transformLocale(locale, components) {
|
||||
components.forEach((key, idx) => {
|
||||
const i18nKey = `__${key}__`
|
||||
const replacement = `<${idx}>${i18nKey}</${idx}>`
|
||||
if (!locale.includes(replacement)) {
|
||||
locale = locale.replace(new RegExp(i18nKey, 'g'), replacement)
|
||||
}
|
||||
})
|
||||
return locale
|
||||
}
|
||||
|
||||
function main() {
|
||||
transformLocales(MAPPING, transformLocale)
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main()
|
||||
}
|
35
services/web/scripts/translations/replaceLinkFragments.js
Normal file
35
services/web/scripts/translations/replaceLinkFragments.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
This script will aid the process of inserting HTML fragments into all the
|
||||
locales.
|
||||
We are migrating from
|
||||
locale: 'PRE __keyLinkOpen__INNER__keyLinkClose__ POST'
|
||||
pug: translate(localeKey, { keyLinkOpen: '<a ...>', keyLinkClose: '</a>' })
|
||||
to
|
||||
locale: 'PRE <0>INNER</0> POST'
|
||||
pug: translate(localeKey, {}, [{ name: 'a', attrs: { href: '...', ... }}])
|
||||
|
||||
|
||||
MAPPING entries:
|
||||
localeKey: ['keyLinkOpen', 'keyLinkClose']
|
||||
faq_pay_by_invoice_answer: ['payByInvoiceLinkOpen', 'payByInvoiceLinkClose']
|
||||
*/
|
||||
const MAPPING = {}
|
||||
|
||||
const { transformLocales } = require('./transformLocales')
|
||||
|
||||
function transformLocale(locale, [open, close]) {
|
||||
const i18nOpen = `__${open}__`
|
||||
const i18nClose = `__${close}__`
|
||||
if (locale.includes(i18nOpen)) {
|
||||
locale = locale.replace(i18nOpen, '<0>').replace(i18nClose, '</0>')
|
||||
}
|
||||
return locale
|
||||
}
|
||||
|
||||
function main() {
|
||||
transformLocales(MAPPING, transformLocale)
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
main()
|
||||
}
|
49
services/web/scripts/translations/transformLocales.js
Normal file
49
services/web/scripts/translations/transformLocales.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
const fs = require('fs')
|
||||
|
||||
const LANGUAGES = [
|
||||
'cs',
|
||||
'da',
|
||||
'de',
|
||||
'en',
|
||||
'es',
|
||||
'fi',
|
||||
'fr',
|
||||
'it',
|
||||
'ja',
|
||||
'ko',
|
||||
'nl',
|
||||
'no',
|
||||
'pl',
|
||||
'pt',
|
||||
'ru',
|
||||
'sv',
|
||||
'tr',
|
||||
'zh-CN'
|
||||
]
|
||||
const LOCALES = {}
|
||||
LANGUAGES.forEach(loadLocales)
|
||||
function loadLocales(language) {
|
||||
LOCALES[language] = require(`../../locales/${language}.json`)
|
||||
}
|
||||
|
||||
function transformLocales(mapping, transformLocale) {
|
||||
Object.entries(LOCALES).forEach(([language, translatedLocales]) => {
|
||||
Object.entries(mapping).forEach(([localeKey, spec]) => {
|
||||
const locale = translatedLocales[localeKey]
|
||||
if (!locale) {
|
||||
// This locale is not translated yet.
|
||||
return
|
||||
}
|
||||
translatedLocales[localeKey] = transformLocale(locale, spec)
|
||||
})
|
||||
|
||||
fs.writeFileSync(
|
||||
`${__dirname}/../../locales/${language}.json`,
|
||||
JSON.stringify(translatedLocales, null, 2) + '\n'
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
transformLocales
|
||||
}
|
Loading…
Add table
Reference in a new issue