I want to keep the directory and file structure for a Karma lesson as simple as possible. Ideally, the html markup, css, and javascript should not be locale-specific. All translated text should be in .po files. I have been looking around at different i18n strategies for web sites and they all appear to only facilitate i18n for text, not for audio neither for right-to-left reading layouts. I haven’t looked carefully at Adobe Flex’s i18n mechanism. Perhaps it provides support for those aspects of i18n.
I have done some research on i18n support for websites and desktop software. I want to thank Sayamindu Dasgupta for being extremely helpful. He sent me many links but the best is perhaps the GNU manual on Gettext.
Various i18n frameworks handle i18n text quite well but they all seem to have 3 significant holes.
1. They lack native digit support. Most people don’t know this but many countries use different characters for the digits 0-9
For example, in Nepali script the digit 5 is ५ . Javascript lets you print ५ as a string, but there is no obvious way to do the following:
var myScore = 555;
$(“scoreBox”).html(myscore);
where myscore displays using the Nepali digits ५५५
2. They lack an easy way support to right-to-left reading layouts. It seems to me that most web frameworks require you to use different css files for different locales. This isn’t very elegant.
3. Sound — No frameworks have an easy mechanism to link to different sound files depending on locale. This doesn’t map seamlessly to the gettext paradigm as .po files would have to hold links to external sound files. It isn’t feasible to embed binary sound files directly into .po files.
Here are some ideas I have for the above problems. Please comment!
Supporting Native Digits
I hoped their would already be simple javascript library for this but they all seems to focus on changing the periods and commas according to locale, such as jquery-i18n. Sayamindu directed me to this feature of GlibC which does appear to convert the digits to the correct localized character. I still haven’t dug into the GLibC library to understand how it works its magic. I the meantime I have a cruder idea on how to output native digits.
I could write simple javascript method that parses the digits to be printed and adds each digit to the unicode base for a given language. For example,
Devanagari numerals (Nepali, Hindi) use the range 0966 – 096F for 0-9 and Arabic uses 06F0-06F9. I could simply add the base for the particular writing script and print out the result as a unicode string.
function kL10nDigit (digit, lang) {
baseNoLang = lookupBaseNumber(lang);
var myDigit = baseNoLang + digit;
return “\u” + myDigit;
}
var myscore = 555;
var listMyscore = makeIntoList(myscore);
$(“scoreBox”).html($.map(myscore, kL10nDigit));
The map function here returns the 555 as the unicode string “\u0970\u0970\u0970” or ५५५ and $(“scoreBox”).html(….) sets the content of the scorebox to that value.
Some Nepalis I have met argue that Nepali kids don’t really need to learn the Devaganari digits and I have met others who feel very passionately that it is essential. This is just another example of how many people have very different but strongly held beliefs about what seem like a small detail to many of us. As software developers, it isn’t my role to decide what is taught but to empower educators and communities to make those decisions for themselves.
Right-to-Left Layouts
Most websites seem to use separate CSS stylesheets when changing between left-to-right and right-to-left layouts. I would really like to avoid this if at all possible. Redundant stylesheets means redundant code, which means codebase fragmentation and more room for errors to creep in. I would really like to label a set of HTML elements and be able to simply reverse the order according to locale.
For Example:
The main CSS file — karma.css
.horizontal_elements { direction: ltr; }
The HTML — index.html
<div class=”horizontal_elements”>
<button id=”btn1″ /> <button id=”btn2″ /> <button id=”btn3″ />
</div>
For a right-to-left script, I want to reverse the order of the buttons but not the order of all the elements on the page. That would turn the page effectively upside down. Ideally, I could just change the direction property in .horizontal_elements to “rtl” . Unfortunately that doesn’t work and I don’t know why.
I could write a simple javascript method that checks if the locale is right-to-left and if true reverses the order of all elements w/in a div elemen having the class “horizontal_elements” . This is feasible but it would be much nicer to do it by changing the “direction” property for the <div> element.
Sound
Sorry, don’t have time to write about that this morning! next blog post 😉
Miscellaneous
GNU gettext seems to assume that the first version of a piece of software will have an English language locale. Can we use the html element id as the msgid in the .pot file?
What about text that shouldn’t be locale specific? for example, portions of an English lesson that are meant to be in English regardless of the locale?
A key priority, trying to adhere as closely as possible to the Model-View-Controller design pattern. Why? Because it is easy to read and understand code that follows MVC and this pattern makes lets teams easily divide up work between less technical designers and their programmer colleagues.
UPDATE #1:
Thanks to Guy Sheffer and Tzafrir Cohen for showing me how to use the “dir” attribute in <html dir=”rtl”> to set the text direction for document. That certainly works for buttons but doesn’t seem to work for <label> or <span> elements. Will have to investigate further.
UPDATE #2:
I have found that <label> and <span> element do change direction properly is the language embedded in them contains text written RTL instead of English, which I was using.