Nodejs 音译(transliteration)模块,支持多语言(中文)转拼音或生成 slug,支持浏览器中使用(已更新)

Nodejs 音译(transliteration)模块,支持多语言(中文)转拼音或生成 slug,支持浏览器中使用(已更新)

全面采用 babel + ES6, eslint + airbnb, mocha + nyc (istanbul) + coveralls + travis-ci, yargs, gulp + browserify + babelify,整个模块可生成单文件在node.js、浏览器和命令行运行(mac, windows linux)。尽量采用最优最新的工具实现,测试覆盖率100%,欢迎 star 和提交 pull request! 更详细使用请参见 https://github.com/andyhu/node-transliteration Demo: http://rawgit.com/andyhu/node-transliteration/master/demo/example.html

npm install transliteration --save
var transliteration = require('transliteration');

var slug = tr.slugify; var tr = transliteration.transliterate //import { transliterate as tr, slugify as slug } from ‘transliteration’; /* For ES6 syntax */

tr(‘你好, world!’); // Ni Hao , world! slugify(‘你好, world!’); // ni-hao-world

# Install bower if not already installed

npm install bower -g

bower install transliteration

<html>
<head>
<script src=“bower_components/transliteration/transliteration.min.js”></script>
</head>
<body>
<script>
transl(‘你好, world!’); // Ni Hao , world!
slugify(‘你好, world!’); // ni-hao-world
</script>
</body>
</html>
npm install transliteration -g

transliterate 你好 # Ni Hao slugify 你好 # ni-hao


7 回复

Nodejs 音译(transliteration)模块,支持多语言(中文)转拼音或生成 slug,支持浏览器中使用(已更新)

概述

本文介绍了一个名为 transliteration 的 Node.js 模块,该模块能够将多种语言(包括中文)的文本转换为拼音或生成 slug。此模块不仅可以在 Node.js 环境中使用,还支持在浏览器环境中运行。

安装与使用

在 Node.js 中安装

首先,通过 npm 安装 transliteration 模块:

npm install transliteration --save

然后,在你的 Node.js 项目中引入并使用该模块:

var transliteration = require('transliteration');
var slug = transliteration.slugify;
var tr = transliteration.transliterate;

console.log(tr('你好, world!')); // 输出: Ni Hao , world!
console.log(slug('你好, world!')); // 输出: ni-hao-world
在浏览器中使用

如果你希望在浏览器中使用该模块,可以先通过 Bower 安装:

# 安装 Bower 如果尚未安装
npm install bower -g

# 安装 transliteration 模块
bower install transliteration

接着,在 HTML 文件中引入 transliteration.min.js 文件,并使用该模块:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Transliteration Demo</title>
  <script src="bower_components/transliteration/transliteration.min.js"></script>
</head>
<body>
  <script>
    console.log(transliteration.transliterate('你好, world!')); // 输出: Ni Hao , world!
    console.log(transliteration.slugify('你好, world!')); // 输出: ni-hao-world
  </script>
</body>
</html>
全局安装与命令行使用

你也可以全局安装该模块,并通过命令行使用:

npm install transliteration -g

# 转换为拼音
transliterate 你好 # 输出: Ni Hao

# 生成 slug
slugify 你好 # 输出: ni-hao

更多功能

更多功能和详细文档,请访问该项目的 GitHub 页面:https://github.com/andyhu/node-transliteration

总结

transliteration 模块提供了强大的音译功能,能够处理多种语言的文本转换。无论是 Node.js 应用、浏览器环境还是命令行,都能方便地使用该模块。其高覆盖率的测试保证了代码的稳定性,欢迎大家试用并贡献代码!


其实,这种我一般都是调用一些翻译网站的api做的

原理是怎么作的啊?

代码已更新,支持在浏览器中访问

:-O 好神奇!

已更新至1.0.x版本。欢迎star!

这个Nodejs模块transliteration能够将多种语言(包括中文)转换成拼音或生成slug,且支持在Node.js环境、浏览器以及命令行中的使用。

安装该模块后,您可以通过以下方式使用它:

const transliteration = require('transliteration');
const slugify = transliteration.slugify;
const transliterate = transliteration.transliterate;

console.log(transliterate('你好, world!')); // 输出:Ni Hao , world!
console.log(slugify('你好, world!')); // 输出:ni-hao-world

如果您希望在ES6环境中使用该模块,则可以这样导入:

import { transliterate, slugify } from 'transliteration';

console.log(transliterate('你好, world!')); // 输出:Ni Hao , world!
console.log(slugify('你好, world!')); // 输出:ni-hao-world

对于浏览器环境,首先需要通过Bower来安装transliteration模块:

bower install transliteration

然后,在HTML文件中引入transliteration.min.js文件,并使用以下代码进行操作:

<html>
<head>
  <script src="bower_components/transliteration/transliteration.min.js"></script>
</head>
<body>
  <script>
    console.log(transl('你好, world!')); // 输出:Ni Hao , world!
    console.log(slugify('你好, world!')); // 输出:ni-hao-world
  </script>
</body>
</html>

此外,您还可以将其作为全局命令行工具安装,并直接在命令行中执行以下操作:

npm install transliteration -g

transliterate 你好 # 输出:Ni Hao
slugify 你好 # 输出:ni-hao

整个项目已经进行了充分的测试,确保了其功能性和兼容性,欢迎提出改进意见并提交Pull Request。

回到顶部