Internationalization Configuration

Configure multi-language support

Type Definition

interface I18nConfig {
  /**
   * Default language
   */
  defaultLanguage?: string;

  /**
   * Supported languages list
   */
  supportedLanguages?: string[];

  /**
   * Translation resources (will be merged with core translations)
   */
  resources?: Record<string, Record<string, unknown>>;
}

Default Values

OptionDefault
defaultLanguage'zh'
supportedLanguages['zh', 'en']

Basic Configuration

i18n: {
  defaultLanguage: 'zh',
  supportedLanguages: ['zh', 'en'],
  resources: {
    en: {
      app: {
        name: 'My App',
        home: 'Home',
      },
    },
    zh: {
      app: {
        name: '我的应用',
        home: '首页',
      },
    },
  },
},

Built-in Translations

The core provides the following built-in translation keys:

common namespace

KeyChineseEnglish
common.loading加载中...Loading...
common.error错误Error
common.confirm确认Confirm
common.cancel取消Cancel
common.save保存Save
common.delete删除Delete

settings namespace

KeyChineseEnglish
settings.title设置Settings
settings.general常规General
settings.about关于About
settings.language语言Language
settings.theme主题Theme
settings.theme.light浅色Light
settings.theme.dark深色Dark
settings.theme.system跟随系统System
settings.version版本Version
settings.checkUpdate检查更新Check for Updates

Using Translations

In Components

import { useTranslation } from 'react-i18next';

function MyComponent() {
  const { t } = useTranslation();

  return (
    <div>
      <h1>{t('app.name')}</h1>
      <p>{t('common.loading')}</p>
    </div>
  );
}

Translations with Parameters

resources: {
  en: {
    app: {
      welcome: 'Welcome, {{name}}!',
    },
  },
  zh: {
    app: {
      welcome: '欢迎,{{name}}!',
    },
  },
},
const { t } = useTranslation();
t('app.welcome', { name: 'John' }); // "Welcome, John!"

Pluralization

resources: {
  en: {
    app: {
      items: '{{count}} item',
      items_other: '{{count}} items',
    },
  },
  zh: {
    app: {
      items: '{{count}} 个项目',
    },
  },
},
t('app.items', { count: 1 }); // "1 item"
t('app.items', { count: 5 }); // "5 items"

Switching Languages

Use the built-in LanguageSwitcher component or customize:

import { changeLanguage, getCurrentLanguage } from '@linch-tech/desktop-core';

function LanguageSelect() {
  const handleChange = async (lang: string) => {
    await changeLanguage(lang);
  };

  return (
    <select
      value={getCurrentLanguage()}
      onChange={(e) => handleChange(e.target.value)}
    >
      <option value="zh">中文</option>
      <option value="en">English</option>
    </select>
  );
}

Adding New Languages

  1. Add language code to supportedLanguages
  2. Add corresponding translations in resources
i18n: {
  defaultLanguage: 'zh',
  supportedLanguages: ['zh', 'en', 'ja'],  // Add Japanese
  resources: {
    en: { ... },
    zh: { ... },
    ja: {
      app: {
        name: 'マイアプリ',
        home: 'ホーム',
      },
      settings: {
        title: '設定',
      },
    },
  },
},

Dynamic Translation Loading

For large translation resources, you can load dynamically:

import { addI18nResources } from '@linch-tech/desktop-core';

// Dynamically load translations for a language
async function loadLanguage(lang: string) {
  const resources = await import(`./locales/${lang}.json`);
  addI18nResources(lang, resources.default);
}

Complete Example

export const config = {
  i18n: {
    defaultLanguage: 'zh',
    supportedLanguages: ['zh', 'en'],
    resources: {
      en: {
        app: {
          name: 'My Desktop App',
          home: 'Home',
          dashboard: 'Dashboard',
          welcome: 'Welcome, {{name}}!',
          items: '{{count}} item',
          items_other: '{{count}} items',
        },
        errors: {
          notFound: 'Page not found',
          serverError: 'Server error',
        },
      },
      zh: {
        app: {
          name: '我的桌面应用',
          home: '首页',
          dashboard: '仪表盘',
          welcome: '欢迎,{{name}}!',
          items: '{{count}} 个项目',
        },
        errors: {
          notFound: '页面未找到',
          serverError: '服务器错误',
        },
      },
    },
  },
};