-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unicode to English literals #7
base: develop
Are you sure you want to change the base?
Changes from 5 commits
b128e93
a98d2c4
dc7823d
9cd31ed
b3d5d1c
c29d558
040668f
964d4db
92506c6
652b67b
7ac59ff
15451a5
ad346b1
c5cbf08
2efd67d
82618e8
2310cce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,21 @@ print(NepaliMoment.fromAD(DateTime.parse('2019-06-02T18:22:14'), | |
// १३ दिन पहिले | ||
``` | ||
|
||
### English literals to Unicode | ||
Converts English literal (Roman Literals) into Nepali Unicode. | ||
```dart | ||
print(NepaliUnicode.convert( | ||
"sayau' thu''gaa fUlakaa haamii, euTai maalaa nepaalii")); | ||
// सयौं थुँगा फूलका हामी, एउटै माला नेपाली | ||
``` | ||
|
||
### Unicode to English iterals | ||
Converts Nepali Unicode into English literals. | ||
```dart | ||
print(NepaliUnicode.convert("सयौं थुँगा फूलका हामी, एउटै माला नेपाली")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct ? |
||
// sayau' thu''gaa fUlakaa haamii, euTai maalaa nepaalii | ||
``` | ||
|
||
|
||
## Example | ||
Find more detailed example [here](https://github.com/sarbagyastha/nepali_utils/tree/master/example/main.dart). | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2020 Sarbagya Dhaubanjar. All rights reserved. | ||
// Use of this source code is governed by a MIT license that can be | ||
// found in the LICENSE file. | ||
|
||
/// Converts Nepali Unicode (Devnagari Literals) into English(Roman) literals. | ||
class UnicodeToEnglish { | ||
static String _text = ''; | ||
|
||
/// Converts specifies [text] into english literals. | ||
/// | ||
/// if live is true, texts will convert in live manner. | ||
/// i.e. as you go on typing | ||
/// Default for live is false. | ||
static String convert(String text, {bool live = false}) { | ||
var splittedString = text.split(''); | ||
sarbagyastha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var convertedString = <String>[]; | ||
sarbagyastha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (var i = 0; i < splittedString.length; i++) { | ||
if (i < splittedString.length - 1 && splittedString[i + 1] == '\u094D') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final <is?> = splittedString[i + 1] == '\u094D'; // give the variable name so that the check you're trying to perform becomes self-explanatory. |
||
var fullStr = _unicodeMap[splittedString[i]].toString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by |
||
fullStr = fullStr.substring(0, fullStr.length - 1); | ||
convertedString.add(fullStr); | ||
} else if (splittedString[i] != '\u094D') { | ||
convertedString | ||
.add(_unicodeMap[splittedString[i]] ?? splittedString[i]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add missing trailing comma |
||
} | ||
} | ||
for (var i = 0; i < convertedString.length; i++) { | ||
if (_symbolMap[convertedString[i]] != null) { | ||
convertedString[i - 1] = convertedString[i - 1].toString(); | ||
if (convertedString[i - 1].endsWith('a')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was the check needed, please add a comment. |
||
convertedString[i - 1] = convertedString[i - 1] | ||
.substring(0, convertedString[i - 1].length - 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
convertedString[i] = _symbolMap[convertedString[i]] ?? ''; | ||
} | ||
} | ||
_text = convertedString.join(''); | ||
return _text; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be return directly. |
||
} | ||
} | ||
|
||
Map<String, String> get _unicodeMap => { | ||
//ka - gya | ||
'\u0915': 'ka', | ||
'\u0916': 'kha', | ||
'\u0917': 'ga', | ||
'\u0918': 'gha', | ||
'\u0919': 'ng', | ||
'\u091A': 'ca', | ||
'\u091B': 'cha', | ||
'\u091C': 'ja', | ||
'\u091D': 'jha', | ||
'\u091E': 'ya', | ||
'\u091F': 'Ta', | ||
'\u0920': 'Tha', | ||
'\u0921': 'Da', | ||
'\u0922': 'Dha', | ||
'\u0923': 'na', | ||
'\u0924': 'ta', | ||
'\u0925': 'tha', | ||
'\u0926': 'da', | ||
'\u0927': 'dha', | ||
'\u0928': 'na', | ||
'\u092A': 'pa', | ||
'\u092B': 'fa', | ||
'\u092C': 'ba', | ||
'\u092D': 'bha', | ||
'\u092E': 'ma', | ||
'\u092F': 'ya', | ||
'\u0930': 'ra', | ||
'\u0932': 'la', | ||
'\u0935': 'wa', | ||
'\u0936': 'sha', | ||
'\u0937': 'sha', | ||
'\u0938': 'sa', | ||
'\u0939': 'ha', | ||
'\u0915\u094D\u0937': 'xa', | ||
'\u0924\u094D\u0930': 'tra', | ||
'\u091C\u094D\u091E': 'gya', | ||
// a - ahm | ||
'\u0905': 'a', | ||
'\u0906': 'aa', | ||
'\u0907': 'i', | ||
'\u0908': 'ii', | ||
'\u0909': 'u', | ||
'\u090A': 'oo', | ||
'\u090F': 'e', | ||
'\u0910': 'ai', | ||
'\u0913': 'o', | ||
'\u0914': 'au', | ||
'\u0935\u0902': 'am', | ||
'\u0935\u0903': 'ah', | ||
|
||
//numbers | ||
'\u0966': '0', | ||
'\u0967': '1', | ||
'\u0968': '2', | ||
'\u0969': '3', | ||
'\u096a': '4', | ||
'\u096b': '5', | ||
'\u096c': '6', | ||
'\u096d': '7', | ||
'\u096e': '8', | ||
'\u096f': '9', | ||
//symbols | ||
'%2c': ',', | ||
}; | ||
Map<String, String> get _symbolMap => { | ||
'\u093E': 'aa', | ||
'\u093F': 'i', | ||
'\u0940': 'ii', | ||
'\u0941': 'u', | ||
'\u0942': 'U', | ||
'\u0947': 'e', | ||
'\u0948': 'ai', | ||
'\u094B': 'o', | ||
'\u094C': 'au', | ||
|
||
'\u0901': "''", //chandrabindu | ||
'\u0902': "'", //shreebindu | ||
}; | ||
sarbagyastha marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import 'package:nepali_utils/nepali_utils.dart'; | ||
import 'package:nepali_utils/src/unicode_to_english.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
|
@@ -164,6 +165,25 @@ void main() { | |
), | ||
); | ||
}); | ||
group('text converter ', () { | ||
final englishText = | ||
"ka baaTa kachuwaa kha kharaayo\nkharaayoko sing' chaina kataa haraayo"; | ||
final nepaliText = 'क बाट कछुवा ख खरायो\nखरायोको सिङं छैन कता हरायो'; | ||
test( | ||
'conversion of english to nepali unicode', | ||
() => expect( | ||
NepaliUnicode.convert('$englishText'), | ||
'$nepaliText', | ||
), | ||
); | ||
test( | ||
'conversion of nepali to english unicode', | ||
() => expect( | ||
UnicodeToEnglish.convert('$nepaliText'), | ||
'$englishText', | ||
), | ||
); | ||
}); | ||
Comment on lines
+168
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests do not belong in this file. |
||
} | ||
|
||
String _format(number) => NepaliNumberFormat().format(number); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please format the code and add a trailing comma.