reactioncommerce/transliteration

Name: transliteration

Owner: Reaction Commerce

Description: UTF-8 to ASCII transliteration / slugify module for node.js, browser, Web Worker, React Native, Electron and CLI.

Forked from: callthemonline/transliteration

Created: 2017-07-12 13:41:13.0

Updated: 2017-07-12 13:41:14.0

Pushed: 2017-06-25 00:06:24.0

Homepage: http://rawgit.com/andyhu/transliteration/master/demo/example.html

Size: 1176

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Transliteration

Build Status Dependencies Dev Dependencies Coverage Status NPM Version Bower Version NPM Download License PRs

Sauce Test Status

Transliteration / slugify module for node.js, browser, Web Worker, ReactNative and CLI. It provides the ability to transliterate UTF-8 characters into corresponding pure ASCII; so they can be safely displayed, used as URL slugs or file names.

Demo

example.html

Installation
Node.js
install transliteration --save
rt { transliterate as tr, slugify } from 'transliteration';

??, world!'); // Ni Hao , world!
ify('??, world!'); // ni-hao-world
Browser

CDN:

ipt src="https://unpkg.com/transliteration/lib/browser/transliteration.min.js"></script>

Bower:

stall bower if not already installed
m install bower -g
r install transliteration
l>
d>
cript src="bower_components/transliteration/transliteration.min.js"></script>
ad>
y>
cript>
transl('??, world!'); // Ni Hao , world!
slugify('??, world!'); // ni-hao-world
script>
dy>
ml>
Browser support

transliteration has a good browser compatibility with all major browsers (including IE 6-8 if used with es5-shim).

CLI
install transliteration -g

sliterate ?? # Ni Hao
ify ?? # ni-hao
 ?? | slugify -S # ni-hao
ReactNative
rt { transliterate, slugify } from 'transliteration/src/main/browser';
Breaking changes
1.5.0

Since version 1.5.0, transliteration module requires minimum node version v6.0.

1.0.0

Please note that the code has been entirely refactored since version 1.0.0. Be careful when you plan to upgrade from v0.1.x or v0.2.x to v1.0.x

Changes:

Usage
transliterate(str, [options])

Transliterates the string str and return the result. Characters which this module doesn't recognise will be defaulted to the placeholder from the unknown argument in the configuration option, defaults to [?].

Options: (optional)


 Unicode characters that are not in the database will be replaced with `unknown` */
known: '[?]', // default: [?]
 Custom replacement of the strings before transliteration */
place: { source1: target1, source2: target2, ... }, // Object form of argument
place: [[source1, target1], [source2, target2], ... ], // Array form of argument
 Strings in the ignore list will be bypassed from transliteration */
nore: [str1, str2] // default: []

transliterate.config([optionsObj])

Bind options globally so any following calls will be using optoinsObj by default. If optionsObj argument is omitted, it will return current default option object.

sliterate.config({ replace: [['??', 'Hello']] });
sliterate('??, world!'); // Result: 'Hello, world!'. This equals transliterate('??, world!', { replace: [['??', 'Hello']] });

Example

rt { transliterate as tr } from 'transliteration';
?????'); // Ni Hao , Shi Jie
???? ???, ??? ?????'); // Geia sas, ton kosmo
?????, ??'); // annyeonghaseyo, segye
?????', { replace: {?: 'You'}, ignore: ['?'] }) // You ?, Shi Jie
?????', { replace: [['?', 'You']], ignore: ['?'] }) // You ?, Shi Jie (option in array form)
r use configurations
onfig({ replace: [['?', 'You']], ignore: ['?'] });
?????') // You ?, Shi Jie
et configurations
ole.log(tr.config());
slugify(str, [options])

Converts Unicode string to slugs. So it can be safely used in URL or file name.

Options: (optional)


 Whether to force slags to be lowercased */
wercase: false, // default: true
 Separator of the slug */
parator: '-', // default: '-'
 Custom replacement of the strings before transliteration */
place: { source1: target1, source2: target2, ... },
place: [[source1, target1], [source2, target2], ... ], // default: []
 Strings in the ignore list will be bypassed from transliteration */
nore: [str1, str2] // default: []

If options is not provided, it will use the above default values.

slugify.config([optionsObj])

Bind options globally so any following calls will be using optoinsObj by default. If optionsObj argument is omitted, it will return current default option object.

ify.config({ replace: [['??', 'Hello']] });
ify('??, world!'); // Result: 'hello-world'. This equals slugify('??, world!', { replace: [['??', 'Hello']] });

Example:

rt { slugify } from 'transliteration';
ify('?????'); // ni-hao-shi-jie
ify('?????', { lowercase: false, separator: '_' }); // Ni_Hao_Shi_Jie
ify('?????', { replace: {??: 'Hello', ??: 'world'}, separator: '_' }); // hello_world
ify('?????', { replace: [['??', 'Hello'], ['??', 'world']], separator: '_' }); // hello_world (option in array form)
ify('?????', { ignore: ['??'] }); // ??shi-jie
r use configurations
ify.config({ lowercase: false, separator: '_' });
ify('?????'); // Ni_Hao_Shi_Jie
et configurations
ole.log(slugify.config());
Usage in browser

transliteration can be loaded as an AMD / CommonJS module, or as global variables (UMD).

When using it in the browser, by default it will create global variables under window object:

sl('??, World'); // window.transl
r
ify('Hello, ??'); // window.slugify

If the variable names conflict with other libraries in your project or you prefer not to use global variables, use noConfilict() before loading libraries which contain the conflicting variables.:

Load the library globally

tr = transl.noConflict();
ole.log(transl); // undefined
??, World'); // Ni Hao , World
slug = slugify.noConfilict();
('??, World'); // ni-hao-world
ole.log(slugify); // undefined
Usage in command line
 transliterate --help
e: transliterate <unicode> [options]

ons:
version      Show version number                                                       [boolean]
, --unknown  Placeholder for unknown characters                        [string] [default: "[?]"]
, --replace  Custom string replacement                                     [array] [default: []]
, --ignore   String list to ignore                                         [array] [default: []]
, --stdin      Use stdin as input                                     [boolean] [default: false]
, --help     Show help                                                                 [boolean]

ples:
ansliterate "??, world!" -r ?=good -r          Replace `,` into `!` and `world` into
orld=Shi Jie"                                     `shijie`.
                                                  Result: Ni good, Shi Jie!
ansliterate "?????!" -i ?? -i ?           Ignore `??` and `?`.
                                                  Result: ???Shi Jie !
                                                  Result: ??,world!

 slugify --help
e: slugify <unicode> [options]

ons:
version        Show version number                                                     [boolean]
, --lowercase  Use lowercase                                           [boolean] [default: true]
, --separator  Separator of the slug                                     [string] [default: "-"]
, --replace    Custom string replacement                                   [array] [default: []]
, --ignore     String list to ignore                                       [array] [default: []]
, --stdin      Use stdin as input                                     [boolean] [default: false]
, --help       Show help                                                               [boolean]

ples:
ugify "??, world!" -r ?=good -r "world=Shi     Replace `,` into `!` and `world` into
e"                                                `shijie`.
                                                  Result: ni-good-shi-jie
ugify "?????!" -i ?? -i ?                 Ignore `??` and `?`.
                                                  Result: ???shi-jie
Caveats

transliteration supports almost all common languages whereas there might be quirks in some specific languages. For example, Kanji characters in Japanese will be transliterated as Chinese Pinyin. I couldn't find a better way to distinguish Chinese Hanzi and Japanese Kanji. So if you would like to romanize Japanese Kanji, please consider kuroshiro.

If you find any issues, please raise a GitHub issue. Thanks!

License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.