✨ ags(i18n): finally use i18n system!
This commit is contained in:
+18
-11
@@ -1,33 +1,32 @@
|
||||
//TODO use I18n system >.<
|
||||
import { Binding, GLib } from "astal";
|
||||
|
||||
import en_US from "./lang/en_US";
|
||||
import pt_BR from "./lang/pt_BR";
|
||||
|
||||
import { GLib } from "astal";
|
||||
|
||||
export type i18nStruct = Record<string, string|object|Binding<string| undefined>>;
|
||||
|
||||
const i18nKeys = {
|
||||
"en_US": en_US,
|
||||
"pt_BR": pt_BR
|
||||
};
|
||||
|
||||
const languages: Array<string> = Object.keys(i18nKeys);
|
||||
|
||||
const defaultLanguage: string = languages[0];
|
||||
let language: string = getSystemLanguage();
|
||||
|
||||
export function getSystemLanguage(): string {
|
||||
const sysLanguage: (string|null|undefined) = GLib.getenv("LANG") || GLib.getenv("LANGUAGE");
|
||||
|
||||
if(!sysLanguage) {
|
||||
console.log(`[WARNING] Couldn't get system language, fallback to default ${defaultLanguage}`);
|
||||
console.log(`[WARNING] Couldn't get system language, fallback to default ${languages[0]}`);
|
||||
console.log("[TIP] Please set the LANG or LANGUAGE environment variable");
|
||||
|
||||
return "en_US";
|
||||
return languages[0];
|
||||
}
|
||||
|
||||
return sysLanguage.split('.')[0];
|
||||
}
|
||||
|
||||
export function setLanguage(lang: keyof typeof i18nKeys): string {
|
||||
export function setLanguage(lang: string): string {
|
||||
languages.map((cur: string) => {
|
||||
if(cur === lang) {
|
||||
language = lang;
|
||||
@@ -42,12 +41,20 @@ export function setLanguage(lang: keyof typeof i18nKeys): string {
|
||||
|
||||
export function tr(key: string): string {
|
||||
let result = i18nKeys[language as keyof typeof i18nKeys],
|
||||
defResult = i18nKeys[defaultLanguage as keyof typeof i18nKeys];
|
||||
defResult = i18nKeys[languages[0] as keyof typeof i18nKeys];
|
||||
|
||||
for(const keyString in key.split('.')) {
|
||||
for(const keyString of key.split('.')) {
|
||||
result = result[keyString as keyof typeof result] as never;
|
||||
defResult = defResult[keyString as keyof typeof defResult] as never;
|
||||
}
|
||||
|
||||
return (result as never) || (defResult as never) || "couldn't find i18n key";
|
||||
return (typeof result == "string") ?
|
||||
result
|
||||
: ((typeof defResult == "string") ?
|
||||
defResult
|
||||
: "not found / is not of type \"string\"");
|
||||
}
|
||||
|
||||
export function trGet() {
|
||||
return i18nKeys[getSystemLanguage() as keyof typeof i18nKeys];
|
||||
}
|
||||
|
||||
+33
-5
@@ -1,8 +1,36 @@
|
||||
import { i18nStruct } from "../intl";
|
||||
|
||||
export default {
|
||||
"language": "English (United States)",
|
||||
"bar": {
|
||||
"logo": {
|
||||
"tooltip": "Applications"
|
||||
language: "English (United States)",
|
||||
bar: {
|
||||
apps: {
|
||||
tooltip: "Applications"
|
||||
}
|
||||
},
|
||||
control_center: {
|
||||
tiles: {
|
||||
more: "More",
|
||||
network: {
|
||||
network: "Network",
|
||||
connected: "Connected",
|
||||
disconnected: "Disconnected",
|
||||
unknown: "Unknown",
|
||||
connecting: "Connecting",
|
||||
wireless: "Wireless",
|
||||
wired: "Wired"
|
||||
},
|
||||
recording: {
|
||||
title: "Screen Recording",
|
||||
disabled_description: "Start recording",
|
||||
enabled_description: "Stop recording",
|
||||
}
|
||||
}
|
||||
},
|
||||
ask_popup: {
|
||||
title: "Question",
|
||||
options: {
|
||||
cancel: "Cancel",
|
||||
accept: "Ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
} as i18nStruct;
|
||||
|
||||
+34
-5
@@ -1,8 +1,37 @@
|
||||
import { i18nStruct } from "../intl";
|
||||
|
||||
export default {
|
||||
"language": "Português (Brasil)",
|
||||
"bar": {
|
||||
"logo": {
|
||||
"tooltip": "Aplicativos"
|
||||
language: "Português (Brasil)",
|
||||
bar: {
|
||||
apps: {
|
||||
tooltip: "Aplicativos"
|
||||
}
|
||||
},
|
||||
control_center: {
|
||||
tiles: {
|
||||
more: "Mais",
|
||||
|
||||
network: {
|
||||
network: "Rede",
|
||||
connected: "Conectado",
|
||||
disconnected: "Desconectado",
|
||||
unknown: "Desconhecido",
|
||||
connecting: "Conectando",
|
||||
wireless: "Wireless",
|
||||
wired: "Cabeado"
|
||||
},
|
||||
recording: {
|
||||
title: "Gravação de Tela",
|
||||
disabled_description: "Iniciar gravação",
|
||||
enabled_description: "Parar gravação",
|
||||
}
|
||||
}
|
||||
},
|
||||
ask_popup: {
|
||||
title: "Pergunta",
|
||||
options: {
|
||||
cancel: "Cancelar",
|
||||
accept: "Ok"
|
||||
}
|
||||
}
|
||||
}
|
||||
} as i18nStruct;
|
||||
|
||||
Reference in New Issue
Block a user