|
| 1 | +模板版本:v0.2.2 |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <h1 align="center"> <code>react-native-localization</code> </h1> |
| 5 | +</p> |
| 6 | + |
| 7 | +<p align="center"> |
| 8 | + <a href="https://github.com/stefalda/ReactNativeLocalization"> |
| 9 | + <img src="https://img.shields.io/badge/platforms-android%20|%20ios%20|%20harmony%20-lightgrey.svg" alt="Supported platforms" /> |
| 10 | + </a> |
| 11 | + <a href="https://github.com/stefalda/ReactNativeLocalization/blob/master/LICENSE"> |
| 12 | + <img src="https://img.shields.io/badge/license-MIT-green.svg" alt="License" /> |
| 13 | + </a> |
| 14 | +</p> |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +> [!TIP] [Github 地址](https://github.com/react-native-oh-library/ReactNativeLocalization) |
| 20 | +
|
| 21 | + |
| 22 | +## 安装与使用 |
| 23 | + |
| 24 | +请到三方库的 Releases 发布地址查看配套的版本信息:[@react-native-oh-tpl/react-native-localization Releases](https://github.com/react-native-oh-library/ReactNativeLocalization/releases),并下载适用版本的 tgz 包。 |
| 25 | + |
| 26 | +进入到工程目录并输入以下命令: |
| 27 | + |
| 28 | +> [!TIP] # 处替换为 tgz 包的路径 |
| 29 | +
|
| 30 | +<!-- tabs:start --> |
| 31 | + |
| 32 | +#### **npm** |
| 33 | + |
| 34 | +```bash |
| 35 | +npm install @react-native-oh-tpl/react-native-localization@file:# |
| 36 | +``` |
| 37 | + |
| 38 | +#### **yarn** |
| 39 | + |
| 40 | +```bash |
| 41 | +yarn add @react-native-oh-tpl/react-native-localization@file:# |
| 42 | +``` |
| 43 | + |
| 44 | +<!-- tabs:end --> |
| 45 | + |
| 46 | +下面的代码展示了这个库的基本使用场景: |
| 47 | + |
| 48 | +> [!WARNING] 使用时 import 的库名不变。 |
| 49 | +
|
| 50 | +```js |
| 51 | +import React, { useState } from 'react'; |
| 52 | +import { View, Text, Button, StyleSheet } from 'react-native'; |
| 53 | +import Localize from 'react-native-localization'; |
| 54 | + |
| 55 | +// 定义本地化内容 |
| 56 | +const strings = new Localize({ |
| 57 | + en: {//英语 |
| 58 | + welcome: 'Welcome', |
| 59 | + question: 'I\'d like some {0} and {1}, or just {0}', |
| 60 | + bread: 'bread', |
| 61 | + butter: 'butter', |
| 62 | + greeting: 'Hello, {0}! ', |
| 63 | + currentlanguage: 'Current Language', |
| 64 | + availableLanguages: 'Available Languages', |
| 65 | + interfaceLanguage: 'The System Language' |
| 66 | + }, |
| 67 | + fr: {//法语 |
| 68 | + welcome: 'Bienvenue', |
| 69 | + question: 'Je voudrais un peu de {0} et {1}, ou juste {0}', |
| 70 | + bread: 'pain', |
| 71 | + butter: 'beurre', |
| 72 | + greeting: 'Bonjour, {0}!', |
| 73 | + currentlanguage: 'Langue actuelle', |
| 74 | + availableLanguages: 'Langues disponibles', |
| 75 | + interfaceLanguage: 'Langue du système' |
| 76 | + }, |
| 77 | + bo: {//藏语 |
| 78 | + welcome: 'བསྐུལ་མཁན།', |
| 79 | + question: 'ང་ལུས་འདི་ལས། {0} དང། {1} ཡང་ཡིན། གང་ཡིན་ནི། {0}', |
| 80 | + bread: 'བཀྲུངས', |
| 81 | + butter: 'བརྡེན', |
| 82 | + greeting: 'བཀའ་བདག་ {0}!', |
| 83 | + currentlanguage: 'ད་དུས་ལག་འཁྱེར།', |
| 84 | + availableLanguages: 'ད་དུས་ལག་འཁྱེར་སྒྲིགས།', |
| 85 | + interfaceLanguage: 'རྩམ་གཞི་སྒྲིག་ལེན་དེ་རྒྱལ་སྤོད་' |
| 86 | + }, |
| 87 | + zh: {//中文 |
| 88 | + welcome: '欢迎', |
| 89 | + question: '我想要一些{0}和{1},或者只要{0}', |
| 90 | + bread: '面包', |
| 91 | + butter: '黄油', |
| 92 | + greeting: '你好, {0}!', |
| 93 | + currentlanguage: '当前语言', |
| 94 | + availableLanguages: '当前可用语言列表', |
| 95 | + interfaceLanguage: '当前系统语言' |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +export function LocalizationDemo() { |
| 100 | + // getLanguage API |
| 101 | + const [language, setLanguage] = useState(strings.getLanguage()); |
| 102 | + const changeLanguage = (lang: string) => { |
| 103 | + // setLanguage API |
| 104 | + strings.setLanguage(lang); |
| 105 | + setLanguage(strings.getLanguage()); |
| 106 | + }; |
| 107 | + |
| 108 | + return ( |
| 109 | + <View style={styles.screen}> |
| 110 | + <View style={{ height: 180, justifyContent: 'space-between', marginBottom: 30 }}> |
| 111 | + <Button |
| 112 | + color="#144400" |
| 113 | + title="切换成中文" |
| 114 | + onPress={() => changeLanguage('zh')} |
| 115 | + /> |
| 116 | + <Button |
| 117 | + color="#841584" |
| 118 | + title="切换成法语" |
| 119 | + onPress={() => changeLanguage('fr')} |
| 120 | + /> |
| 121 | + <Button |
| 122 | + color="#645555" |
| 123 | + title="切换成英语" |
| 124 | + onPress={() => changeLanguage('en')} |
| 125 | + /> |
| 126 | + <Button |
| 127 | + color="#241595" |
| 128 | + title="切换成藏语" |
| 129 | + onPress={() => changeLanguage('bo')} |
| 130 | + /> |
| 131 | + </View> |
| 132 | + |
| 133 | + <Text style={styles.text}> |
| 134 | + {strings.welcome} |
| 135 | + </Text> |
| 136 | + <Text style={styles.text}> |
| 137 | + {/* formatString API */} |
| 138 | + {strings.formatString(strings.question, strings.bread, strings.butter)} |
| 139 | + </Text> |
| 140 | + <Text style={styles.text}> |
| 141 | + {strings.currentlanguage}: {language} |
| 142 | + </Text> |
| 143 | + {/* getAvailableLanguages API */} |
| 144 | + <Text style={styles.text}> |
| 145 | + {strings.availableLanguages}: {strings.getAvailableLanguages().join(', ')} |
| 146 | + </Text> |
| 147 | + {/* getInterfaceLanguage API */} |
| 148 | + <Text style={styles.text}> |
| 149 | + {strings.interfaceLanguage}: {strings.getInterfaceLanguage()} |
| 150 | + </Text> |
| 151 | + </View> |
| 152 | + ); |
| 153 | +} |
| 154 | + |
| 155 | +const styles = StyleSheet.create({ |
| 156 | + text: { |
| 157 | + fontSize: 18, |
| 158 | + marginBottom: 10, |
| 159 | + color: 'white' |
| 160 | + }, |
| 161 | + |
| 162 | + screen: { |
| 163 | + flex: 1, |
| 164 | + padding: 4, |
| 165 | + paddingTop: 10, |
| 166 | + backgroundColor: 'black', |
| 167 | + marginHorizontal: 4, |
| 168 | + marginVertical: 8, |
| 169 | + paddingHorizontal: 8, |
| 170 | + }, |
| 171 | +}); |
| 172 | +``` |
| 173 | +## 使用 Codegen |
| 174 | + |
| 175 | +本库已经适配了 `Codegen` ,在使用前需要主动执行生成三方库桥接代码,详细请参考[ Codegen 使用文档](/zh-cn/codegen.md)。 |
| 176 | + |
| 177 | +## Link |
| 178 | + |
| 179 | +目前鸿蒙暂不支持 AutoLink,所以 Link 步骤需要手动配置。 |
| 180 | + |
| 181 | +首先需要使用 DevEco Studio 打开项目里的鸿蒙工程 `harmony` |
| 182 | + |
| 183 | +### 1.在工程根目录的 `oh-package.json5` 添加 overrides字段 |
| 184 | + |
| 185 | +```json |
| 186 | +{ |
| 187 | + ... |
| 188 | + "overrides": { |
| 189 | + "@rnoh/react-native-openharmony" : "file:./react_native_openharmony" |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +### 2.引入原生端代码 |
| 195 | + |
| 196 | +目前有两种方法: |
| 197 | + |
| 198 | +1. 通过 har 包引入(在 IDE 完善相关功能后该方法会被遗弃,目前首选此方法); |
| 199 | +2. 直接链接源码。 |
| 200 | + |
| 201 | +方法一:通过 har 包引入(推荐) |
| 202 | + |
| 203 | +> [!TIP] har 包位于三方库安装路径的 `harmony` 文件夹下。 |
| 204 | +
|
| 205 | +打开 `entry/oh-package.json5`,添加以下依赖 |
| 206 | + |
| 207 | +```json |
| 208 | +"dependencies": { |
| 209 | + "@rnoh/react-native-openharmony": "file:../react_native_openharmony", |
| 210 | + "@react-native-oh-tpl/react-native-localization": "file:../../node_modules/@react-native-oh-tpl/react-native-localization/harmony/react_localization.har" |
| 211 | + } |
| 212 | +``` |
| 213 | + |
| 214 | +点击右上角的 `sync` 按钮 |
| 215 | + |
| 216 | +或者在终端执行: |
| 217 | + |
| 218 | +```bash |
| 219 | +cd entry |
| 220 | +ohpm install |
| 221 | +``` |
| 222 | +方法二:直接链接源码 |
| 223 | + |
| 224 | +> [!TIP] 如需使用直接链接源码,请参考[直接链接源码说明](/zh-cn/link-source-code.md) |
| 225 | +
|
| 226 | + |
| 227 | +### 3.在 ArkTs 侧引入 RNReactLocalizationPackage |
| 228 | + |
| 229 | +打开 `entry/src/main/ets/RNPackagesFactory.ts`,添加: |
| 230 | + |
| 231 | +```diff |
| 232 | + ... |
| 233 | ++ import { RNReactLocalizationPackage } from '@react-native-oh-tpl/react-native-localization'; |
| 234 | + |
| 235 | +export function createRNPackages(ctx: RNPackageContext): RNPackage[] { |
| 236 | + return [ |
| 237 | + new SamplePackage(ctx), |
| 238 | ++ new RNReactLocalizationPackage(ctx), |
| 239 | + ]; |
| 240 | +} |
| 241 | +``` |
| 242 | + |
| 243 | +### 4.运行 |
| 244 | + |
| 245 | +点击右上角的 `sync` 按钮 |
| 246 | + |
| 247 | +或者在终端执行: |
| 248 | + |
| 249 | +```bash |
| 250 | +cd entry |
| 251 | +ohpm install |
| 252 | +``` |
| 253 | + |
| 254 | +然后编译、运行即可。 |
| 255 | + |
| 256 | +## 约束与限制 |
| 257 | + |
| 258 | +### 兼容性 |
| 259 | + |
| 260 | +要使用此库,需要使用正确的 React-Native 和 RNOH 版本。另外,还需要使用配套的 DevEco Studio 和 手机 ROM。 |
| 261 | + |
| 262 | +请到三方库相应的 Releases 发布地址查看 Release 配套的版本信息:[@react-native-oh-tpl/react-native-localization Releases](https://github.com/react-native-oh-library/ReactNativeLocalization/releases) |
| 263 | + |
| 264 | + |
| 265 | + |
| 266 | +## 静态方法 |
| 267 | + |
| 268 | +> [!TIP] "Platform"列表示该属性在原三方库上支持的平台。 |
| 269 | +
|
| 270 | +> [!TIP] "HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。 |
| 271 | +
|
| 272 | +| Name | Description | Type | Required | Platform | HarmonyOS Support | |
| 273 | +| ---- | :---------- | ---- | :------: | :------: | :----------------: | |
| 274 | +| setLanguage(languageCode) | force manually a particular language | void | yes | iOS/Android | yes | |
| 275 | +| getLanguage() | get the current displayed language | string | yes | iOS/Android | yes | |
| 276 | +| getInterfaceLanguage() | get the current device interface language | string | yes | iOS/Android | yes | |
| 277 | +| formatString() | format the passed string replacing its placeholders with the other arguments strings | string | yes | iOS/Android | yes | |
| 278 | +| getAvailableLanguages() | get an array of the languages passed in the constructor | string[] | yes | iOS/Android | yes | |
| 279 | +| getString(key: string, language?: string) | character information based on the key value | string | yes | iOS/Android | yes | |
| 280 | +| setContent(props: any) | replace the NamedLocalization object without reinstantiating the object | void | yes | iOS/Android | yes | |
| 281 | + |
| 282 | +## 遗留问题 |
| 283 | + |
| 284 | +## 其他 |
| 285 | + |
| 286 | +## 开源协议 |
| 287 | + |
| 288 | +本项目基于 [The MIT License (MIT)](https://github.com/stefalda/ReactNativeLocalization/blob/master/LICENSE) ,请自由地享受和参与开源。 |
0 commit comments