react-native-i18njs 0.0.1-beta → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +221 -26
- package/dist/index.d.mts +132 -0
- package/dist/index.d.ts +132 -0
- package/dist/index.js +536 -0
- package/dist/index.mjs +497 -0
- package/package.json +48 -6
- package/README.en.md +0 -36
- package/index.js +0 -1
package/README.md
CHANGED
|
@@ -1,39 +1,234 @@
|
|
|
1
1
|
# react-native-i18njs
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/react-native-i18njs)
|
|
4
|
+
[](https://www.npmjs.com/package/react-native-i18njs)
|
|
5
|
+
[](https://reactnative.dev)
|
|
6
|
+
[](https://www.typescriptlang.org)
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
软件架构说明
|
|
8
|
+
一个轻量级、类型安全、零心智负担的 React Native 国际化解决方案。
|
|
10
9
|
|
|
10
|
+
专为 React Native 设计,集成了最佳实践,解决了常见的国际化痛点:繁琐的配置、类型缺失、复杂的 API 以及系统语言跟随问题。
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
## ✨ 特性
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
- � **零配置启动**:内置智能默认值,安装即用。
|
|
15
|
+
- 🛡️ **极致类型安全**:完全 TypeScript 编写,提供从 Key 到插值参数的完整类型推导。
|
|
16
|
+
- 📱 **自动跟随系统**:基于 `react-native-localize`,自动检测并响应设备语言变更。
|
|
17
|
+
- ⚡ **高性能**:基于 `i18n-js` 核心,轻量高效,无多余运行时开销。
|
|
18
|
+
- 🔌 **灵活 API**:同时支持 Hook (`useI18n`)、高阶组件 (`withTranslation`) 和全局函数 (`t`)。
|
|
19
|
+
- 📝 **富文本支持**:`Trans` 组件轻松处理嵌套样式和组件插值。
|
|
20
|
+
- 🌍 **格式化内置**:开箱即用的数字、货币、日期格式化支持。
|
|
17
21
|
|
|
18
|
-
|
|
22
|
+
## 📦 安装
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
```bash
|
|
25
|
+
npm install react-native-i18njs
|
|
26
|
+
# 或者
|
|
27
|
+
yarn add react-native-i18njs
|
|
28
|
+
```
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
> **注意**:本库已内置 `i18n-js` 和 `react-native-localize` 的稳定版本,无需手动安装 peer dependencies。
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
2. 新建 Feat_xxx 分支
|
|
28
|
-
3. 提交代码
|
|
29
|
-
4. 新建 Pull Request
|
|
32
|
+
## 🚀 快速开始
|
|
30
33
|
|
|
34
|
+
### 1. 定义翻译资源
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
建议在单独的文件中管理翻译资源,例如 `src/locales/index.ts`:
|
|
33
37
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
```ts
|
|
39
|
+
// src/locales/index.ts
|
|
40
|
+
export const translations = {
|
|
41
|
+
en: {
|
|
42
|
+
welcome: 'Welcome',
|
|
43
|
+
hello: 'Hello, %{name}!',
|
|
44
|
+
},
|
|
45
|
+
zh: {
|
|
46
|
+
welcome: '欢迎',
|
|
47
|
+
hello: '你好,%{name}!',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// 导出类型以获得类型提示
|
|
52
|
+
export type Translations = typeof translations.en;
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 2. 初始化
|
|
56
|
+
|
|
57
|
+
在你的 App 入口文件(如 `App.tsx`)中初始化:
|
|
58
|
+
|
|
59
|
+
```tsx
|
|
60
|
+
import React from 'react';
|
|
61
|
+
import { initI18n, I18nProvider } from 'react-native-i18njs';
|
|
62
|
+
import { translations } from './src/locales';
|
|
63
|
+
import Home from './src/Home';
|
|
64
|
+
|
|
65
|
+
// 初始化配置
|
|
66
|
+
initI18n(translations, {
|
|
67
|
+
defaultLocale: 'en',
|
|
68
|
+
enableFallback: true, // 找不到翻译时回退到默认语言
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
export default function App() {
|
|
72
|
+
return (
|
|
73
|
+
// 使用 Provider 以支持语言切换时的自动重渲染
|
|
74
|
+
<I18nProvider>
|
|
75
|
+
<Home />
|
|
76
|
+
</I18nProvider>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 3. 在组件中使用
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
// src/Home.tsx
|
|
85
|
+
import React from 'react';
|
|
86
|
+
import { Text, Button, View } from 'react-native';
|
|
87
|
+
import { useI18n } from 'react-native-i18njs';
|
|
88
|
+
import { Translations } from './locales';
|
|
89
|
+
|
|
90
|
+
export default function Home() {
|
|
91
|
+
// 传入泛型 Translations 以获得 key 的自动补全和类型检查
|
|
92
|
+
const { t, locale, setLocale } = useI18n<Translations>();
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<View>
|
|
96
|
+
<Text>{t('welcome')}</Text>
|
|
97
|
+
{/* 这里的 name 参数会有类型提示 */}
|
|
98
|
+
<Text>{t('hello', { name: 'Trae' })}</Text>
|
|
99
|
+
|
|
100
|
+
<Text>当前语言: {locale}</Text>
|
|
101
|
+
|
|
102
|
+
<Button title="Switch to English" onPress={() => setLocale('en')} />
|
|
103
|
+
<Button title="切换到中文" onPress={() => setLocale('zh')} />
|
|
104
|
+
</View>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## 📖 核心功能详解
|
|
110
|
+
|
|
111
|
+
### 1. 非组件环境使用(Global API)
|
|
112
|
+
|
|
113
|
+
在 Redux、Axios 拦截器、工具函数等非组件环境中,你可以直接使用全局导出的 API。
|
|
114
|
+
|
|
115
|
+
#### 基础用法
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { t, getLocale, setLocale } from 'react-native-i18njs';
|
|
119
|
+
|
|
120
|
+
// 获取当前语言
|
|
121
|
+
const current = getLocale();
|
|
122
|
+
|
|
123
|
+
// 切换语言
|
|
124
|
+
setLocale('zh');
|
|
125
|
+
|
|
126
|
+
// 直接翻译
|
|
127
|
+
const message = t('errors.network_timeout');
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
#### 进阶:监听语言变化
|
|
131
|
+
|
|
132
|
+
如果你需要在组件外监听语言变更(例如同步更新全局状态),可以使用默认导出的实例:
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import i18n from 'react-native-i18njs';
|
|
136
|
+
|
|
137
|
+
// 订阅语言变更
|
|
138
|
+
const unsubscribe = i18n.subscribe((locale) => {
|
|
139
|
+
console.log('Language changed to:', locale);
|
|
140
|
+
// 更新 API 默认 Header 或其他全局状态
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// 取消订阅
|
|
144
|
+
// unsubscribe();
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### 实战示例:Axios 拦截器
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import axios from 'axios';
|
|
151
|
+
import { getLocale } from 'react-native-i18njs';
|
|
152
|
+
|
|
153
|
+
axios.interceptors.request.use((config) => {
|
|
154
|
+
// 动态获取当前语言,确保每次请求都携带最新的语言标识
|
|
155
|
+
config.headers['Accept-Language'] = getLocale();
|
|
156
|
+
return config;
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### 2. 富文本翻译 (`Trans` 组件)
|
|
161
|
+
|
|
162
|
+
当翻译内容中包含样式或组件时,使用 `Trans` 组件:
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import { Trans } from 'react-native-i18njs';
|
|
166
|
+
import { Text } from 'react-native';
|
|
167
|
+
|
|
168
|
+
// 翻译资源:
|
|
169
|
+
// zh: { agreement: '我同意 <link>服务条款</link>' }
|
|
170
|
+
|
|
171
|
+
<Trans
|
|
172
|
+
i18nKey="agreement"
|
|
173
|
+
components={{
|
|
174
|
+
link: <Text style={{ color: 'blue' }} onPress={openTerms} />,
|
|
175
|
+
}}
|
|
176
|
+
/>
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### 3. 动态加载翻译
|
|
180
|
+
|
|
181
|
+
适用于大型应用的分包加载场景:
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
import { loadTranslations } from 'react-native-i18njs';
|
|
185
|
+
|
|
186
|
+
// 异步加载法语包
|
|
187
|
+
async function loadFrench() {
|
|
188
|
+
const fr = await import('./locales/fr');
|
|
189
|
+
loadTranslations({ fr: fr.default });
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### 4. 格式化工具
|
|
194
|
+
|
|
195
|
+
利用 `Intl` 标准进行格式化:
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
const { formatNumber, formatCurrency, formatDate } = useI18n();
|
|
199
|
+
|
|
200
|
+
// 数字
|
|
201
|
+
formatNumber(1234.56); // "1,234.56"
|
|
202
|
+
|
|
203
|
+
// 货币
|
|
204
|
+
formatCurrency(99.99, 'USD'); // "$99.99"
|
|
205
|
+
|
|
206
|
+
// 日期
|
|
207
|
+
formatDate(new Date(), { dateStyle: 'full' }); // "Tuesday, October 10, 2023"
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## ⚙️ 配置选项 (I18nOptions)
|
|
211
|
+
|
|
212
|
+
`initI18n` 接受的第二个参数对象:
|
|
213
|
+
|
|
214
|
+
| 属性 | 类型 | 默认值 | 说明 |
|
|
215
|
+
|------|------|--------|------|
|
|
216
|
+
| `defaultLocale` | `string` | `'en'` | 默认语言 |
|
|
217
|
+
| `enableFallback` | `boolean` | `true` | 是否启用回退机制 |
|
|
218
|
+
| `followSystem` | `boolean` | `true` | 是否初始化时自动跟随系统语言 |
|
|
219
|
+
| `fallbackLocales` | `string[]` \| `func` | - | 自定义回退链 |
|
|
220
|
+
| `missingBehavior` | `'key'` \| `'empty'` \| `'throw'` | `'key'` | 缺失翻译时的行为 |
|
|
221
|
+
| `onMissingKey` | `function` | - | 缺失 key 的回调 |
|
|
222
|
+
| `onLocaleChange` | `function` | - | 语言变更回调 |
|
|
223
|
+
|
|
224
|
+
## 🧩 常见问题
|
|
225
|
+
|
|
226
|
+
### TypeScript 类型提示不工作?
|
|
227
|
+
确保你在使用 `useI18n<MyTranslations>()` 时传入了你的翻译类型定义。
|
|
228
|
+
|
|
229
|
+
### 安卓上语言检测不准确?
|
|
230
|
+
请确保你的 `android/app/src/main/res` 目录下有对应的语言资源文件夹(如 `values-zh`),React Native 有时依赖这些原生配置来正确识别系统语言。
|
|
231
|
+
|
|
232
|
+
## 📄 License
|
|
233
|
+
|
|
234
|
+
ISC
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Scope, TranslateOptions } from 'i18n-js';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
3
|
+
import { TextProps } from 'react-native';
|
|
4
|
+
|
|
5
|
+
interface I18nOptions {
|
|
6
|
+
defaultLocale?: string;
|
|
7
|
+
enableFallback?: boolean;
|
|
8
|
+
onLocaleChange?: (locale: string) => void;
|
|
9
|
+
fallbackLocales?: string[] | ((locale: string) => string[]);
|
|
10
|
+
/**
|
|
11
|
+
* 是否跟随系统语言变化自动切换。
|
|
12
|
+
* - 默认 true:初始化会使用系统语言;App 回到前台时会再次同步系统语言(直到用户手动 setLocale)。
|
|
13
|
+
* - 设为 false:始终使用 defaultLocale / 手动 setLocale,不再自动同步系统语言。
|
|
14
|
+
*/
|
|
15
|
+
followSystem?: boolean;
|
|
16
|
+
missingBehavior?: 'key' | 'empty' | 'throw';
|
|
17
|
+
onMissingKey?: (key: string, locale: string) => void;
|
|
18
|
+
}
|
|
19
|
+
type Translations = Record<string, any>;
|
|
20
|
+
type Listener = (locale: string, change?: {
|
|
21
|
+
type: 'locale' | 'translations';
|
|
22
|
+
version: number;
|
|
23
|
+
}) => void;
|
|
24
|
+
/**
|
|
25
|
+
* i18n 引擎接口,定义了任何翻译引擎必须实现的方法
|
|
26
|
+
*/
|
|
27
|
+
interface I18nEngine {
|
|
28
|
+
init(translations: Translations, options?: I18nOptions): void;
|
|
29
|
+
loadTranslations(translations: Translations): void;
|
|
30
|
+
setLocale(locale: string): void;
|
|
31
|
+
getLocale(): string;
|
|
32
|
+
t(scope: Scope, options?: TranslateOptions): string;
|
|
33
|
+
formatNumber(n: number, options?: Intl.NumberFormatOptions): string;
|
|
34
|
+
formatCurrency(n: number, currency: string, options?: Intl.NumberFormatOptions): string;
|
|
35
|
+
formatDate(date: Date | number, options?: Intl.DateTimeFormatOptions): string;
|
|
36
|
+
subscribe(listener: Listener): () => void;
|
|
37
|
+
isRTL(): boolean;
|
|
38
|
+
ready(): Promise<void>;
|
|
39
|
+
isReady(): boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 默认的 i18n-js 引擎实现
|
|
43
|
+
*/
|
|
44
|
+
declare class DefaultI18nEngine implements I18nEngine {
|
|
45
|
+
private i18n;
|
|
46
|
+
private listeners;
|
|
47
|
+
private onLocaleChange?;
|
|
48
|
+
private fallbackLocales?;
|
|
49
|
+
private missingBehavior;
|
|
50
|
+
private onMissingKey?;
|
|
51
|
+
private enableFallback;
|
|
52
|
+
private followSystem;
|
|
53
|
+
private localeSource;
|
|
54
|
+
private initialized;
|
|
55
|
+
private version;
|
|
56
|
+
private readyPromise;
|
|
57
|
+
private resolveReady?;
|
|
58
|
+
constructor();
|
|
59
|
+
init(translations: Translations, options?: I18nOptions): void;
|
|
60
|
+
loadTranslations(translations: Translations): void;
|
|
61
|
+
updateLocale(): void;
|
|
62
|
+
setLocale(locale: string): void;
|
|
63
|
+
getLocale(): string;
|
|
64
|
+
t(scope: Scope, options?: TranslateOptions): string;
|
|
65
|
+
formatNumber(n: number, options?: Intl.NumberFormatOptions): string;
|
|
66
|
+
formatCurrency(n: number, currency: string, options?: Intl.NumberFormatOptions): string;
|
|
67
|
+
formatDate(date: Date | number, options?: Intl.DateTimeFormatOptions): string;
|
|
68
|
+
subscribe(listener: Listener): () => void;
|
|
69
|
+
isRTL(): boolean;
|
|
70
|
+
ready(): Promise<void>;
|
|
71
|
+
isReady(): boolean;
|
|
72
|
+
private handleRTL;
|
|
73
|
+
private notifyListeners;
|
|
74
|
+
private setLocaleFromSystem;
|
|
75
|
+
private applyLocale;
|
|
76
|
+
private translateAtLocale;
|
|
77
|
+
private getLocaleChain;
|
|
78
|
+
private hasTranslation;
|
|
79
|
+
private normalizeTranslateResult;
|
|
80
|
+
private normalizeLocaleTag;
|
|
81
|
+
private normalizeTranslations;
|
|
82
|
+
private isRTLLocale;
|
|
83
|
+
}
|
|
84
|
+
declare const i18nService: DefaultI18nEngine;
|
|
85
|
+
|
|
86
|
+
interface I18nContextType {
|
|
87
|
+
locale: string;
|
|
88
|
+
setLocale: (locale: string) => void;
|
|
89
|
+
t: (scope: Scope, options?: TranslateOptions) => string;
|
|
90
|
+
formatNumber: (n: number, options?: Intl.NumberFormatOptions) => string;
|
|
91
|
+
formatCurrency: (n: number, currency: string, options?: Intl.NumberFormatOptions) => string;
|
|
92
|
+
formatDate: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string;
|
|
93
|
+
}
|
|
94
|
+
declare const I18nContext: React.Context<I18nContextType>;
|
|
95
|
+
interface I18nProviderProps {
|
|
96
|
+
children: ReactNode;
|
|
97
|
+
readyGate?: boolean;
|
|
98
|
+
fallback?: ReactNode;
|
|
99
|
+
}
|
|
100
|
+
declare const I18nProvider: React.FC<I18nProviderProps>;
|
|
101
|
+
declare function withI18n<P extends object>(Component: React.ComponentType<P & I18nContextType>): React.FC<Omit<P, keyof I18nContextType>>;
|
|
102
|
+
|
|
103
|
+
type Path<T> = T extends object ? {
|
|
104
|
+
[K in keyof T]: `${Exclude<K, symbol>}${"" | `.${Path<T[K]>}`}`;
|
|
105
|
+
}[keyof T] : never;
|
|
106
|
+
|
|
107
|
+
type TFunction<T> = (scope: T extends Translations ? Path<T> : string, options?: TranslateOptions) => string;
|
|
108
|
+
declare function useI18n<T = Translations>(): {
|
|
109
|
+
t: TFunction<T>;
|
|
110
|
+
locale: string;
|
|
111
|
+
setLocale: (locale: string) => void;
|
|
112
|
+
formatNumber: (n: number, options?: Intl.NumberFormatOptions) => string;
|
|
113
|
+
formatCurrency: (n: number, currency: string, options?: Intl.NumberFormatOptions) => string;
|
|
114
|
+
formatDate: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
interface TransProps extends TextProps {
|
|
118
|
+
i18nKey: string;
|
|
119
|
+
values?: Record<string, any>;
|
|
120
|
+
components?: Record<string, ReactNode>;
|
|
121
|
+
}
|
|
122
|
+
declare const Trans: React.FC<TransProps>;
|
|
123
|
+
|
|
124
|
+
declare const initI18n: (translations: Translations, options?: I18nOptions) => void;
|
|
125
|
+
declare const loadTranslations: (translations: Translations) => void;
|
|
126
|
+
declare const setLocale: (locale: string) => void;
|
|
127
|
+
declare const getLocale: () => string;
|
|
128
|
+
declare const t: (scope: Scope, options?: TranslateOptions) => string;
|
|
129
|
+
declare const readyI18n: () => Promise<void>;
|
|
130
|
+
declare const isI18nReady: () => boolean;
|
|
131
|
+
|
|
132
|
+
export { I18nContext, type I18nContextType, type I18nOptions, I18nProvider, type I18nProviderProps, type Path, Trans, type Translations, i18nService as default, getLocale, initI18n, isI18nReady, loadTranslations, readyI18n, setLocale, t, useI18n, withI18n };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { Scope, TranslateOptions } from 'i18n-js';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
3
|
+
import { TextProps } from 'react-native';
|
|
4
|
+
|
|
5
|
+
interface I18nOptions {
|
|
6
|
+
defaultLocale?: string;
|
|
7
|
+
enableFallback?: boolean;
|
|
8
|
+
onLocaleChange?: (locale: string) => void;
|
|
9
|
+
fallbackLocales?: string[] | ((locale: string) => string[]);
|
|
10
|
+
/**
|
|
11
|
+
* 是否跟随系统语言变化自动切换。
|
|
12
|
+
* - 默认 true:初始化会使用系统语言;App 回到前台时会再次同步系统语言(直到用户手动 setLocale)。
|
|
13
|
+
* - 设为 false:始终使用 defaultLocale / 手动 setLocale,不再自动同步系统语言。
|
|
14
|
+
*/
|
|
15
|
+
followSystem?: boolean;
|
|
16
|
+
missingBehavior?: 'key' | 'empty' | 'throw';
|
|
17
|
+
onMissingKey?: (key: string, locale: string) => void;
|
|
18
|
+
}
|
|
19
|
+
type Translations = Record<string, any>;
|
|
20
|
+
type Listener = (locale: string, change?: {
|
|
21
|
+
type: 'locale' | 'translations';
|
|
22
|
+
version: number;
|
|
23
|
+
}) => void;
|
|
24
|
+
/**
|
|
25
|
+
* i18n 引擎接口,定义了任何翻译引擎必须实现的方法
|
|
26
|
+
*/
|
|
27
|
+
interface I18nEngine {
|
|
28
|
+
init(translations: Translations, options?: I18nOptions): void;
|
|
29
|
+
loadTranslations(translations: Translations): void;
|
|
30
|
+
setLocale(locale: string): void;
|
|
31
|
+
getLocale(): string;
|
|
32
|
+
t(scope: Scope, options?: TranslateOptions): string;
|
|
33
|
+
formatNumber(n: number, options?: Intl.NumberFormatOptions): string;
|
|
34
|
+
formatCurrency(n: number, currency: string, options?: Intl.NumberFormatOptions): string;
|
|
35
|
+
formatDate(date: Date | number, options?: Intl.DateTimeFormatOptions): string;
|
|
36
|
+
subscribe(listener: Listener): () => void;
|
|
37
|
+
isRTL(): boolean;
|
|
38
|
+
ready(): Promise<void>;
|
|
39
|
+
isReady(): boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* 默认的 i18n-js 引擎实现
|
|
43
|
+
*/
|
|
44
|
+
declare class DefaultI18nEngine implements I18nEngine {
|
|
45
|
+
private i18n;
|
|
46
|
+
private listeners;
|
|
47
|
+
private onLocaleChange?;
|
|
48
|
+
private fallbackLocales?;
|
|
49
|
+
private missingBehavior;
|
|
50
|
+
private onMissingKey?;
|
|
51
|
+
private enableFallback;
|
|
52
|
+
private followSystem;
|
|
53
|
+
private localeSource;
|
|
54
|
+
private initialized;
|
|
55
|
+
private version;
|
|
56
|
+
private readyPromise;
|
|
57
|
+
private resolveReady?;
|
|
58
|
+
constructor();
|
|
59
|
+
init(translations: Translations, options?: I18nOptions): void;
|
|
60
|
+
loadTranslations(translations: Translations): void;
|
|
61
|
+
updateLocale(): void;
|
|
62
|
+
setLocale(locale: string): void;
|
|
63
|
+
getLocale(): string;
|
|
64
|
+
t(scope: Scope, options?: TranslateOptions): string;
|
|
65
|
+
formatNumber(n: number, options?: Intl.NumberFormatOptions): string;
|
|
66
|
+
formatCurrency(n: number, currency: string, options?: Intl.NumberFormatOptions): string;
|
|
67
|
+
formatDate(date: Date | number, options?: Intl.DateTimeFormatOptions): string;
|
|
68
|
+
subscribe(listener: Listener): () => void;
|
|
69
|
+
isRTL(): boolean;
|
|
70
|
+
ready(): Promise<void>;
|
|
71
|
+
isReady(): boolean;
|
|
72
|
+
private handleRTL;
|
|
73
|
+
private notifyListeners;
|
|
74
|
+
private setLocaleFromSystem;
|
|
75
|
+
private applyLocale;
|
|
76
|
+
private translateAtLocale;
|
|
77
|
+
private getLocaleChain;
|
|
78
|
+
private hasTranslation;
|
|
79
|
+
private normalizeTranslateResult;
|
|
80
|
+
private normalizeLocaleTag;
|
|
81
|
+
private normalizeTranslations;
|
|
82
|
+
private isRTLLocale;
|
|
83
|
+
}
|
|
84
|
+
declare const i18nService: DefaultI18nEngine;
|
|
85
|
+
|
|
86
|
+
interface I18nContextType {
|
|
87
|
+
locale: string;
|
|
88
|
+
setLocale: (locale: string) => void;
|
|
89
|
+
t: (scope: Scope, options?: TranslateOptions) => string;
|
|
90
|
+
formatNumber: (n: number, options?: Intl.NumberFormatOptions) => string;
|
|
91
|
+
formatCurrency: (n: number, currency: string, options?: Intl.NumberFormatOptions) => string;
|
|
92
|
+
formatDate: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string;
|
|
93
|
+
}
|
|
94
|
+
declare const I18nContext: React.Context<I18nContextType>;
|
|
95
|
+
interface I18nProviderProps {
|
|
96
|
+
children: ReactNode;
|
|
97
|
+
readyGate?: boolean;
|
|
98
|
+
fallback?: ReactNode;
|
|
99
|
+
}
|
|
100
|
+
declare const I18nProvider: React.FC<I18nProviderProps>;
|
|
101
|
+
declare function withI18n<P extends object>(Component: React.ComponentType<P & I18nContextType>): React.FC<Omit<P, keyof I18nContextType>>;
|
|
102
|
+
|
|
103
|
+
type Path<T> = T extends object ? {
|
|
104
|
+
[K in keyof T]: `${Exclude<K, symbol>}${"" | `.${Path<T[K]>}`}`;
|
|
105
|
+
}[keyof T] : never;
|
|
106
|
+
|
|
107
|
+
type TFunction<T> = (scope: T extends Translations ? Path<T> : string, options?: TranslateOptions) => string;
|
|
108
|
+
declare function useI18n<T = Translations>(): {
|
|
109
|
+
t: TFunction<T>;
|
|
110
|
+
locale: string;
|
|
111
|
+
setLocale: (locale: string) => void;
|
|
112
|
+
formatNumber: (n: number, options?: Intl.NumberFormatOptions) => string;
|
|
113
|
+
formatCurrency: (n: number, currency: string, options?: Intl.NumberFormatOptions) => string;
|
|
114
|
+
formatDate: (date: Date | number, options?: Intl.DateTimeFormatOptions) => string;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
interface TransProps extends TextProps {
|
|
118
|
+
i18nKey: string;
|
|
119
|
+
values?: Record<string, any>;
|
|
120
|
+
components?: Record<string, ReactNode>;
|
|
121
|
+
}
|
|
122
|
+
declare const Trans: React.FC<TransProps>;
|
|
123
|
+
|
|
124
|
+
declare const initI18n: (translations: Translations, options?: I18nOptions) => void;
|
|
125
|
+
declare const loadTranslations: (translations: Translations) => void;
|
|
126
|
+
declare const setLocale: (locale: string) => void;
|
|
127
|
+
declare const getLocale: () => string;
|
|
128
|
+
declare const t: (scope: Scope, options?: TranslateOptions) => string;
|
|
129
|
+
declare const readyI18n: () => Promise<void>;
|
|
130
|
+
declare const isI18nReady: () => boolean;
|
|
131
|
+
|
|
132
|
+
export { I18nContext, type I18nContextType, type I18nOptions, I18nProvider, type I18nProviderProps, type Path, Trans, type Translations, i18nService as default, getLocale, initI18n, isI18nReady, loadTranslations, readyI18n, setLocale, t, useI18n, withI18n };
|