Reorganize i18n structure and add support for multiple languages

This commit is contained in:
Sangrak Choi 2025-04-15 03:21:58 +09:00
parent ebfe6a9b17
commit 58923faccc
17 changed files with 133 additions and 23 deletions

3
i18n/ar/command.json Normal file
View file

@ -0,0 +1,3 @@
{
"CREATE_TODAY_NOTE": "إنشاء ملاحظة جديدة (بتاريخ اليوم)"
}

6
i18n/ar/notice.json Normal file
View file

@ -0,0 +1,6 @@
{
"FILE_EXISTS": "الملف {filename} موجود بالفعل.",
"FILE_CREATED": "تم إنشاء الملف {filename}.",
"FOLDER_NOT_FOUND": "تعذر العثور على مجلد الملف الحالي.",
"ERROR_CREATING_FILE": "خطأ في إنشاء الملف: {error}"
}

View file

@ -1,9 +0,0 @@
{
"COMMANDS": { "CREATE_TODAY_NOTE": "Create New Note (Today's Date)" },
"NOTICES": {
"FILE_EXISTS": "File {filename} already exists.",
"FILE_CREATED": "File {filename} has been created.",
"FOLDER_NOT_FOUND": "Could not find the current file's folder.",
"ERROR_CREATING_FILE": "Error creating file: {error}"
}
}

3
i18n/en/command.json Normal file
View file

@ -0,0 +1,3 @@
{
"CREATE_TODAY_NOTE": "Create New Note (Today's Date)"
}

6
i18n/en/notice.json Normal file
View file

@ -0,0 +1,6 @@
{
"FILE_EXISTS": "File {filename} already exists.",
"FILE_CREATED": "File {filename} has been created.",
"FOLDER_NOT_FOUND": "Could not find the current file's folder.",
"ERROR_CREATING_FILE": "Error creating file: {error}"
}

3
i18n/es-ES/command.json Normal file
View file

@ -0,0 +1,3 @@
{
"CREATE_TODAY_NOTE": "Crear Nueva Nota (Fecha de Hoy)"
}

6
i18n/es-ES/notice.json Normal file
View file

@ -0,0 +1,6 @@
{
"FILE_EXISTS": "El archivo {filename} ya existe.",
"FILE_CREATED": "El archivo {filename} ha sido creado.",
"FOLDER_NOT_FOUND": "No se pudo encontrar la carpeta del archivo actual.",
"ERROR_CREATING_FILE": "Error al crear el archivo: {error}"
}

3
i18n/ja/command.json Normal file
View file

@ -0,0 +1,3 @@
{
"CREATE_TODAY_NOTE": "新規ノート作成(今日の日付)"
}

6
i18n/ja/notice.json Normal file
View file

@ -0,0 +1,6 @@
{
"FILE_EXISTS": "ファイル {filename} はすでに存在します。",
"FILE_CREATED": "ファイル {filename} が作成されました。",
"FOLDER_NOT_FOUND": "現在のファイルのフォルダが見つかりませんでした。",
"ERROR_CREATING_FILE": "ファイル作成エラー: {error}"
}

View file

@ -1,9 +0,0 @@
{
"COMMANDS": { "CREATE_TODAY_NOTE": "새 노트 생성 (오늘 날짜)" },
"NOTICES": {
"FILE_EXISTS": "{filename} 파일이 이미 존재합니다.",
"FILE_CREATED": "{filename} 파일이 생성되었습니다.",
"FOLDER_NOT_FOUND": "현재 파일의 폴더를 찾을 수 없습니다.",
"ERROR_CREATING_FILE": "파일 생성 중 오류가 발생했습니다: {error}"
}
}

3
i18n/ko/command.json Normal file
View file

@ -0,0 +1,3 @@
{
"CREATE_TODAY_NOTE": "새 노트 생성 (오늘 날짜)"
}

6
i18n/ko/notice.json Normal file
View file

@ -0,0 +1,6 @@
{
"FILE_EXISTS": "파일 {filename}이(가) 이미 존재합니다.",
"FILE_CREATED": "파일 {filename}이(가) 생성되었습니다.",
"FOLDER_NOT_FOUND": "현재 파일의 폴더를 찾을 수 없습니다.",
"ERROR_CREATING_FILE": "파일 생성 오류: {error}"
}

3
i18n/zh-CN/command.json Normal file
View file

@ -0,0 +1,3 @@
{
"CREATE_TODAY_NOTE": "创建新笔记(今日日期)"
}

6
i18n/zh-CN/notice.json Normal file
View file

@ -0,0 +1,6 @@
{
"FILE_EXISTS": "文件 {filename} 已存在。",
"FILE_CREATED": "文件 {filename} 已创建。",
"FOLDER_NOT_FOUND": "无法找到当前文件的文件夹。",
"ERROR_CREATING_FILE": "创建文件时出错:{error}"
}

3
i18n/zh-TW/command.json Normal file
View file

@ -0,0 +1,3 @@
{
"CREATE_TODAY_NOTE": "建立新筆記(今日日期)"
}

6
i18n/zh-TW/notice.json Normal file
View file

@ -0,0 +1,6 @@
{
"FILE_EXISTS": "檔案 {filename} 已經存在。",
"FILE_CREATED": "檔案 {filename} 已建立。",
"FOLDER_NOT_FOUND": "找不到目前檔案的資料夾。",
"ERROR_CREATING_FILE": "建立檔案時發生錯誤:{error}"
}

75
main.ts
View file

@ -1,8 +1,72 @@
import { App, Editor, MarkdownView, Modal, Notice, Plugin, TFile, TAbstractFile } from 'obsidian';
import * as moment from 'moment';
// Import JSON files directly
import koTranslations from './i18n/ko.json';
import enTranslations from './i18n/en.json';
// Import translations
import arCommand from './i18n/ar/command.json';
import arNotice from './i18n/ar/notice.json';
import enCommand from './i18n/en/command.json';
import enNotice from './i18n/en/notice.json';
import esCommand from './i18n/es-ES/command.json';
import esNotice from './i18n/es-ES/notice.json';
import jaCommand from './i18n/ja/command.json';
import jaNotice from './i18n/ja/notice.json';
import koCommand from './i18n/ko/command.json';
import koNotice from './i18n/ko/notice.json';
import zhCNCommand from './i18n/zh-CN/command.json';
import zhCNNotice from './i18n/zh-CN/notice.json';
import zhTWCommand from './i18n/zh-TW/command.json';
import zhTWNotice from './i18n/zh-TW/notice.json';
type TranslationData = {
COMMANDS: typeof enCommand;
NOTICES: typeof enNotice;
};
type TranslationsMap = {
[key: string]: TranslationData;
};
// Language code mapping
const LANG_CODE_MAP: { [key: string]: string } = {
'ar': 'ar',
'en': 'en',
'es-ES': 'es',
'ja': 'ja',
'ko': 'ko',
'zh-CN': 'zh',
'zh-TW': 'zh-TW'
};
const translations: TranslationsMap = {
ar: {
COMMANDS: arCommand,
NOTICES: arNotice
},
en: {
COMMANDS: enCommand,
NOTICES: enNotice
},
es: {
COMMANDS: esCommand,
NOTICES: esNotice
},
ja: {
COMMANDS: jaCommand,
NOTICES: jaNotice
},
ko: {
COMMANDS: koCommand,
NOTICES: koNotice
},
zh: {
COMMANDS: zhCNCommand,
NOTICES: zhCNNotice
},
'zh-TW': {
COMMANDS: zhTWCommand,
NOTICES: zhTWNotice
}
};
interface Translations {
COMMANDS: {
@ -19,11 +83,12 @@ interface Translations {
export default class KarsPlugin extends Plugin {
getText(key: string, vars: Record<string, string> = {}): string {
// Get language setting from localStorage
const language = window.localStorage.getItem('language') || 'en';
const translations = language === 'ko' ? koTranslations : enTranslations;
const rawLanguage = window.localStorage.getItem('language') || 'en';
const language = LANG_CODE_MAP[rawLanguage] || rawLanguage;
const currentTranslations = translations[language] || translations.en;
const keys = key.split('.');
let value: any = translations;
let value: any = currentTranslations;
for (const k of keys) {
if (!value || !value[k]) return key;
value = value[k];