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'.
Transliteration
Transliteration module for node.js. It converts special characters in unicode text into corresponding ascii letters, with support of nearly every common languages including CJK (Chinese, Japanese and Korean).
Install
npm install transliteration
Usage
transliteration(str, [unknown])
Transliterate the string str
. Characters which this module doesn't recognise will be converted to the character in the unknown
parameter, defaults to ?
.
Example
var tr = require('transliteration');
tr('你好,世界');
tr('Γεια σας, τον κόσμο');
tr('안녕하세요, 세계');
slugify(str, options)
Converts unicode string to slugs. So it can be safely used in URL or file name.
Options:
{
lowercase: true,
separator: '-'
}
If no options
parameter provided it will use the above default values.
Example:
var slugify = require('transliteration').slugify;
slugify('你好,世界');
slugify('你好,世界', {lowercase: false, separator: '_'});
Client side usage
Transliteration module can be run in the browser as well.
Donload the library with bower:
bower install transliteration
It supports AMD / CommonJS standard or just to be loaded as a global variable.
When use in the browser, by default it will create global variables under window
object:
TR('你好, World');
Transliteration('String');
If you don't like the default variable names or they conflict with other libraries, you can call noConfilict() method before loading other libraries, then both window.TR
and window.Transliteration
will be deleted from windows object and Transliteration function will be returned:
var trans = Transliteration.noConflict();
trans('你好, World');
trans.slugify('你好, World');
For detailed usage, please check example.html.