Files
bw-mini-app/src/i18n/manager.ts
iHeyTang 157f055acb feat(i18n): implement internationalization support across the application
- Added a new i18n hook for managing language settings and translations.
- Introduced a LanguageSwitcher component for dynamic language switching.
- Updated existing components and pages to utilize the i18n system for text translations.
- Enhanced app configuration to support runtime language updates for tabBar and navigation titles.
- Added language resources for English, Japanese, Korean, and simplified Chinese.
- Improved loading and error messages to be language-aware.
- Refactored existing code to ensure compatibility with the new i18n structure.
2025-09-28 11:16:43 +08:00

111 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* i18n管理器
* 处理应用级别的国际化设置包括tabBar、导航栏标题等
*/
import Taro from '@tarojs/taro';
import { Language } from './index';
import { t } from './utils';
export class I18nManager {
private static instance: I18nManager;
public static getInstance(): I18nManager {
if (!I18nManager.instance) {
I18nManager.instance = new I18nManager();
}
return I18nManager.instance;
}
/**
* 更新TabBar文本
*/
public async updateTabBarTexts(): Promise<void> {
try {
// 更新第一个tab游乐场
await Taro.setTabBarItem({
index: 0,
text: t('navigation.playground'),
});
// 更新第二个tab我的
await Taro.setTabBarItem({
index: 1,
text: t('navigation.mine'),
});
} catch (error) {
console.warn('Failed to update tabBar texts:', error);
}
}
/**
* 更新导航栏标题
* @param pageKey 页面键名
*/
public async updateNavigationBarTitle(pageKey?: string): Promise<void> {
try {
let title = '';
// 根据页面设置不同的标题
switch (pageKey) {
case 'home':
title = t('home.title');
break;
case 'friends-photo':
title = t('friendsPhoto.title');
break;
case 'history':
title = t('navigation.history');
break;
default:
title = t('home.title'); // 默认标题
}
await Taro.setNavigationBarTitle({ title });
} catch (error) {
console.warn('Failed to update navigation bar title:', error);
}
}
/**
* 初始化应用国际化设置
*/
public async initializeApp(): Promise<void> {
try {
// 更新TabBar文本
await this.updateTabBarTexts();
// 更新当前页面标题
const pages = Taro.getCurrentPages();
const currentPage = pages[pages.length - 1];
if (currentPage) {
const route = currentPage.route;
let pageKey = '';
if (route?.includes('home')) {
pageKey = 'home';
} else if (route?.includes('friends-photo')) {
pageKey = 'friends-photo';
} else if (route?.includes('history')) {
pageKey = 'history';
}
await this.updateNavigationBarTitle(pageKey);
}
} catch (error) {
console.error('Failed to initialize app i18n:', error);
}
}
/**
* 语言切换时的处理
*/
public async onLanguageChange(): Promise<void> {
await this.initializeApp();
}
}
// 导出单例实例
export const i18nManager = I18nManager.getInstance();