项目作者: dzcpy

项目描述 :
UTF-8 to ASCII transliteration / slugify module for node.js, browser, Web Worker, React Native, Electron and CLI.
高级语言: TypeScript
项目地址: git://github.com/dzcpy/transliteration.git
创建时间: 2014-08-27T07:45:46Z
项目社区:https://github.com/dzcpy/transliteration

开源协议:MIT License

下载


Transliteration

Build Status
Coverage Status
NPM Version
NPM Download
JSDelivr Download
License

Transliteration

Universal Unicode to Latin transliteration + slugify module. Works on all platforms and with all major languages.

Try it out online →

Table of Contents

Features

  • Convert Unicode characters to their Latin equivalents
  • Create URL-friendly slugs from any Unicode string
  • Customizable transliteration options
  • Works in Node.js, browsers, and command-line
  • TypeScript support
  • Lightweight and dependency-free

Compatibility

  • Browsers: IE 9+ and all modern browsers
  • Server: Node.js (all versions)
  • Mobile: React Native
  • Environments: Web Workers, CLI

Installation

Node.js / React Native

  1. npm install transliteration --save

Note for TypeScript users: Type definition files are built into this project since version 2.x. Do not install @types/transliteration.

Basic usage example:

  1. import { transliterate as tr, slugify } from 'transliteration';
  2. // Transliteration
  3. tr('你好, world!'); // => 'Ni Hao , world!'
  4. // Slugify
  5. slugify('你好, world!'); // => 'ni-hao-world'

Browser (CDN)

UMD Build (Global Variables)

  1. <script src="https://cdn.jsdelivr.net/npm/transliteration@2.1.8/dist/browser/bundle.umd.min.js"></script>
  2. <script>
  3. // Available as global variables
  4. transliterate('你好, World'); // => 'Ni Hao , World'
  5. slugify('Hello, 世界'); // => 'hello-shi-jie'
  6. // Legacy method (will be removed in next major version)
  7. transl('Hola, mundo'); // => 'hola-mundo'
  8. </script>

ES Module

  1. <script type="module">
  2. import { transliterate } from 'https://cdn.jsdelivr.net/npm/transliteration@2.1.8/dist/browser/bundle.esm.min.js';
  3. console.log(transliterate('你好')); // => 'Ni Hao'
  4. </script>

CLI

  1. # Global installation
  2. npm install transliteration -g
  3. # Basic usage
  4. transliterate 你好 # => Ni Hao
  5. slugify 你好 # => ni-hao
  6. # Using stdin
  7. echo 你好 | slugify -S # => ni-hao

Usage

transliterate(str, [options])

Transliterates the string str and returns the result. Characters that this module cannot handle will default to the placeholder character(s) specified in the unknown option. If no placeholder is provided, these characters will be removed.

Options

Option Type Default Description
ignore string[] [] List of strings to ignore (keep unchanged)
replace object or array {} Custom replacements before transliteration
replaceAfter object or array {} Custom replacements after transliteration
trim boolean false Whether to trim the result string
unknown string '' Placeholder for unknown characters
fixChineseSpacing boolean true Add spaces between transliterated Chinese characters

Example

  1. import { transliterate as tr } from 'transliteration';
  2. // Basic usage
  3. tr('你好,世界'); // => 'Ni Hao , Shi Jie'
  4. tr('Γεια σας, τον κόσμο'); // => 'Geia sas, ton kosmo'
  5. tr('안녕하세요, 세계'); // => 'annyeonghaseyo, segye'
  6. // With options
  7. tr('你好,世界', {
  8. replace: { 你: 'You' },
  9. ignore: ['好']
  10. }); // => 'You 好, Shi Jie'
  11. // Array form of replace option
  12. tr('你好,世界', {
  13. replace: [['你', 'You']],
  14. ignore: ['好']
  15. }); // => 'You 好, Shi Jie'

transliterate.config([optionsObj], [reset = false])

Binds option object globally so all subsequent calls will use optionsObj by default. If optionsObj is not provided, it will return the current default option object.

  1. // Set global configuration
  2. tr.config({ replace: [['你', 'You']], ignore: ['好'] });
  3. // All calls will use this configuration
  4. tr('你好,世界'); // => 'You 好, Shi Jie'
  5. // View current configuration
  6. console.log(tr.config()); // => { replace: [['你', 'You']], ignore: ['好'] }
  7. // Reset to defaults
  8. tr.config(undefined, true);
  9. console.log(tr.config()); // => {}

slugify(str, [options])

Converts Unicode str into a slug string, ensuring it is safe to use in a URL or filename.

Options

Option Type Default Description
ignore string[] [] List of strings to ignore (keep unchanged)
replace object or array {} Custom replacements before transliteration
replaceAfter object or array {} Custom replacements after transliteration
trim boolean false Whether to trim the result string
unknown string '' Placeholder for unknown characters
lowercase boolean true Convert result to lowercase
uppercase boolean false Convert result to uppercase
separator string - Character used between words
allowedChars string a-zA-Z0-9-_.~' Regex pattern of allowed characters
fixChineseSpacing boolean true Add spaces between transliterated Chinese characters

Example

  1. // Basic usage
  2. slugify('你好,世界'); // => 'ni-hao-shi-jie'
  3. // With options
  4. slugify('你好,世界', {
  5. lowercase: false,
  6. separator: '_'
  7. }); // => 'Ni_Hao_Shi_Jie'
  8. // Using replace option
  9. slugify('你好,世界', {
  10. replace: {
  11. 你好: 'Hello',
  12. 世界: 'world'
  13. },
  14. separator: '_'
  15. }); // => 'hello_world'
  16. // Using ignore option
  17. slugify('你好,世界', {
  18. ignore: ['你好']
  19. }); // => '你好shi-jie'

slugify.config([optionsObj], [reset = false])

Binds option object globally so all subsequent calls will use optionsObj by default. If optionsObj is not provided, it will return the current default option object.

  1. // Set global configuration
  2. slugify.config({ lowercase: false, separator: '_' });
  3. // All calls will use this configuration
  4. slugify('你好,世界'); // => 'Ni_Hao_Shi_Jie'
  5. // View current configuration
  6. console.log(slugify.config()); // => { lowercase: false, separator: "_" }
  7. // Reset to defaults
  8. slugify.config(undefined, true);
  9. console.log(slugify.config()); // => {}

CLI Usage

Transliterate Command

  1. transliterate <unicode> [options]
  2. Options:
  3. --version Show version number [boolean]
  4. -u, --unknown Placeholder for unknown characters [string] [default: ""]
  5. -r, --replace Custom string replacement [array] [default: []]
  6. -i, --ignore String list to ignore [array] [default: []]
  7. -S, --stdin Use stdin as input [boolean] [default: false]
  8. -h, --help Show help information [boolean]
  9. Examples:
  10. transliterate "你好, world!" -r 好=good -r "world=Shi Jie"
  11. transliterate "你好,世界!" -i 你好 -i

Slugify Command

  1. slugify <unicode> [options]
  2. Options:
  3. --version Show version number [boolean]
  4. -U, --unknown Placeholder for unknown characters [string] [default: ""]
  5. -l, --lowercase Returns result in lowercase [boolean] [default: true]
  6. -u, --uppercase Returns result in uppercase [boolean] [default: false]
  7. -s, --separator Separator of the slug [string] [default: "-"]
  8. -r, --replace Custom string replacement [array] [default: []]
  9. -i, --ignore String list to ignore [array] [default: []]
  10. -S, --stdin Use stdin as input [boolean] [default: false]
  11. -h, --help Show help information [boolean]
  12. Examples:
  13. slugify "你好, world!" -r 好=good -r "world=Shi Jie"
  14. slugify "你好,世界!" -i 你好 -i

Known Issues

Currently, transliteration only supports 1-to-1 code mapping (from Unicode to Latin). This is the simplest implementation approach, but it has limitations with polyphonic characters. Please test thoroughly with your specific languages before using in production.

Known language-specific issues:

Language Issue Alternative
Chinese Polyphonic characters may not transliterate correctly pinyin
Japanese Kanji characters often convert to Chinese Pinyin due to Unicode overlap kuroshiro
Thai Not working properly Issue #67
Cyrillic May be inaccurate for specific languages like Bulgarian -

If you find any other issues, please raise a ticket.

License

MIT