Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "augmented-steam",
"scripts": {
"test": "node test/language-locale.test.mjs",
"build": "node scripts/build.mjs",
"update-locales": "node scripts/locales.mjs download",
"compile-locales": "node scripts/locales.mjs compile",
Expand Down
2 changes: 1 addition & 1 deletion src/js/Content/Features/Store/App/FPurchaseDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class FPurchaseDate extends Feature<CApp> {
date: datetime.toLocaleString({
dateStyle: "medium"
}, {
locale: this.context.language?.code ?? undefined
locale: this.context.language?.locale ?? undefined
})
})})`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/Content/Features/Store/App/FReleaseCountdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class FReleaseCountdown extends Feature<CApp> {
dateStyle: "medium",
timeStyle: "short"
}, {
locale: this.context.language?.code ?? undefined
locale: this.context.language?.locale ?? undefined
})
const str = ` (${date})`;

Expand Down
13 changes: 12 additions & 1 deletion src/js/Core/Localization/Language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ export default class Language {
"vietnamese": "vi",
};

public static readonly localeMap: Record<string, string> = {
...Language.map,
"thai": "th-TH-u-ca-gregory",
"ukrainian": "uk",
};

private readonly _name: string;
private readonly _code: string|null;
private readonly _locale: string|null;

constructor(language: string) {
this._name = language;
this._code = Language.map[language] ?? null;
this._locale = Language.localeMap[language] ?? this._code;
// TODO should we throw when there is unsupported language?
}

Expand All @@ -49,8 +57,11 @@ export default class Language {
return this._code;
}

get locale(): string|null {
return this._locale;
}

isOneOf(...languages: string[]) {
return languages.includes(this._name);
}
}

40 changes: 40 additions & 0 deletions test/language-locale.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import {pathToFileURL} from "node:url";
import {build} from "esbuild";
import {DateTime} from "luxon";

async function importLanguage() {
const result = await build({
entryPoints: ["src/js/Core/Localization/Language.ts"],
bundle: true,
format: "esm",
platform: "node",
write: false
});
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "as-language-"));
const modulePath = path.join(tempDir, "Language.mjs");
await fs.writeFile(modulePath, result.outputFiles[0].text);
return import(pathToFileURL(modulePath));
}

const {default: Language} = await importLanguage();

const ukrainian = new Language("ukrainian");
assert.equal(ukrainian.code, "ua");
assert.equal(ukrainian.locale, "uk");

const thai = new Language("thai");
assert.equal(thai.code, "th");
assert.equal(thai.locale, "th-TH-u-ca-gregory");

const formattedThaiDate = DateTime.fromObject({
year: 2026,
month: 1,
day: 23
}).toLocaleString({dateStyle: "medium"}, {locale: thai.locale});

assert.match(formattedThaiDate, /2026/);
assert.doesNotMatch(formattedThaiDate, /2569/);