prettify

baseline

Javascript i18n (internationalization)

The users of a web app should be able to select the interface language of preference. The default language is english and the developer has to give a list of the terms used along with the translations in all languages supported.
Each term can be
Each user selects a UI language that is stored in: ins.oUser.eUsrLang
Enumeration ins.enLng contains all the languages our system supports.
If a specified term is not supported, in the language that the user has chosen, then the english term is used.
if (typeof ins !== "object") ins = {};
ins.enLang = { el: 0, sp: 1, de: 2, fr: 3, it: 4, ru: 5 };
ins.Msgs = { "Add": ["Καταχώρηση", "Añadir", "", "Ajouter"]
    , "Add New": ["Καταχώρηση", "Añadir", "", "Ajouter"]
    , "Comment": ["Παρατήρηση", "Comentario", "Kommentare", "Feedback"]
    , "Cancel": ["Άκυρο", "Cancelación", "", "Annuler"]
    , "Contact": ["Επικοινωνία", "Comunicación", "Kontakt", "Contacter"]
    , "CRM": ["Διαχείριση Συνεργατών"]
    , "Delete": ["Διαγραφή", "Eliminar", "", "Supprimer"]
    , "Delete?": ["Να διαγραφεί;", "Eliminar?", "Löschen?", "Supprimer?"]
    , "Delete link": ["Διαγραφή σχέσης"]
    , "Edit": ["Τροποποίηση", "Editar", "Änderung", "Modifier"]
    , "End": ["Λήξη", "Final", "Ende", "Fin"]
};
ins.Msgs.push = function () {
    var o;
    for (var nArg = 0; nArg <= arguments.length - 1; nArg++) {
        o = arguments[nArg];
        for (var key in o) {ins.Msgs[key] = o[key];}
    }
};
ins.Msg = function (sTermEn, aMsgs) {
    var sMsg;
    if (typeof aMsgs === "undefined") { aMsgs = ins.Msgs;}
    if (ins.oUser.eUsrLng === "en" ||
        typeof aMsgs === "undefined" || 
        typeof aMsgs[sTermEn] === "undefined" || 
        typeof aMsgs[sTermEn][ins.enLang[ins.oUser.eUsrLang]] === "undefined") {
        return sTermEn;
    } else {
        return aMsgs[sTermEn][ins.enLang[ins.oUser.eUsrLang]];
    }
};
Usage:
ins.Msgs.push({
    "App #": ["# Αίτησης"] 
    ,"Application #": ["# Αίτησης"] 
    ,"Beneficiary": ["Δικαιούχος"] 
    ,"Branch": ["Κλάδος"] 
    ,"Comments": ["Παρατηρήσεις"] 
    ,"Company": ["Ασφαλιστική"] 
    ,"Customers": ["Πρόσωπα"] 
});
oDiv.innerHTML = ins.Msg("Branch");
Updated:2010.12.14

No comments:

Post a Comment