From fc11fcb08665abcc5b7e11bd14e1445070176a66 Mon Sep 17 00:00:00 2001 From: JayBridge <12310903@mail.sustech.edu.cn> Date: Sat, 12 Apr 2025 10:10:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=B3=BB=E7=BB=9F=E8=AF=AD=E8=A8=80=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E9=BB=98=E8=AE=A4=E8=AF=AD=E8=A8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/i18n/index.ts | 31 ++++++++++++++++++++++--------- src/main.ts | 22 +++++++++++----------- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/i18n/index.ts b/src/i18n/index.ts index 2b2a46d..04851c5 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -9,19 +9,32 @@ export const LOCALE = { export type Locale = keyof typeof LOCALE; export class I18n { - private locale: Locale = 'zh-cn'; + private locale: Locale = 'en'; - constructor(locale?: Locale) { - if (locale && locale in LOCALE) { - this.locale = locale; - } + constructor(locale?: string) { + this.setLocale(locale || 'en'); } - setLocale(locale: Locale) { - if (locale in LOCALE) { - this.locale = locale; + setLocale(locale: string) { + // Map Obsidian language codes to our locale keys + let mappedLocale: Locale = 'en'; + + // Convert to lowercase for case-insensitive matching + const lowerLocale = locale.toLowerCase(); + + // Map language codes to our locale keys + if (lowerLocale.startsWith('zh')) { + mappedLocale = 'zh-cn'; + } else if (lowerLocale.startsWith('en')) { + mappedLocale = 'en'; + } + + // Set the locale if it exists in our translations + if (mappedLocale in LOCALE) { + this.locale = mappedLocale; } else { - console.warn(`Locale ${locale} not found, using current locale`); + console.warn(`Locale ${locale} not found, using English as fallback`); + this.locale = 'en'; } } diff --git a/src/main.ts b/src/main.ts index 86d17c5..52fb4c1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,15 +1,13 @@ -import { App, Editor, MarkdownView, Modal, Notice, Plugin, Setting, WorkspaceLeaf } from 'obsidian'; +import { Plugin, WorkspaceLeaf } from 'obsidian'; import { CSVView, VIEW_TYPE_CSV } from "./view"; -import { i18n, Locale } from './i18n'; +import { i18n } from './i18n'; interface CSVPluginSettings { csvSettings: string; - locale: Locale; // 添加语言设置 } const DEFAULT_SETTINGS: CSVPluginSettings = { - csvSettings: 'default', - locale: 'en' // 默认使用英文 + csvSettings: 'default' } export default class CSVPlugin extends Plugin { @@ -17,19 +15,21 @@ export default class CSVPlugin extends Plugin { async onload() { await this.loadSettings(); - - // 设置语言 - i18n.setLocale(this.settings.locale); - + + // 设置语言 - 使用系统语言 + // 注意:如果升级到 Obsidian 1.8.0 或更高版本,可以使用 this.app.getLanguage() + const systemLang = navigator.language || "en"; + i18n.setLocale(systemLang); + // 注册CSV视图类型 this.registerView( VIEW_TYPE_CSV, (leaf: WorkspaceLeaf) => new CSVView(leaf) ); - + // 将.csv文件扩展名与视图类型绑定 this.registerExtensions(["csv"], VIEW_TYPE_CSV); - + } onunload() {