android-opencc是一个比较强大的汉字简繁转换库。
首先在项目根目录的build.gradle中添加如下内容:
repositories {
...
maven { url 'https://jitpack.io' }
}
然后添加依赖:
dependencies {
...
//简繁体转换
implementation 'com.github.qichuan:android-opencc:1.2.0'
}
咱先小小的封装一下:
/**
* 描 述: <汉字简繁体转换>
*/
class HyConverter private constructor(val context: Context) {
companion object {
fun it(context: Context): HyConverter {
return HyConverter(context)
}
}
/**
* HK2S, Traditional Chinese (Hong Kong Standard) to Simplified
* 香港繁体(香港小学学习字词表标准)到简体
*/
fun doHK2S(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.HK2S, context)
}
/**
* HK2T, Traditional Chinese (Hong Kong variant) to Traditional Chinese
* 香港繁体(香港小学学习字词表标准)到繁体
*/
fun doHK2T(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.HK2T, context)
}
/**
* JP2T, New Japanese Kanji (Shinjitai) to Traditional Chinese Characters (Kyūjitai)
* 日本汉字到繁体
*/
fun doJP2T(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.JP2T, context)
}
/**
* S2HK, Simplified Chinese to Traditional Chinese (Hong Kong Standard)
* 简体到香港繁体(香港小学学习字词表标准)
*/
fun doS2HK(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.S2HK, context)
}
/**
* S2T, Simplified Chinese to Traditional Chinese
* 简体到繁体
*/
fun doS2T(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.S2T, context)
}
/**
* S2TW, Simplified Chinese to Traditional Chinese (Taiwan Standard)
* 简体到台湾正体
*/
fun doS2TW(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.S2TW, context)
}
/**
* S2TWP, Simplified Chinese to Traditional Chinese (Taiwan Standard) with Taiwanese idiom
* 简体到台湾正体,并转换为台湾常用词汇
*/
fun doS2TWP(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.S2TWP, context)
}
/**
* T2HK, Traditional Chinese to Traditional Chinese (Hong Kong Standard)
* 繁体到香港繁体(香港小学学习字词表标准)
*/
fun doT2HK(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.T2HK, context)
}
/**
* T2S, Traditional Chinese to Simplified Chinese
* 繁体到简体
*/
fun doT2S(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.T2S, context)
}
/**
* T2TW, Traditional Chinese to Traditional Chinese (Taiwan Standard)
* 繁体到台湾正体
*/
fun doT2TW(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.T2TW, context)
}
/**
* TW2S, Traditional Chinese (Taiwan Standard) to Simplified Chinese
* 台湾正体到简体
*/
fun doTW2S(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.TW2S, context)
}
/**
* T2JP, Traditional Chinese Characters (Kyūjitai) to New Japanese Kanji (Shinjitai)
* 繁体到日本汉字
*/
fun doT2JP(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.T2JP, context)
}
/**
* TW2T, Traditional Chinese (Taiwan standard) to Traditional Chinese
* 台湾正体到繁体
*/
fun doTW2T(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.TW2T, context)
}
/**
* TW2SP, Traditional Chinese (Taiwan Standard) to Simplified Chinese with Mainland Chinese idiom
* 台湾正体到简体并转换为中国大陆常用词汇
*/
fun doTW2SP(origin: String): String {
return ChineseConverter.convert(origin, ConversionType.TW2SP, context)
}
}
使用例子:
//简体到繁体
val fanti = HyConverter.it(context).doS2T("这里输入一个简体的字符串")