1
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
## 1.0.1(2025-09-24)
|
||||
支持Android,iOS,Harmony,web/h5,小程序
|
||||
## 1.0.0(2024-06-1)
|
||||
安卓,ios支持。
|
||||
@@ -0,0 +1,115 @@
|
||||
{
|
||||
"id": "x-chinesetopinyin-s",
|
||||
"displayName": "中文转拼音 全平台支持",
|
||||
"version": "1.0.1",
|
||||
"description": "支持多达5个端已经在安卓,ios,鸿蒙,web/h5,微信小程序上测试通过",
|
||||
"keywords": [
|
||||
"中文转拼音"
|
||||
],
|
||||
"repository": "",
|
||||
"engines": {
|
||||
"HBuilderX": "^3.6.8",
|
||||
"uni-app": "^4.76",
|
||||
"uni-app-x": "^4.76"
|
||||
},
|
||||
"dcloudext": {
|
||||
"type": "uts",
|
||||
"sale": {
|
||||
"regular": {
|
||||
"price": "99.00"
|
||||
},
|
||||
"sourcecode": {
|
||||
"price": "0.00"
|
||||
}
|
||||
},
|
||||
"contact": {
|
||||
"qq": "369986563"
|
||||
},
|
||||
"declaration": {
|
||||
"ads": "无",
|
||||
"data": "无",
|
||||
"permissions": "音频播放权限"
|
||||
},
|
||||
"npmurl": "",
|
||||
"darkmode": "√",
|
||||
"i18n": "√",
|
||||
"widescreen": "√"
|
||||
},
|
||||
"uni_modules": {
|
||||
"dependencies": [],
|
||||
"encrypt": [],
|
||||
"platforms": {
|
||||
"cloud": {
|
||||
"tcb": "√",
|
||||
"aliyun": "√",
|
||||
"alipay": "√"
|
||||
},
|
||||
"client": {
|
||||
"uni-app": {
|
||||
"vue": {
|
||||
"vue2": "-",
|
||||
"vue3": "-"
|
||||
},
|
||||
"web": {
|
||||
"safari": "-",
|
||||
"chrome": "-"
|
||||
},
|
||||
"app": {
|
||||
"vue": "-",
|
||||
"nvue": "-",
|
||||
"android": "-",
|
||||
"ios": "-",
|
||||
"harmony": "-"
|
||||
},
|
||||
"mp": {
|
||||
"weixin": "-",
|
||||
"alipay": "-",
|
||||
"toutiao": "-",
|
||||
"baidu": "-",
|
||||
"kuaishou": "-",
|
||||
"jd": "-",
|
||||
"harmony": "-",
|
||||
"qq": "-",
|
||||
"lark": "-"
|
||||
},
|
||||
"quickapp": {
|
||||
"huawei": "-",
|
||||
"union": "-"
|
||||
}
|
||||
},
|
||||
"uni-app-x": {
|
||||
"web": {
|
||||
"safari": {
|
||||
"extVersion": "1.0.0",
|
||||
"minVersion": "100"
|
||||
},
|
||||
"chrome": {
|
||||
"extVersion": "1.0.0",
|
||||
"minVersion": "100"
|
||||
}
|
||||
},
|
||||
"app": {
|
||||
"android": {
|
||||
"extVersion": "1.0.0",
|
||||
"minVersion": "21"
|
||||
},
|
||||
"ios": {
|
||||
"extVersion": "1.0.0",
|
||||
"minVersion": "12"
|
||||
},
|
||||
"harmony": {
|
||||
"extVersion": "1.0.0",
|
||||
"minVersion": "5.1"
|
||||
}
|
||||
},
|
||||
"mp": {
|
||||
"weixin": {
|
||||
"extVersion": "1.0.0",
|
||||
"minVersion": "3.0.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
# x-chinesetopinyin-s
|
||||
|
||||
### 开发文档
|
||||
|
||||
将中文转为拼音,带注音和不注音
|
||||
|
||||
### 兼容性
|
||||
|
||||
| iOS | Harmony | 小程序 | Andriod | WEB |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| 9+ | 12+ | 全部 | 5.0+ | 全部 |
|
||||
|
||||
### 方法说明
|
||||
|
||||
使用
|
||||
```ts
|
||||
|
||||
import { toPinYin } from '@/uni_modules/x-chinesetopinyin-s';
|
||||
toPinYin({
|
||||
char: "你好",
|
||||
success(res) {
|
||||
// 输出:{detail: "ni hao", zhuyin: "nĭ hăo"}
|
||||
console.log(res)
|
||||
}
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
### toPinYin(opts:XuiChineseToPinYinOpts)
|
||||
|
||||
```ts
|
||||
|
||||
export type XuiChineseToPinYinResult = {
|
||||
detail : string,
|
||||
zhuyin : string
|
||||
}
|
||||
|
||||
export interface XuiChineseToPinYinFail extends IUniError {
|
||||
errCode : number
|
||||
};
|
||||
|
||||
export type XuiChineseToPinYinOpts = {
|
||||
/** 待转换的字符串 */
|
||||
char : string
|
||||
success ?: (res : XuiChineseToPinYinResult) => void
|
||||
fail ?: (res : XuiChineseToPinYinFail) => void
|
||||
complete ?: (res : XuiChineseToPinYinResult|null) => void
|
||||
}
|
||||
|
||||
|
||||
```
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"minSdkVersion": "19",
|
||||
"dependencies":[]
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// https://github.com/promeG/TinyPinyin
|
||||
import Pinyin from "com.github.promeg.pinyinhelper.Pinyin";
|
||||
import PinyinHelper from "net.sourceforge.pinyin4j.PinyinHelper";
|
||||
import HanyuPinyinOutputFormat from 'net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat';
|
||||
import HanyuPinyinToneType from 'net.sourceforge.pinyin4j.format.HanyuPinyinToneType';
|
||||
import HanyuPinyinVCharType from 'net.sourceforge.pinyin4j.format.HanyuPinyinVCharType';
|
||||
import { XuiChineseToPinYinOpts, XuiChineseToPinYinResult } from "../interface.uts"
|
||||
import { XuiChineseToPinYinFailImpl } from "../unierror.uts"
|
||||
|
||||
/** 中文转拼音 */
|
||||
export function toPinYin(opts : XuiChineseToPinYinOpts){
|
||||
try {
|
||||
let c = opts.char;
|
||||
if (c == '') {
|
||||
let errorIml = new XuiChineseToPinYinFailImpl(1002)
|
||||
opts.fail?.(errorIml)
|
||||
opts.complete?.(null)
|
||||
console.error('TMUI Error:','参数不能为空')
|
||||
return;
|
||||
}
|
||||
let result = Pinyin.toPinyin(c," ",null) as string;
|
||||
const fmt = new HanyuPinyinOutputFormat()
|
||||
fmt.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
|
||||
fmt.setToneType(HanyuPinyinToneType.WITH_TONE_MARK)
|
||||
const ok : XuiChineseToPinYinResult = {
|
||||
detail: result.toLowerCase(),
|
||||
zhuyin: PinyinHelper.toHanyuPinyinString(c,fmt,' ')
|
||||
}
|
||||
opts.success?.(ok)
|
||||
opts.complete?.(ok)
|
||||
|
||||
} catch (error:Error) {
|
||||
opts.fail?.(new XuiChineseToPinYinFailImpl(1001))
|
||||
opts.complete?.(null)
|
||||
console.error('TMUI Error:',error.message)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,32 @@
|
||||
import { i18n } from '@kit.LocalizationKit';
|
||||
import { XuiChineseToPinYinOpts, XuiChineseToPinYinResult } from "../interface.uts"
|
||||
import { XuiChineseToPinYinFailImpl } from "../unierror.uts"
|
||||
|
||||
/** 中文转拼音 */
|
||||
export function toPinYin(opts : XuiChineseToPinYinOpts) {
|
||||
try {
|
||||
let c = opts.char;
|
||||
if (c == '') {
|
||||
let errorIml = new XuiChineseToPinYinFailImpl(1002)
|
||||
opts?.fail?.(errorIml)
|
||||
opts?.complete?.(null)
|
||||
return;
|
||||
}
|
||||
let transliterator = i18n.Transliterator.getInstance('Any-Latn');
|
||||
let result = transliterator.transform(opts.char);
|
||||
|
||||
let voiceRemovedTransliterator = i18n.Transliterator.getInstance('Latin-ASCII');
|
||||
let resultZhuying = voiceRemovedTransliterator.transform(opts.char);
|
||||
const ok : XuiChineseToPinYinResult = {
|
||||
detail: result,
|
||||
zhuyin: resultZhuying
|
||||
}
|
||||
opts?.success?.(ok)
|
||||
opts?.complete?.(ok)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
opts?.fail?.(new XuiChineseToPinYinFailImpl(1001))
|
||||
opts?.complete?.(null)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"deploymentTarget": "9"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import {NSMutableString,CFStringTransform,kCFStringTransformToLatin,kCFStringTransformStripDiacritics, StringTransform} from "Foundation"
|
||||
import { XuiChineseToPinYinOpts, XuiChineseToPinYinResult } from "../interface.uts"
|
||||
import { XuiChineseToPinYinFailImpl } from "../unierror.uts"
|
||||
|
||||
/** 中文转拼音 */
|
||||
export function toPinYin(opts : XuiChineseToPinYinOpts){
|
||||
try {
|
||||
let c = opts.char;
|
||||
if (c == '') {
|
||||
let errorIml = new XuiChineseToPinYinFailImpl(1002)
|
||||
opts.fail?.(errorIml)
|
||||
opts.complete?.(null)
|
||||
return;
|
||||
}
|
||||
|
||||
let tostrings = new NSMutableString(string = opts.char)
|
||||
let tostrings2 = new NSMutableString(string = opts.char)
|
||||
CFStringTransform(tostrings,null,kCFStringTransformToLatin,false)
|
||||
|
||||
CFStringTransform(tostrings2,null,kCFStringTransformToLatin,false)
|
||||
CFStringTransform(tostrings2,null,kCFStringTransformStripDiacritics,false)
|
||||
const ok : XuiChineseToPinYinResult = {
|
||||
detail: tostrings2 as string,
|
||||
zhuyin: tostrings as string
|
||||
}
|
||||
opts.success?.(ok)
|
||||
opts.complete?.(ok)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
opts.fail?.(new XuiChineseToPinYinFailImpl(1001))
|
||||
opts.complete?.(null)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
export type XuiChineseToPinYinResult = {
|
||||
detail : string,
|
||||
zhuyin : string
|
||||
}
|
||||
|
||||
export interface XuiChineseToPinYinFail extends IUniError {
|
||||
errCode : number
|
||||
};
|
||||
|
||||
export type XuiChineseToPinYinOpts = {
|
||||
/** 待转换的字符串 */
|
||||
char : string
|
||||
success ?: (res : XuiChineseToPinYinResult) => void
|
||||
fail ?: (res : XuiChineseToPinYinFail) => void
|
||||
complete ?: (res : XuiChineseToPinYinResult|null) => void
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
import { pinyin } from "./pinyintojs.js"
|
||||
import { XuiChineseToPinYinOpts, XuiChineseToPinYinResult } from "../interface.uts"
|
||||
import { XuiChineseToPinYinFailImpl } from "../unierror.uts"
|
||||
|
||||
/** 中文转拼音 */
|
||||
export function toPinYin(opts : XuiChineseToPinYinOpts){
|
||||
try {
|
||||
let c = opts.char;
|
||||
if (c == '') {
|
||||
let errorIml = new XuiChineseToPinYinFailImpl(1002)
|
||||
opts?.fail?.(errorIml)
|
||||
opts?.complete?.(null)
|
||||
return;
|
||||
}
|
||||
const result = pinyin(c, { toneType: 'none', style: pinyin.STYLE_NORMAL })
|
||||
const resultZhuying = pinyin(c, { toneType: 'symbol', style: pinyin.STYLE_NORMAL })
|
||||
const ok : XuiChineseToPinYinResult = {
|
||||
detail: result,
|
||||
zhuyin: resultZhuying
|
||||
}
|
||||
opts?.success?.(ok)
|
||||
opts?.complete?.(ok)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
opts?.fail?.(new XuiChineseToPinYinFailImpl(1001))
|
||||
opts?.complete?.(null)
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,19 @@
|
||||
/* 此规范为 uni 规范,可以按照自己的需要选择是否实现 */
|
||||
import { XuiChineseToPinYinFail } from "./interface.uts"
|
||||
|
||||
export const XuiErrors : Map<number, string> = new Map([
|
||||
[1001, '函数内部错误'],
|
||||
[1002, '参数错误,缺少char参数或者char为空'],
|
||||
]);
|
||||
|
||||
export class XuiChineseToPinYinFailImpl extends UniError implements XuiChineseToPinYinFail {
|
||||
/**
|
||||
* 错误对象构造函数
|
||||
*/
|
||||
constructor(errCode : number) {
|
||||
super();
|
||||
this.errSubject = "x-chinesetopinyin-s";
|
||||
this.errCode = errCode;
|
||||
this.errMsg = XuiErrors.get(errCode) ?? "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { pinyin } from "./pinyintojs.js"
|
||||
import { XuiChineseToPinYinOpts, XuiChineseToPinYinResult } from "../interface.uts"
|
||||
import { XuiChineseToPinYinFailImpl } from "../unierror.uts"
|
||||
|
||||
/** 中文转拼音 */
|
||||
export function toPinYin(opts : XuiChineseToPinYinOpts){
|
||||
try {
|
||||
let c = opts.char;
|
||||
if (c == '') {
|
||||
let errorIml = new XuiChineseToPinYinFailImpl(1002)
|
||||
opts?.fail?.(errorIml)
|
||||
opts?.complete?.(null)
|
||||
return;
|
||||
}
|
||||
const result = pinyin(c, { toneType: 'none', style: pinyin.STYLE_NORMAL })
|
||||
const resultZhuying = pinyin(c, { toneType: 'symbol', style: pinyin.STYLE_NORMAL })
|
||||
const ok : XuiChineseToPinYinResult = {
|
||||
detail: result,
|
||||
zhuyin: resultZhuying
|
||||
}
|
||||
opts?.success?.(ok)
|
||||
opts?.complete?.(ok)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
opts?.fail?.(new XuiChineseToPinYinFailImpl(1001))
|
||||
opts?.complete?.(null)
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user