Hmmmm…. After a long time nuh… ;) My 2nd
post… On this post I’m going to introduce a JAVA API for Singlish to
Sinhala Unicode transliation or in other words an API to translate
English typed sinhala text to Sinhala Unicode characters with a small
application which build using the API.
This
code + API have some nice features to enhance the usability of this
application. The API can use for applications that need to translate the
user type english text into Sinhala Unicode at the editing place. To
represent consonant modifiers you can use either uppercase letters or
the same lowercase letters. User should use minus symbol to combine
letters (බැඳි අකුරු), for example to type ක්ෂ
letter k-SHA should use. “-“ will be ignored in the translated text and
it may use to combine letter. But if user wants to type “-“ sign, then
two minus letters should use as ”--”. Other features and corresponding
English letters of the API has described in the user guide.
You
can get the java files from the links at the bottom. When considering
about using these classes in applications, there are two main approaches
to use them.
So
I’ll start from the easy way. You can directly add the Sinhala to
English translation capability to your swing application’s text
component, which inherited from JTextComponent by setting the document
filter.
JEditorPane editor = new JEditorPane();
((AbstractDocument) editor.getDocument()).setDocumentFilter(new SinhalaTransliatorFilter());
There is another two possible ways translate.
You can directly translate text by using the static method translate, as
String str = SinhalaTranslator.translate("k K g G X gA ch CH j k-DH”);
This
method has some drawbacks, if you are using the above method you will
have to keep the original english text separately and translate entire
text as user editing the text. Then the next possible and efficent way
is by creating an object of the SinhalaTranslator.
In
this case you can append, insert or delete text. Except to that, the
entire translated text can be retrieved by calling to the getText method
of the SihalaTranslator instance. Or the most recently translated text
portion can be retrieved by using the getReplacedPosition,
getReplacedLength and getTranslatedText methods. If you want to
translate text at the editing place (Translate text at the same place,
where user types, as in the demo application.), these methods will be
useful. The reason is, when user typing text there is still a portion of
text that hasn’t translated. So as user appends new characters, that
non-translated part should also translate as necessary.
SinhalaTranslator trans = new SinhalaTranslator();
trans.appendText(“ae”);
trans.appendText(“eaa”);
String translated = trans.getText();//ඈaa
or
trans.appendText(“ae”);
String str = trans. getTranslatedText();//ae # case 1, replacedLength – 0,
// replacedPosition - 0
trans.appendText(“eaa”);
str = trans. getTranslatedText();// ඈaa # case 2, replacedLength – 2,
// replacedPosition - 0
getReplacedPosition()
returns the replaced position in the text, and the getReplacedLength()
returns the number of chars that replaced in the original text by the
most recent append, delete or insert operation.
For
future enhancements, I think we should add a feature to insert English
text without translating. And there might be some bugs with text
deleting. The following links contain class files and the help document.
If
there are any bugs or suggestions let me know. And on the next post I’m
going to introduce a sinhala – sinhala unicode translation addon for
firefox. Mmmmmm, c ya all in next post…. Byee…….. :D