自动化脚本之-中文转拼音字母

背景

  1. 美术的资源文件往往都是中文名字
  2. 我们做自动化工具 很多时候 生成的目标 不支持中文
  3. 转成拼音。在程序中,也方便我们理解文件对应的代码

解决方案

git地址:https://github.com/hotoo/pinyin

使用方式:

  • node环境下 npm install pinyin
var pinyin = require("pinyin");

console.log(pinyin("中心"));    // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
    
    
  heteronym: true               // 启用多音字模式
}));                            // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
    
    
  heteronym: true,              // 启用多音字模式
  segment: true                 // 启用分词,以解决多音字问题。
}));                            // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("我喜欢你", {
    
    
  segment: true,                // 启用分词
  group: true                   // 启用词组
}));                            // [ [ 'wǒ' ], [ 'xǐhuān' ], [ 'nǐ' ] ]
console.log(pinyin("中心", {
    
    
  style: pinyin.STYLE_INITIALS, // 设置拼音风格
  heteronym: true
}));                            // [ [ 'zh' ], [ 'x' ] ]

具体详细的使用方式 请移步git详情

至此 我们自动化工具的 又可以解析中文了。对美术 和 别的 合作者来说。不需要为了配合我们 在修改自己用中文标注的习惯(虽然可能不是个好习惯。但是中文的标注,美术自己维护起来成本低)

猜你喜欢

转载自blog.csdn.net/qq_45504161/article/details/110297231