I decided to create an "English.js" file in a folder called "resources" (though you can call it "locale" or whatever you want). In this file I created an object containing all my custom strings that would need to be translated which looked a little something like below:
window.applanguage = {
/*General form elements*/
login : "Login",
OK: "OK",
changepassword: "Change password",
currentpassword: "Current password",
sam: "System Access Manager",
userid: "User ID",
adminid: "Admin ID",
email: "Email",
password: "Password",
newpassword: "New password",
confirmpassword: "Confirm password",
confirm: "Confirm",
confirmation: "Confirmation",
wentwrong: "Something went wrong",
username: "Username",
passvalidity: "Password Validity (days)",
product: "Product",
accesslevel: "Access Level",
timeoutmins: "Timeout (mins)",
cancel: "Cancel",
save: "Save",
reset: "Reset",
passwordutility: "Change password utility",
expireform: "Session expired, please log in to continue.",
adduser: "Add user",
edituser: "Edit user",
removeuser: "Remove user",
resetuser: "Reset user",
add: "Add"
};
Wherever I needed my custom string translated I just replaced it with window.applanguage.string_to_translate
. I.E:
Ext.Msg.show({
closable: false,
title: window.applanguage.info,
msg: window.applanguage.selectuserfirst,
buttons: Ext.Msg.OK,
icon: Ext.Msg.INFO
});
Now if you wanted your application in French (although tedious to do) you can copy the "English.js" file and name is "French,js" and change all the string to french.
NB: Don't forget to include your language file in the <header>
of your web file. You can dynamically change this by having a global in PHP (I set mine in the ZF application.ini file) with the language to you want to display and then in your header you can then have the line:
<script type="text/javascript" src="../extjs/resources/<?php echo $languageFile; ?>.js"></script>
Translation Tip 2:
If you needed to translate all the Ext JS components to language other than English you can include the EXT JS locale file in your header
. For example if you want French:
<script type="text/javascript" src="../ext-4.2.0/locale/ext-lang-fr.js"></script>
Using the dynamic PHP option it would look something like this (This is for ZF but you could just use a global variable in your app):
// get params from application.ini
$config = new Zend_Config_Ini('../application/configs/application.ini', 'development');
$langFile = $config->tranlation->language->file;
$extLang = null;
switch($langFile){
case 'English':
$extLang= "ext-lang-en_GB";
break;
case 'French':
$extLang= "ext-lang-fr";
break;
case 'Spanish':
$extLang= "ext-lang-es";
break;
case 'German':
$extLang= "ext-lang-de";
break;
case 'Chinese (Simplified)':
$extLang= "ext-lang-zh_CN";
break;
case 'Chinese (Traditional)':
$extLang= "ext-lang-zh_TW";
break;
}
and then in your <header>
place this:
<script type="text/javascript" src="../ext-4.2.0/locale/<?php echo $extLang; ?>.js"></script>