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 can be used to transliterate unicode text into corresponding ascii characters, with support of nearly every commong languages including CJK (Chinese, Japanese and Korean).
Install
npm install transliteration
Usage
transliteration(str, [unknown])
Transliterate str
. Characters which this module cannot recognise will be converted to the unknown
parameter, defaults to ?
.
Example
var tr = require('transliteration');
tr('你好,世界');
tr('Γεια σας, τον κόσμο');
tr('안녕하세요, 세계');
slugify(str, options)
Convert unicode string to slugs. It can be savely used in URL or file name.
You can provide an options
parameter in the form of
{
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 run in browser as well. You can download it using bower:
bower install transliteration
It can be loaded as AMD / CommonJS component or 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 name or the variable names 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:
var trans = Transliteration.noConflict();
trans('你好, World');
trans.slugify('你好, World');
Please check example.html for a quick demo.