forked from Shinonome/dots-hyprland
Add i18n
This commit is contained in:
Executable
+68
@@ -0,0 +1,68 @@
|
||||
const Gio = imports.gi.Gio;
|
||||
const GLib = imports.gi.GLib;
|
||||
|
||||
function getLanguageCode() {
|
||||
let langEnv = GLib.getenv('LANG') || GLib.getenv('LANGUAGE') || 'C.UTF-8'; // Assume the default value contains a dot
|
||||
let langCode = langEnv.split('.')[0]; // Split the string and get the first part
|
||||
return langCode;
|
||||
}
|
||||
|
||||
const translations = {};
|
||||
let currentLanguage = getLanguageCode();
|
||||
|
||||
// Load language file
|
||||
async function loadLanguage(lang) {
|
||||
if (!translations[lang]) {
|
||||
try {
|
||||
let filePath = `~/.config/ags/i18n/locales/${lang}.json`;
|
||||
filePath = filePath.replace(/^~/, GLib.get_home_dir());
|
||||
|
||||
let file = Gio.File.new_for_path(filePath);
|
||||
let [success, contents] = file.load_contents(null);
|
||||
if (success) {
|
||||
let decoder = new TextDecoder('utf-8');
|
||||
let jsonString = decoder.decode(contents);
|
||||
translations[lang] = JSON.parse(jsonString);
|
||||
} else {
|
||||
throw new Error(`Unable to load file: ${filePath}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to load language file, language code: ${lang}:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
currentLanguage = lang;
|
||||
}
|
||||
|
||||
// Set the current language
|
||||
function setLanguage(lang) {
|
||||
try {
|
||||
loadLanguage(lang);
|
||||
} catch (error) {
|
||||
console.error(`Failed to set language, language code: ${lang}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Get translation, if no corresponding value, return the key
|
||||
function setString(key) {
|
||||
if (!translations[currentLanguage]?.[key]) {
|
||||
console.log('无:' + key);
|
||||
}
|
||||
return translations[currentLanguage]?.[key] || key;
|
||||
}
|
||||
|
||||
// Initialize default language
|
||||
function init() {
|
||||
try {
|
||||
loadLanguage(currentLanguage);
|
||||
console.log("初始化完成");
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to initialize default language:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export { setString, init, setLanguage };
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"No media": "无媒体活动",
|
||||
"Options": "选项",
|
||||
"Dark Mode": "深色模式",
|
||||
"Close": "关闭",
|
||||
"Keybinds": "按键绑定",
|
||||
"Periodic table": "周期表",
|
||||
"Cheat sheet": "备忘单",
|
||||
"notifications": "通知",
|
||||
"Clear": "清除"
|
||||
}
|
||||
@@ -5,6 +5,10 @@ import Mpris from 'resource:///com/github/Aylur/ags/service/mpris.js';
|
||||
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
|
||||
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
|
||||
const { exec, execAsync } = Utils;
|
||||
import { init as i18n_init, setLanguage as i18n_setLanguage } from './i18n/i18n.js'
|
||||
//init i18n, Load language file
|
||||
i18n_init()
|
||||
// i18n_setLanguage('zh_CN') //Manual configuration
|
||||
|
||||
Gtk.IconTheme.get_default().append_search_path(`${App.configDir}/assets/icons`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user