What is transliteration?
The 'transliteration' npm package provides utilities for converting text between different scripts, particularly useful for converting non-Latin scripts to Latin scripts. It supports various transliteration schemes and can handle a wide range of languages.
What are transliteration's main functionalities?
Basic Transliteration
This feature allows you to convert text from non-Latin scripts to Latin scripts. In this example, Chinese characters are transliterated to their Latin equivalents.
const { transliterate } = require('transliteration');
const text = '你好,世界';
const result = transliterate(text);
console.log(result); // Output: 'Ni Hao , Shi Jie'
Slugify
This feature converts text into a URL-friendly slug. It is particularly useful for creating SEO-friendly URLs from non-Latin scripts.
const { slugify } = require('transliteration');
const text = '你好,世界';
const result = slugify(text);
console.log(result); // Output: 'ni-hao-shi-jie'
Custom Transliteration
This feature allows you to define custom mappings for transliteration. In this example, specific Cyrillic characters are mapped to their Latin equivalents.
const { transliterate } = require('transliteration');
const text = 'Привет, мир';
const customMap = { 'и': 'i', 'е': 'e', 'в': 'v' };
const result = transliterate(text, { customMap });
console.log(result); // Output: 'Privet, mir'
Other packages similar to transliteration
transliterator
The 'transliterator' package provides similar functionality for converting text between different scripts. It supports a wide range of languages and offers customizable transliteration schemes. Compared to 'transliteration', it may offer more flexibility in defining custom rules.
unidecode
The 'unidecode' package is another alternative for transliterating Unicode text to ASCII. It is particularly known for its simplicity and ease of use. However, it may not support as many languages or offer as much customization as 'transliteration'.
diacritics
The 'diacritics' package focuses on removing diacritical marks from text, converting it to a simpler form. While it doesn't offer full transliteration capabilities, it is useful for normalizing text. It is more specialized compared to 'transliteration'.


Universal Unicode to Latin transliteration + slugify module. Works on all platforms and with all major languages.
Demo
Try it out
Compatibility / Browser support
IE 9+ and all modern browsers, Node.js, Web Worker, React Native and CLI
Installation
Node.js / React Native
npm install transliteration --save
If you are using Typescript, please do not install @types/transliteration
. Since in verson 2.x
, type definition files are built-in within this project.
import { transliterate as tr, slugify } from 'transliteration';
tr('你好, world!');
slugify('你好, world!');
Browser (CDN):
<script
async
defer
src="https://cdn.jsdelivr.net/npm/transliteration@2.1.8/dist/browser/bundle.umd.min.js"
></script>
<script>
console.log(transliterate('你好'));
</script>
<script type="module">
import { transliterate } from 'https://cdn.jsdelivr.net/npm/transliteration@2.1.8/dist/browser/bundle.esm.min.js';
console.log(transliterate('你好'));
</script>
transliteration
can be loaded as an AMD / CommonJS module, or as global variables (UMD).
When you use it in the browser, by default it creates three global variables under window
object:
transliterate('你好, World');
slugify('Hello, 世界');
transl('Hola, mundo');
CLI
npm install transliteration -g
transliterate 你好
slugify 你好
echo 你好 | slugify -S
Usage
transliterate(str, [options])
Transliterate the string str
and return the result. Characters which this module can't handle will default to the placeholder character(s) given in the unknown
option. If it's not provided, they will be removed.
Options: (optional)
{
ignore?: string[];
replace?: OptionReplaceCombined;
replaceAfter?: OptionReplaceCombined;
trim?: boolean;
unknown?: string;
fixChineseSpacing?: boolean;
}
transliterate.config([optionsObj], [reset = false])
Bind option object globally so any following calls will use optionsObj
by default. If optionsObj
is not given, it will return current default option object.
import { transliterate as tr } from 'transliteration';
tr('你好,世界');
tr('Γεια σας, τον κόσμο');
tr('안녕하세요, 세계');
tr('你好,世界', { replace: { 你: 'You' }, ignore: ['好'] });
tr('你好,世界', { replace: [['你', 'You']], ignore: ['好'] });
tr.config({ replace: [['你', 'You']], ignore: ['好'] });
tr('你好,世界');
console.log(tr.config());
tr.config(undefined, true);
console.log(tr.config());
slugify(str, [options])
Convert Unicode str
into a slug string, making sure it is safe to be used in an URL or in a file name.
Options: (optional)
ignore?: string[];
replace?: OptionReplaceCombined;
replaceAfter?: OptionReplaceCombined;
trim?: boolean;
unknown?: string;
lowercase?: boolean;
uppercase?: boolean;
separator?: string;
allowedChars?: string;
fixChineseSpacing?: boolean;
slugify('你好,世界');
slugify('你好,世界', { lowercase: false, separator: '_' });
slugify('你好,世界', {
replace: { 你好: 'Hello', 世界: 'world' },
separator: '_',
});
slugify('你好,世界', {
replace: [
['你好', 'Hello'],
['世界', 'world'],
],
separator: '_',
});
slugify('你好,世界', { ignore: ['你好'] });
slugify.config([optionsObj], [reset = false])
Bind option object globally so any following calls will use optionsObj
by default. If optionsObj
is not given, it will return current default option object.
slugify.config({ lowercase: false, separator: '_' });
slugify('你好,世界');
console.log(slugify.config());
slugify.config({ replace: [['你好', 'Hello']] });
slugify('你好, world!');
console.log(slugify.config());
slugify.config(undefined, true);
console.log(slugify.config());
CLI Usage
➜ ~ transliterate --help
Usage: transliterate <unicode> [options]
Options:
--version Show version number [boolean]
-u, --unknown Placeholder for unknown characters [string] [default: ""]
-r, --replace Custom string replacement [array] [default: []]
-i, --ignore String list to ignore [array] [default: []]
-S, --stdin Use stdin as input [boolean] [default: false]
-h, --help [boolean]
Examples:
transliterate "你好, world!" -r 好=good -r Replace `,` with `!`, `world` with `shijie`.
"world=Shi Jie" Result: Ni good, Shi Jie!
transliterate "你好,世界!" -i 你好 -i , Ignore `你好` and `,`.
Result: 你好,Shi Jie !
➜ ~ slugify --help
Usage: slugify <unicode> [options]
Options:
--version Show version number [boolean]
-U, --unknown Placeholder for unknown characters [string] [default: ""]
-l, --lowercase Returns result in lowercase [boolean] [default: true]
-u, --uppercase Returns result in uppercase [boolean] [default: false]
-s, --separator Separator of the slug [string] [default: "-"]
-r, --replace Custom string replacement [array] [default: []]
-i, --ignore String list to ignore [array] [default: []]
-S, --stdin Use stdin as input [boolean] [default: false]
-h, --help [boolean]
Examples:
slugify "你好, world!" -r 好=good -r "world=Shi Replace `,` with `!` and `world` with
Jie" `shijie`.
Result: ni-good-shi-jie
slugify "你好,世界!" -i 你好 -i , Ignore `你好` and `,`.
Result: 你好,shi-jie
Caveats
Currently, transliteration
only supports 1 to 1 code map (from Unicode to Latin). It is the simplest way to implement, but there are some limitations when dealing with polyphonic characters. It does not work well with all languages, please test all possible situations before using it. Some known issues are:
-
Chinese: Polyphonic characters are not always transliterated correctly. Alternative: pinyin
.
-
Japanese: Most Japanese Kanji characters are transliterated into Chinese Pinyin because of the overlapped code map in Unicode. Also there are many polyphonic characters in Japanese which makes it impossible to transliterate Japanese Kanji correctly without tokenizing the sentence. Consider using kuroshiro
for a better Kanji -> Romaji conversion.
-
Thai: Currently it is not working. If you know how to fix it, please comment on this issue.
-
Cyrillic: Cyrillic characters are overlapped between a few languages. The result might be inaccurate in some specific languages, for example Bulgarian.
If you find any other issues, please raise a ticket.
License
MIT