Localization refactoring:
* the front-end now use global constants, based on the translation key * build-client.js use the ESBuild "define" directive to replace these globals at compile time, by the english value * build:client must now be called after build:languages * moving the loadLoc and loc backend functions in a separate lib
This commit is contained in:
43
server/lib/loc.ts
Normal file
43
server/lib/loc.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { resolve } from 'path'
|
||||
import { existsSync, promises as fsPromises } from 'fs'
|
||||
|
||||
const locContent: Map<string, string> = new Map<string, string>()
|
||||
|
||||
/**
|
||||
* Currently, the Peertube plugin system assumes that settings strings
|
||||
* are localized in english, and will be translated on front-end.
|
||||
* This system make it hard to have complex strings (with html, newlines, ...).
|
||||
* See https://github.com/Chocobozzz/PeerTube/issues/4523
|
||||
*
|
||||
* Waiting for a better solution, we implemented a custom solution:
|
||||
* - We are using keys to identify strings
|
||||
* - the `loc` function gets the english segment for the key
|
||||
* - the build-languages.js script builds all needed files.
|
||||
* @param key The key to translate
|
||||
*/
|
||||
function loc (key: string): string {
|
||||
return locContent.get(key) ?? key
|
||||
}
|
||||
|
||||
async function loadLoc (): Promise<void> {
|
||||
const filePath = resolve(__dirname, '..', '..', 'languages', 'en.reference.json')
|
||||
if (!existsSync(filePath)) {
|
||||
throw new Error(`File ${filePath} missing, can't load plugin loc strings`)
|
||||
}
|
||||
const content = await fsPromises.readFile(filePath, 'utf8')
|
||||
const json = JSON.parse(content ?? '{}')
|
||||
if (typeof json !== 'object') {
|
||||
throw new Error(`File ${filePath} invalid, can't load plugin loc strings`)
|
||||
}
|
||||
for (const k in json) {
|
||||
const v = json[k]
|
||||
if (typeof v === 'string') {
|
||||
locContent.set(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
loc,
|
||||
loadLoc
|
||||
}
|
Reference in New Issue
Block a user