-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Internationalization Implementation
Welcome to the LobeChat Internationalization Implementation Guide. This document will guide you through understanding the internationalization mechanism of LobeChat, including file structure and how to add new languages. LobeChat uses i18next
and lobe-i18n
as the internationalization solution, aiming to provide users with seamless multilingual support.
- Internationalization Overview
- File Structure
- Core Implementation Logic
- Adding Support for New Languages
- Resources and Further Reading
Internationalization (i18n for short) is the process of enabling an application to adapt to different languages and regions. In LobeChat, we support multiple languages and achieve dynamic language switching and content localization through the i18next
library. Our goal is to provide a localized experience for global users.
In the LobeChat project, internationalization-related files are organized as follows:
-
src/locales/default
: Contains translation files for the default development language (Chinese), which we use as Chinese. -
locales
: Contains folders for all supported languages, with each language folder containing the respective translation files generated by lobe-i18n.
In the directory structure of src/locales
, the default
folder contains the original translation files (Chinese), while each other language folder contains JSON translation files for the respective language. The files in each language folder correspond to the TypeScript files in the default
folder, ensuring consistency in the structure of translation files across languages.
src/locales
├── create.ts
├── default
│ ├── chat.ts
│ ├── common.ts
│ ├── error.ts
│ ├── index.ts
│ ├── market.ts
│ ├── migration.ts
│ ├── plugin.ts
│ ├── setting.ts
│ ├── tool.ts
│ └── welcome.ts
└── resources.ts
The file structure generated by lobe-i18n is as follows:
locales
├── ar
│ ├── chat.json
│ ├── common.json
│ ├── error.json
│ └── ... (other translation files)
├── de-DE
│ ├── chat.json
│ ├── common.json
│ ├── error.json
│ └── ... (other translation files)
├── en-US
├── ... (other language directories)
├── zh-CN
└── zh-TW
The internationalization core implementation logic of LobeChat is as follows:
- Initialize and configure using the
i18next
library. - Automatically detect the user's language preference using
i18next-browser-languagedetector
. - Dynamically load translation resources using
i18next-resources-to-backend
. - Set the direction of the HTML document (LTR or RTL) based on the user's language preference.
Here is a simplified pseudo code example to illustrate the core implementation logic of internationalization in LobeChat:
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import resourcesToBackend from 'i18next-resources-to-backend';
import { isRtlLang } from 'rtl-detect';
// Create i18n instance and configure
const createI18nInstance = (lang) => {
const i18nInstance = i18n
.use(LanguageDetector) // Use language detection
.use(
resourcesToBackend((language, namespace) => {
// Dynamically load translation resources for the corresponding language
return import(`path/to/locales/${language}/${namespace}.json`);
}),
);
// Listen for language change events and dynamically set document direction
i18nInstance.on('languageChanged', (language) => {
const direction = isRtlLang(language) ? 'rtl' : 'ltr';
document.documentElement.dir = direction; // Set HTML document direction
});
// Initialize i18n instance
i18nInstance.init({
// Relevant configurations
});
return i18nInstance;
};
In this example, we demonstrate how to use i18next
and related plugins to initialize internationalization settings. We dynamically import translation resources and respond to language change events to adjust the text direction of the page. This process provides LobeChat with flexible multilingual support capabilities.
We have already supported a variety of languages globally through the following efforts:
- ✨ feat: adding Arabic Language Support #1049
- 🌐 style: Add Vietnamese files and add the vi-VN option in the General Settings #860
- 🌐 style: support it-IT nl-NL and pl-PL locales #759
- 🌐 feat(locale): Add fr-FR (#637) #645
- 🌐 Add russian localy #137
To add support for new languages, please refer to the detailed steps in the New Locale Addition Guide.
By following this guide, you can better understand and participate in the internationalization work of LobeChat, providing a seamless multilingual experience for global users.
This is the 🤯 / 🤖 Lobe Chat wiki. Wiki Home
- Architecture Design | 架构设计
- Code Style and Contribution Guidelines | 代码风格与贡献指南
- Complete Guide to LobeChat Feature Development | LobeChat 功能开发完全指南
- Conversation API Implementation Logic | 会话 API 实现逻辑
- Directory Structure | 目录架构
- Environment Setup Guide | 环境设置指南
- How to Develop a New Feature | 如何开发一个新功能:前端实现
- New Authentication Provider Guide | 新身份验证方式开发指南
- Resources and References | 资源与参考
- Technical Development Getting Started Guide | 技术开发上手指南
- Testing Guide | 测试指南