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. Transliterate unicode characters into latin ones. Supports all common unicode characters including CJK.
Install
npm install transliteration
Usage
transliterate(str, unknown)
Transliterate str
. Unknown characters will be converted to unknown
Example
var transliteration = require('transliteration');
transliteration.transliterate('你好,世界');
transliteration.transliterate('Γεια σας, τον κόσμο');
transliteration.transliterate('안녕하세요, 세계');
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: '-'
}
Leave it blank to use the above default values.
Example
var transliteration = require('transliteration');
transliteration.slugify('你好,世界');
transliteration.slugify('你好,世界', {lowercase: false, separator: '_'});