This repository has been archived on 2025-01-27. You can view files and clone it, but cannot push or open issues or pull requests.
puyoskey-firefish/packages/client/src/i18n.ts
naskya d096da02e6 🎉 First Commit
release: v20240729

Co-authored-by: Laura Hausmann <laura@hausmann.dev>
Co-authored-by: GitLab CI <project_7_bot_1bfaee5701aed20091a86249a967a6c1@noreply.firefish.dev>
Co-authored-by: Hosted Weblate <hosted@weblate.org>
Co-authored-by: Saamkhaih Kyakya <70475761+hiohlan@users.noreply.github.com>

See merge request firefish/firefish!11214
2024-08-01 00:03:39 +09:00

37 lines
998 B
TypeScript

import { markRaw } from "vue";
import { locale } from "@/config";
// biome-ignore lint/suspicious/noExplicitAny: temporary use any
class I18n<T extends Record<string, any>> {
public ts: T;
constructor(locale: T) {
this.ts = locale;
// #region BIND
this.t = this.t.bind(this);
// #endregion
}
// string にしているのは、ドット区切りでのパス指定を許可するため
// なるべくこのメソッド使うよりもlocale直接参照の方がvueのキャッシュ効いてパフォーマンスが良いかも
public t(key: string, args?: Record<string, string | number>): string {
try {
let str = key
.split(".")
.reduce((o, i) => o[i], this.ts) as unknown as string;
if (args) {
for (const [k, v] of Object.entries(args)) {
str = str.replaceAll(`{${k}}`, v.toString());
}
}
return str;
} catch (err) {
console.warn(`missing localization '${key}'`);
return key;
}
}
}
export const i18n = markRaw(new I18n(locale));