fix: 修复语言设置逻辑,确保使用系统语言作为默认语言

This commit is contained in:
JayBridge 2025-04-12 10:10:45 +08:00
parent 463b0034d8
commit fc11fcb086
2 changed files with 33 additions and 20 deletions

View file

@ -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';
}
}

View file

@ -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() {