From 81edd0b5a954b680cee6c6e29db796b952fb7bdf Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Sat, 9 Sep 2023 00:34:56 +0200 Subject: [PATCH] feat(Translations): add list of locales that are supported by ArxLibertatis --- src/Translations.ts | 58 ++++++++++++++++++++++++++++++++++++--------- src/index.ts | 1 + 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/Translations.ts b/src/Translations.ts index ddaf303e..99ddea35 100644 --- a/src/Translations.ts +++ b/src/Translations.ts @@ -2,27 +2,63 @@ import fs from 'node:fs' import path from 'node:path' import { Settings } from '@src/Settings.js' +export type Locales = + | 'chinese' + | 'german' + | 'english' + | 'french' + | 'hungarian' + | 'italian' + | 'japanese' + | 'russian' + | 'spanish' + +const toArxLocale = (locale: Locales) => { + if (locale === 'german') { + return 'deutsch' + } + + if (locale === 'french') { + return 'francais' + } + + if (locale === 'italian') { + return 'italiano' + } + + return locale +} + export class Translations { - translations: Record> = {} + translations: Record>> = {} exportSourcesAndTargets(settings: Settings) { - const translations: Record> = {} + const translations: Partial>> = {} - Object.entries(this.translations).forEach(([key, valuesByLanguage]) => { - Object.entries(valuesByLanguage).forEach(([language, value]) => { - if (!translations[language]) { - translations[language] = {} + Object.entries(this.translations).forEach(([key, valuesByLocale]) => { + const localeValuePairs = Object.entries(valuesByLocale) as [Locales, string][] + localeValuePairs.forEach(([locale, value]) => { + const translation = translations[locale] + if (typeof translation === 'undefined') { + translations[locale] = { + [key]: value, + } + } else { + translation[key] = value } - translations[language][key] = value }) }) const results: Record = {} - Object.entries(translations).forEach(([language, dictionary]) => { + const languageDictionaryPairs = Object.entries(translations) as [Locales, Record][] + languageDictionaryPairs.forEach(([locale, dictionary]) => { const content = this.stringifyDictionary(dictionary) if (content.length) { - const filename = path.resolve(settings.outputDir, `localisation/xtext_${language}_002_arx-level-generator.ini`) + const filename = path.resolve( + settings.outputDir, + `localisation/xtext_${toArxLocale(locale)}_002_arx-level-generator.ini`, + ) results[filename] = content } }) @@ -41,7 +77,7 @@ export class Translations { .join('') } - add(translations: Record>) { + add(translations: Record>>) { this.translations = { ...this.translations, ...translations, @@ -51,7 +87,7 @@ export class Translations { async addFromFile(filename: string, settings: Settings) { try { const rawIn = await fs.promises.readFile(path.resolve(settings.assetsDir, filename), 'utf-8') - const translations = JSON.parse(rawIn) as Record> + const translations = JSON.parse(rawIn) as Record>> this.add(translations) } catch (error) { console.error(error) diff --git a/src/index.ts b/src/index.ts index e64e5ca9..ec21fde8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,6 +36,7 @@ export type { PathPoint } from '@src/Path.js' export type { TransparencyType } from '@src/Polygon.js' export type { MeshImportProps } from '@src/Polygons.js' export type { ScriptHandler } from '@src/Script.js' +export type { Locales } from '@src/Translations.js' export type { ZonePoint } from '@src/Zone.js' export type { ArxVertexWithColor, OriginalLevel, TextureOrMaterial } from '@src/types.js'