What is @sindresorhus/transliterate?
@sindresorhus/transliterate is an npm package that provides functionality to transliterate Unicode strings into ASCII. This is particularly useful for converting non-Latin scripts into a Latin-based alphabet, making the text easier to read and process in various applications.
What are @sindresorhus/transliterate's main functionalities?
Basic Transliteration
This feature allows you to transliterate a given Unicode string into its ASCII representation. In this example, the Chinese characters '你好' are transliterated to 'Ni Hao'.
const transliterate = require('@sindresorhus/transliterate');
console.log(transliterate('你好')); // Output: 'Ni Hao'
Custom Replacement Map
This feature allows you to provide a custom replacement map for specific characters. In this example, the custom map is used to transliterate '你好' to 'Nǐ Hǎo' instead of the default 'Ni Hao'.
const transliterate = require('@sindresorhus/transliterate');
const customMap = { '你': 'Nǐ', '好': 'Hǎo' };
console.log(transliterate('你好', { customReplacements: customMap })); // Output: 'Nǐ Hǎo'
Slugify
This feature allows you to create URL-friendly slugs from Unicode strings. In this example, the string '你好, 世界!' is transliterated and slugified to 'ni-hao-shi-jie'.
const transliterate = require('@sindresorhus/transliterate');
console.log(transliterate.slugify('你好, 世界!')); // Output: 'ni-hao-shi-jie'
Other packages similar to @sindresorhus/transliterate
transliteration
The 'transliteration' package provides similar functionality for converting Unicode strings to ASCII. It also supports custom replacement maps and slugification. Compared to @sindresorhus/transliterate, it offers a more extensive set of default transliterations and additional configuration options.
unidecode
The 'unidecode' package is another alternative for transliterating Unicode strings to ASCII. It is based on the Python Unidecode library and provides a straightforward way to convert non-Latin scripts. While it is effective, it may not offer as much customization as @sindresorhus/transliterate.
slugify
The 'slugify' package focuses on creating URL-friendly slugs from strings, including Unicode strings. It provides a range of options for customization and is highly performant. While it specializes in slug creation, it may not offer the same level of general transliteration functionality as @sindresorhus/transliterate.
transliterate
Convert Unicode characters to Latin characters using transliteration
Can be useful for slugification purposes and other times you cannot use Unicode.
Install
$ npm install @sindresorhus/transliterate
Usage
const transliterate = require('@sindresorhus/transliterate');
transliterate('Fußgängerübergänge');
transliterate('Я люблю единорогов');
transliterate('أنا أحب حيدات');
transliterate('tôi yêu những chú kỳ lân');
API
transliterate(string, options?)
string
Type: string
String to transliterate.
options
Type: object
customReplacements
Type: Array<string[]>
Default: []
Add your own custom replacements.
The replacements are run on the original string before any other transformations.
This only overrides a default replacement if you set an item with the same key.
const transliterate = require('@sindresorhus/transliterate');
transliterate('Я люблю единорогов', {
customReplacements: [
['единорогов', '🦄']
]
})
Supported languages
Most major languages are supported.
This includes special handling for:
- Arabic
- Armenian
- Czech
- Danish
- Dhivehi
- Georgian
- German (umlauts)
- Greek
- Hungarian
- Latin
- Latvian
- Lithuanian
- Macedonian
- Pashto
- Persian
- Polish
- Romanian
- Russian
- Serbian
- Slovak
- Swedish
- Turkish
- Ukrainian
- Urdu
- Vietnamese
However, Chinese is currently not supported.
Related