From 02cd6d02103e42fe25b8d3682c8ce0c81fc9432c Mon Sep 17 00:00:00 2001 From: xiagengwen Date: Tue, 24 Sep 2024 14:42:43 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=84=E4=BB=B6=E6=B7=BB=E5=8A=A0=E8=8B=B1?= =?UTF-8?q?=E6=96=87README=E5=92=8Cdemo=E5=9B=BD=E9=99=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiagengwen --- README-EN.md | 75 ---------------- README.md | 90 +++++++++---------- README_zh.md | 81 +++++++++++++++++ entry/src/main/ets/pages/index.ets | 18 ++-- .../main/resources/base/element/string.json | 28 ++++++ .../main/resources/en_US/element/string.json | 28 ++++++ .../main/resources/zh_CN/element/string.json | 28 ++++++ 7 files changed, 221 insertions(+), 127 deletions(-) delete mode 100644 README-EN.md create mode 100644 README_zh.md diff --git a/README-EN.md b/README-EN.md deleted file mode 100644 index 0b7f27e..0000000 --- a/README-EN.md +++ /dev/null @@ -1,75 +0,0 @@ -# pinyin4js - -## Introduction - -pinyin4js is an open-source JavaScript library that has been adapted to OpenHarmony for converting Chinese characters to Pinyin. It has the following features: - -* **Zero dependency** - -* **Flexible import and packaging of dictionaries** - - The imported dictionaries can be adjusted as needed. For details, see **src/dict**. All resource scheduling is encapsulated by **PinyinResource**, and the resources can be packaged after being modified. - -* **Accurate and comprehensive dictionaries** - - Among the 20903 Chinese characters in the Unicode range of 4E00-9FA5 and 3007, pinyin4js can convert all of the characters except 46 heterogenous characters (which do not have standard pinyin). - -* **Fast conversion** - - Test results reveal that pinyin4js takes about 110 milliseconds to convert the supported 20902 Chinese characters. - -* **Multiple output formats** - - The output can come with phonetic symbols, without phonetic symbols, phonetic symbols represented by numbers, or Pinyin initials. - -* **Common multi-sound character recognition** - - Common multi-sound characters, as in phrases, idioms, and place names, can be recognized. - -* **Simplified/Traditional Chinese conversion** - -* **Custom dictionaries** - - Custom dictionaries can be added. - -## Download and Installation -``` -ohpm install @ohos/pinyin4js -``` - -## How to Use - -```javascript - import {pinyin4js} from '@ohos/pinyin4js'; - - // more detail methods in test - // WITH_TONE_NUMBER: with a number indicating the tone; WITHOUT_TONE: without any tone marks; WITH_TONE_MARK: with a tone mark - // output: xià#mén#nǐ#hǎo#dà#shà#xià#mén - console.info("00771-" + pinyin4js.convertToPinyinString('校对', '#', pinyin4js.WITH_TONE_MARK)); - console.info("00771-" + pinyin4js.convertToPinyinString('學校', '#', pinyin4js.WITH_TONE_MARK)); - console.info("00772-" + pinyin4js.convertToPinyinString('厦门你好大厦厦门', '#', pinyin4js.WITHOUT_TONE)); - - // Initial letter style - // output: xmnhdsxm - console.info("00773-" + pinyin4js.convertToPinyinString('厦门你好大厦厦门', '', pinyin4js.FIRST_LETTER)); - // or - console.info("00774-" + pinyin4js.getShortPinyin('厦门你好大厦厦门')); - - // Convert traditional Chinese to simplified Chinese. - console.info("00775-" + pinyin4js.convertToSimplifiedChinese('歲月時光')); - // Convert simplified Chinese to traditional Chinese. - console.info("00776-" + pinyin4js.convertToTraditionalChinese('岁月时光')); - -``` - -## Constraints - -pinyin4js has been verified in the following versions: - -DevEco Studio: DevEco Studio:4.0 (4.0.3.512), SDK:API10 (4.0.10.9) - -## How to Contribute -If you find any problem when using Lottie, submit an [issue](https://gitee.com/openharmony-tpc/pinyin4js/issues) or a [PR](https://gitee.com/openharmony-tpc/pinyin4js/pulls) to us. - -## License -This project is licensed under the terms of the [MIT License](https://gitee.com/openharmony-tpc/pinyin4js/blob/master/LICENSE). diff --git a/README.md b/README.md index ff93da2..7386ad9 100644 --- a/README.md +++ b/README.md @@ -1,81 +1,81 @@ # pinyin4js -## 简介 +## Introduction -适配了OpenHarmony的一款汉字转拼音的Javascript开源库,包含如下特性: +pinyin4js is an open-source JavaScript library that has been adapted to OpenHarmony for converting Chinese characters to Pinyin. It has the following features: -* **零依赖** +* Zero dependency -* **词库灵活导入,打包** - 可以自行调整字典,具体可以参照src/dict;所有资源调用由PinyinResource封装,可自行修改后打包 +* Flexible import and packaging of word dictionaries + You can adjust the dictionary. For details, see **src/dict**. All resource calls are encapsulated by PinyinResource. You can modify resources before packaging them. -* **准确、完善的字库** - Unicode编码从4E00-9FA5范围及3007(〇)的20903个汉字中,pinyin4js能转换除46个异体字(异体字不存在标准拼音)之外的所有汉字 +* Accurate and complete font library + For the 20903 Chinese characters in the Unicode 4E00-9FA5 range and 3007 (〇), pinyin4js can convert all Chinese characters except 46 heterogeneity characters that have no standard pinyin. -* **拼音转换速度快** - 经测试,从4E00-9FA5范围的20902个汉字,pinyin4js耗时约110毫秒 +* Fast pinyin conversion + According to the test result, pinyin4js takes about 110 ms to process 20902 Chinese characters from 4E00 to 9FA5. -* **多拼音格式输出支持** - 支持多种拼音输出格式:带音标、不带音标、数字表示音标以及拼音首字母输出格式 +* Multi-pinyin output + Supports multiple pinyin output formats: with or without phonetic symbols, with phonetic symbols represented by digits, and with the initial letter of the pinyin. -* **常见多音字识别** - 支持常见多音字的识别,其中包括词组、成语、地名等 +* Multi-tone recognition + Supports recognition of common multi-tone characters, including phrases, idioms, and place names. -* **简繁体中文转换** +* Simplified and Traditional Chinese conversion -* **支持添加自定义字典** - 支持添加用户自定义字典 +* Adding a user-defined dictionary + Supports the adding of user-defined dictionaries. -## 下载安装 +## How to Install ``` ohpm install @ohos/pinyin4js ``` -## 使用说明 +## How to Use ```javascript import {pinyin4js} from '@ohos/pinyin4js'; // more detail methods in test - // WITH_TONE_NUMBER--数字代表声调,WITHOUT_TONE--不带声调,WITH_TONE_MARK--带声调 + // WITH_TONE_NUMBER indicates that tones are represented by numbers, WITHOUT_TONE indicates pinyin without tones, and WITH_TONE_MARK indicates pinyin with tones. // output: xià#mén#nǐ#hǎo#dà#shà#xià#mén - console.info("00771-" + pinyin4js.convertToPinyinString('校对', '#', pinyin4js.WITH_TONE_MARK)); - console.info("00771-" + pinyin4js.convertToPinyinString('學校', '#', pinyin4js.WITH_TONE_MARK)); - console.info("00772-" + pinyin4js.convertToPinyinString('厦门你好大厦厦门', '#', pinyin4js.WITHOUT_TONE)); + console.info("00771-" + pinyin4js.convertToPinyinString ('jiaodui', '#', pinyin4js.WITH_TONE_MARK)); + console.info("00771-" + pinyin4js.convertToPinyinString ('xuexiao', '#', pinyin4js.WITH_TONE_MARK)); + console.info("00772-" + pinyin4js.convertToPinyinString ('Xiamennihaodaxiaxiamen', '#', pinyin4js.WITHOUT_TONE)); - // 首字母风格 + // Initial letter style // output: xmnhdsxm - console.info("00773-" + pinyin4js.convertToPinyinString('厦门你好大厦厦门', '', pinyin4js.FIRST_LETTER)); + console.info("00773-" + pinyin4js.convertToPinyinString ('Xiamennihaodaxiaxiamen',", pinyin4js.FIRST_LETTER)); // or - console.info("00774-" + pinyin4js.getShortPinyin('厦门你好大厦厦门')); + console.info("00774-" + pinyin4js.getShortPinyin ('Xiamennihaodaxiaxiamen'); - // 繁体转简体 - console.info("00775-" + pinyin4js.convertToSimplifiedChinese('歲月時光')); - // 简体转繁体 - console.info("00776-" + pinyin4js.convertToTraditionalChinese('岁月时光')); + // Traditional Chinese to simplified Chinese + console.info("00775-" + pinyin4js.convertToSimplifiedChinese ('歲月時光')); + // Simplified Chinese to Traditional Chinese + console.info("00776-" + pinyin4js.convertToTraditionalChinese ('岁月时光'); ``` -## 接口说明 +## Available APIs -| 方法名 | 接口描述 | +| API | Description | |:----------------------------------:|:-------------------------------:| -| convertToPinyinString() | 将字符串转换成相应格式的拼音 | -| convertToSimplifiedChinese() | 将繁体转化为简体 | -| convertToTraditionalChinese() | 将简体转化为繁体 | -| getShortPinyin() | 获取拼音的首字母 | -| hasMultiPinyin() | 判断一个汉字是否为多音字 | -| containsChinese() | 是否包含汉字 | -| isChinese() | 判断是否为汉字 | +| convertToPinyinString() | Converts a string into pinyin of the corresponding format. | +| convertToSimplifiedChinese() | Converts traditional Chinese characters to simplified Chinese characters. | +| convertToTraditionalChinese() | Convert Simplified to Traditional Chinese. | +| getShortPinyin() | Obtains the initial letter of pinyin. | +| hasMultiPinyin() | Checks whether a Chinese character is a multi-tone character. | +| containsChinese() | Checks whether Chinese characters are contained. | +| isChinese() | Checks whether the character is a Chinese character. | -## 约束与限制 +## Constraints -在下述版本验证通过: +This project has been verified in the following version: -DevEco Studio版本: Deveco Studio:4.0 (4.0.3.512),SDK:API10 (4.0.10.9) +DevEco Studio: DevEco Studio:4.0 (4.0.3.512), SDK:API10 (4.0.10.9) -## 贡献代码 -使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-tpc/pinyin4js/issues) 给我们,当然,我们也非常欢迎你给我们发 [PR](https://gitee.com/openharmony-tpc/pinyin4js/pulls) 。 +## How to Contribute +If you find any problem when using the project, submit an [Issue](https://gitee.com/openharmony-tpc/pinyin4js/issues) or a [PR](https://gitee.com/openharmony-tpc/pinyin4js/pulls) to us. -## 开源协议 -本项目基于 [MIT License](https://gitee.com/openharmony-tpc/pinyin4js/blob/master/LICENSE) ,请自由地享受和参与开源。 \ No newline at end of file +## License +This project is licensed under [MIT License](https://gitee.com/openharmony-tpc/pinyin4js/blob/master/LICENSE). diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000..0211d64 --- /dev/null +++ b/README_zh.md @@ -0,0 +1,81 @@ +# pinyin4js + +## 简介 + +适配了OpenHarmony的一款汉字转拼音的Javascript开源库,包含如下特性: + +* **零依赖** + +* **词库灵活导入,打包** + 可以自行调整字典,具体可以参照src/dict;所有资源调用由PinyinResource封装,可自行修改后打包 + +* **准确、完善的字库** + Unicode编码从4E00-9FA5范围及3007(〇)的20903个汉字中,pinyin4js能转换除46个异体字(异体字不存在标准拼音)之外的所有汉字 + +* **拼音转换速度快** + 经测试,从4E00-9FA5范围的20902个汉字,pinyin4js耗时约110毫秒 + +* **多拼音格式输出支持** + 支持多种拼音输出格式:带音标、不带音标、数字表示音标以及拼音首字母输出格式 + +* **常见多音字识别** + 支持常见多音字的识别,其中包括词组、成语、地名等 + +* **简繁体中文转换** + +* **支持添加自定义字典** + 支持添加用户自定义字典 + +## 下载安装 +``` +ohpm install @ohos/pinyin4js +``` + +## 使用说明 + +```javascript + import {pinyin4js} from '@ohos/pinyin4js'; + + // more detail methods in test + // WITH_TONE_NUMBER--数字代表声调,WITHOUT_TONE--不带声调,WITH_TONE_MARK--带声调 + // output: xià#mén#hǎo#dà#shà#xià#mén + console.info("00771-" + pinyin4js.convertToPinyinString('校对', '#', pinyin4js.WITH_TONE_MARK)); + console.info("00771-" + pinyin4js.convertToPinyinString('學校', '#', pinyin4js.WITH_TONE_MARK)); + console.info("00772-" + pinyin4js.convertToPinyinString('厦门好大厦厦门', '#', pinyin4js.WITHOUT_TONE)); + + // 首字母风格 + // output: xmhdsxm + console.info("00773-" + pinyin4js.convertToPinyinString('厦门好大厦厦门', '', pinyin4js.FIRST_LETTER)); + // or + console.info("00774-" + pinyin4js.getShortPinyin('厦门好大厦厦门')); + + // 繁体转简体 + console.info("00775-" + pinyin4js.convertToSimplifiedChinese('歲月時光')); + // 简体转繁体 + console.info("00776-" + pinyin4js.convertToTraditionalChinese('岁月时光')); + +``` + +## 接口说明 + +| 方法名 | 接口描述 | +|:----------------------------------:|:-------------------------------:| +| convertToPinyinString() | 将字符串转换成相应格式的拼音 | +| convertToSimplifiedChinese() | 将繁体转化为简体 | +| convertToTraditionalChinese() | 将简体转化为繁体 | +| getShortPinyin() | 获取拼音的首字母 | +| hasMultiPinyin() | 判断一个汉字是否为多音字 | +| containsChinese() | 是否包含汉字 | +| isChinese() | 判断是否为汉字 | + +## 约束与限制 + +在下述版本验证通过: + +DevEco Studio版本: Deveco Studio:4.0 (4.0.3.512),SDK:API10 (4.0.10.9) + +## 贡献代码 +使用过程中发现任何问题都可以提 [Issue](https://gitee.com/openharmony-tpc/pinyin4js/issues) 给组件,当然,也非常欢迎给发 [PR](https://gitee.com/openharmony-tpc/pinyin4js/pulls)共建 。 + +## 开源协议 +本项目基于 [MIT License](https://gitee.com/openharmony-tpc/pinyin4js/blob/master/LICENSE) ,请自由地享受和参与开源。 \ No newline at end of file diff --git a/entry/src/main/ets/pages/index.ets b/entry/src/main/ets/pages/index.ets index cee7a8a..a57e3e3 100644 --- a/entry/src/main/ets/pages/index.ets +++ b/entry/src/main/ets/pages/index.ets @@ -37,7 +37,7 @@ struct Index { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { Row() { - TextInput({ placeholder: '简体字带声调', text: this.v1t }) + TextInput({ placeholder: this.getResourceString('Simplified_Chinese_characters_with_tones'), text: this.v1t }) .onChange((value: string) => { this.v1t = value; }) @@ -50,7 +50,7 @@ struct Index { } Row() { - TextInput({ placeholder: '繁体字带声调', text: this.v2t }) + TextInput({ placeholder: this.getResourceString('Traditional_Chinese_characters_with_tones'), text: this.v2t }) .onChange((value: string) => { this.v2t = value; }) @@ -63,7 +63,7 @@ struct Index { } Column() { - TextInput({ placeholder: '不带声调', text: this.v3t }) + TextInput({ placeholder: this.getResourceString('Without_tone'), text: this.v3t }) .onChange((value: string) => { this.v3t = value; }) @@ -79,7 +79,7 @@ struct Index { } Row() { - TextInput({ placeholder: '首字母风格方法一', text: this.v4t }) + TextInput({ placeholder: this.getResourceString('First_letter_style_method_one'), text: this.v4t }) .onChange((value: string) => { this.v4t = value; }) @@ -92,7 +92,7 @@ struct Index { } Row() { - TextInput({ placeholder: '首字母风格方法二', text: this.v5t }) + TextInput({ placeholder: this.getResourceString('Initial_style_method_2'), text: this.v5t }) .onChange((value: string) => { this.v5t = value; }) @@ -105,7 +105,7 @@ struct Index { } Row() { - TextInput({ placeholder: '繁体转简体', text: this.v6t }) + TextInput({ placeholder: this.getResourceString('Traditional_to_Simplified_Chinese'), text: this.v6t }) .onChange((value: string) => { this.v6t = value; }) @@ -118,7 +118,7 @@ struct Index { } Row() { - TextInput({ placeholder: '简体转繁体', text: this.v7t }) + TextInput({ placeholder: this.getResourceString('Simplified_to_Traditional_Chinese'), text: this.v7t }) .onChange((value: string) => { this.v7t = value; }) @@ -167,4 +167,8 @@ struct Index { .width('100%') .height('100%') } + + getResourceString(resourceName:string) { + return getContext().resourceManager.getStringByNameSync(resourceName) + } } \ No newline at end of file diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index f945955..1207c19 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -11,6 +11,34 @@ { "name": "EntryAbility_label", "value": "label" + }, + { + "name": "Simplified_Chinese_characters_with_tones", + "value": "Simplified Chinese characters with tones" + }, + { + "name": "Traditional_Chinese_characters_with_tones", + "value": "Traditional Chinese characters with tones" + }, + { + "name": "Without_tone", + "value": "Without tone" + }, + { + "name": "First_letter_style_method_one", + "value": "First letter style method one" + }, + { + "name": "Initial_style_method_2", + "value": "Initial style method 2" + }, + { + "name": "Traditional_to_Simplified_Chinese", + "value": "Traditional to Simplified Chinese" + }, + { + "name": "Simplified_to_Traditional_Chinese", + "value": "Simplified to Traditional Chinese" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json index f945955..1207c19 100644 --- a/entry/src/main/resources/en_US/element/string.json +++ b/entry/src/main/resources/en_US/element/string.json @@ -11,6 +11,34 @@ { "name": "EntryAbility_label", "value": "label" + }, + { + "name": "Simplified_Chinese_characters_with_tones", + "value": "Simplified Chinese characters with tones" + }, + { + "name": "Traditional_Chinese_characters_with_tones", + "value": "Traditional Chinese characters with tones" + }, + { + "name": "Without_tone", + "value": "Without tone" + }, + { + "name": "First_letter_style_method_one", + "value": "First letter style method one" + }, + { + "name": "Initial_style_method_2", + "value": "Initial style method 2" + }, + { + "name": "Traditional_to_Simplified_Chinese", + "value": "Traditional to Simplified Chinese" + }, + { + "name": "Simplified_to_Traditional_Chinese", + "value": "Simplified to Traditional Chinese" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json index 5a65e9b..2a582db 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -11,6 +11,34 @@ { "name": "EntryAbility_label", "value": "pinyin4js" + }, + { + "name": "Simplified_Chinese_characters_with_tones", + "value": "简体字带声调" + }, + { + "name": "Traditional_Chinese_characters_with_tones", + "value": "繁体字带声调" + }, + { + "name": "Without_tone", + "value": "不带声调" + }, + { + "name": "First_letter_style_method_one", + "value": "首字母风格方法一" + }, + { + "name": "Initial_style_method_2", + "value": "首字母风格方法二" + }, + { + "name": "Traditional_to_Simplified_Chinese", + "value": "繁体转简体" + }, + { + "name": "Simplified_to_Traditional_Chinese", + "value": "简体转繁体" } ] } \ No newline at end of file -- Gitee