Nepali Unicode
NepaliUnicode converts Latin-script text into Nepali Unicode (Devanagari), and back. Use it to transliterate romanized Nepali, or to add Nepali typing to a text field without a Nepali keyboard.
const unicode = NepaliUnicode();
unicode.convert("sayau' thu''gaa fUlakaa haamii, euTai maalaa nepaalii");// सयौं थुँगा फूलका हामी, एउटै माला नेपालीunicode.convert('saarwabhauma bhai failiekaa, mecii-mahaakaalii');// सार्वभौम भै फैलिएका, मेची-महाकालीunicode.convert('mero naam {Sarbagya} ho'); // मेरो नाम Sarbagya होunicode.toRoman('नेपाली'); // nepaaliiNepaliUnicode is a const, immutable Converter<String, String>:
const NepaliUnicode({NepaliUnicodeStyle style = .classic, bool nepaliDigits = true})Styles
Section titled “Styles”Choose the romanization scheme your users already know:
| Style | Typing नेपाल |
Kind |
|---|---|---|
.classic (default) |
nepaala |
Phonetic. These are the rules of earlier versions of this package. |
.google |
nepaal |
Phonetic, in the style of Google Input Tools and Easy Nepali typing. |
.mppRomanized |
nepal |
Madan Puraskar Pustakalaya’s Nepali Unicode Romanized keyboard. Each key types one character. |
.mppTraditional |
g]kfn |
Madan Puraskar Pustakalaya’s Nepali Unicode Traditional keyboard, with the same key positions as Preeti. |
const NepaliUnicode().convert('nepaala'); // नेपालconst NepaliUnicode(style: .google).convert('nepaal ko maayaa'); // नेपाल को मायाconst NepaliUnicode(style: .mppRomanized).convert('nepal'); // नेपालconst NepaliUnicode(style: .mppTraditional).convert('g]kfn'); // नेपालClassic
Section titled “Classic”A consonant keeps a halant (्) until a vowel follows it. Before whitespace, the halant is dropped.
const classic = NepaliUnicode();
classic.convert('nepal'); // नेपल्classic.convert('nepaala'); // नेपालclassic.convert('kamal'); // कमल्classic.convert('kamal '); // कमल (a space drops the halant)classic.convert('khasha'); // खशclassic.convert('Tha Da kaaraNa Sa'); // ठ ड कारण षclassic.convert('ma ghara jaanchu.'); // म घर जान्छु।| Type | Get |
|---|---|
aa or A |
ा |
ii or I |
ी |
uu or U |
ू |
ai or E |
ै |
au |
ौ |
T, D, N, S |
ट, ड, ण, ष |
consonant + h |
aspirated, e.g. kh ख, sh श |
' |
ं (anusvara) |
'' |
ँ (chandrabindu) |
: |
ः (visarga) |
., |, / or \ |
। |
A consonant at the end of a word keeps its inherent vowel, and ~ forces a halant:
const google = NepaliUnicode(style: .google);
google.convert('nepaal ko maayaa, bhagawaan~ ko dayaa');// नेपाल को माया, भगवान् को दयाgoogle.convert('chhoraa chiyaa shaanti Shatkon kshetra gyaan');// छोरा चिया शान्ति षत्कोन क्षेत्र ज्ञानgoogle.convert('saMsaar haa^s duHkha kRshi');// संसार हाँस दुःख कृशि| Type | Get |
|---|---|
ch / chh |
च / छ |
sh / Sh |
श / ष |
ksh / gy |
क्ष / ज्ञ |
ee or ii / oo or uu |
ी / ू |
M / ^ / H |
ं / ँ / ः |
R |
ृ |
~ |
् (halant) |
. |
। |
MPP keyboards
Section titled “MPP keyboards”The keyboard styles map each key to one character, or to a fixed conjunct. Nothing is combined. They suit users who learned these layouts.
const NepaliUnicode(style: .mppRomanized).convert('nmste'); // नमसतेconst NepaliUnicode(style: .mppRomanized).toRoman('नमस्ते'); // nms/teTo read documents that were typed in the Preeti font, use Preeti.toUnicode instead of .mppTraditional.
Keeping text as it is: {…}
Section titled “Keeping text as it is: {…}”In the phonetic styles (classic and google), text inside braces isn’t converted, and the braces are removed:
const NepaliUnicode().convert('mero naam {Sarbagya} ho'); // मेरो नाम Sarbagya होTo write a literal {, escape it as {{}:
const NepaliUnicode().convert('{{}a}'); // {अ}Digits
Section titled “Digits”ASCII digits become Devanagari digits by default. Pass nepaliDigits: false to keep them:
const NepaliUnicode().convert('2081 saala'); // २०८१ सालconst NepaliUnicode(nepaliDigits: false).convert('2081 saala'); // 2081 सालMixing Devanagari and Latin
Section titled “Mixing Devanagari and Latin”Devanagari that is already in the input is kept. Latin letters typed right after it join its last syllable:
const NepaliUnicode().convert('कमलa'); // कमलाconst NepaliUnicode().convert('नेपालi'); // नेपालैA Devanagari consonant counts as a consonant plus its inherent a. So adding a gives ा, and adding i gives ै (a + i = ai).
Typing in a text field: convertLive
Section titled “Typing in a text field: convertLive”To convert while the user types, call convertLive on the field’s whole text after every change, and put the result back:
const unicode = NepaliUnicode(style: .google);
unicode.convertLive('k'); // क्unicode.convertLive('क्a'); // कunicode.convertLive('कm'); // कम्unicode.convertLive('कम् '); // कम (followed by a space)convertLive is different from convert in three ways:
- The word being typed keeps its halant until it’s finished, so the next letter can still join it.
- A halant just before whitespace is dropped.
{…}escapes keep their braces, so text typed later still knows it is inside an escape. Callconverton the final text to remove them.
A minimal Flutter version:
final controller = TextEditingController();const unicode = NepaliUnicode(style: .google);
TextField( controller: controller, onChanged: (text) { final converted = unicode.convertLive(text); if (converted != text) { controller.value = TextEditingValue( text: converted, selection: TextSelection.collapsed(offset: converted.length), ); } },);For the keyboard styles, convertLive is the same as convert.
Back to Latin: toRoman
Section titled “Back to Latin: toRoman”toRoman romanizes Devanagari so that convert gives the same text back:
const NepaliUnicode().toRoman('नेपाल'); // nepaalaconst NepaliUnicode(style: .google).toRoman('नेपाल'); // nepaalconst NepaliUnicode(style: .google).toRoman('क्षत्रिय'); // kshatriyconst NepaliUnicode().toRoman('मेरो नाम Sarbagya हो'); // mero naam {Sarbagya} hoconst NepaliUnicode(style: .mppTraditional).toRoman('नेपाल'); // g]kfn- Latin text is escaped in
{}. - In the phonetic styles, a separator is added between letters that would otherwise be read as a different letter:
{}in classic,~in google. - The keyboard styles return the keys to press.
- Characters the style can’t write, such as ळ in the phonetic styles, are left as they are.
Streams and chunked input
Section titled “Streams and chunked input”NepaliUnicode is a Converter, so it can transform a stream:
final text = await Stream.fromIterable(['mero naa', 'm nepaal', 'ii ho']) .transform(const NepaliUnicode()) .join();print(text); // मेरो नाम नेपाली होEach chunk is converted up to its last whitespace. The rest is held back until the next chunk arrives, so a word split across chunks still converts correctly.
