1
This commit is contained in:
@@ -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