Localization in Ext JSCreating an application that works is one thing; creating an application that works for your users is something very different. Communicating with users in a language that they understand and with conventions that they're used to is vital. Imagine this scenario, you hand your phone to a friend in good faith but when they return it, everything's in Japanese. Frustrated, you try to remember which combination of buttons leads you to the Settings menu so you can change it back, navigating through, you realize that menus slide in the opposite direction, maybe even the color scheme is different. You start to realize just how important language and cultural conventions are and how disorienting it is when faced with a localization setting that wasn't meant for you. Now imagine your users, wanting to use your Ext JS application but feeling the same confusion and unsure of what's being asked of them. To fix this, we go through a process known as 'localization' (sometimes called l10n). A large part of localization is translation and, thankfully, Ext JS makes it easy to localize your application. Ext's Localization FilesIn the root directory of your copy of Ext JS there is a folder called if (Ext.toolbar.Paging){
Note: The You can see that it checks to see if a Paging toolbar is in use, and if it is, applies the Spanish strings to each area text is shown. If you have custom text areas you, can append them here as well with the appropriate translations. You'll also notice that it is setting these properties to the Paging Toolbar's prototype. The upshot of this is that every new Paging Toolbar that is created will inherit these translated properties. Utilizing LocalizationThere are two ways you could implement localization in your application: statically or dynamically. We're going to look at how to do it dynamically so users can choose which language they're most familiar with. First, we're going to create a Combobox where users will select their language and secondly, we'll deduce the language from the URL so if a user visits http://yoursite.com/?lang=es the Spanish version of your Ext application is used. Set up a basic HTML page with links to Ext JS's necessary parts and our localized application's languages.js and app.js files. <!DOCTYPE html> We have two separate JavaScript files: the first will be a list of all the languages that Ext JS comes with, the second will be the application itself. We've also set up two Now create a file called Ext.namespace('Ext.local');
This is all the languages file will consist of but will serve as a useful reference for our Ext JS application. Next, we'll start building the application itself. Using the module pattern, we will have four methods: Ext.Loader.setConfig({enabled: true});
To create the combobox that will contain all the possible language selections we first need to create an array store in the var store = Ext.create('Ext.data.ArrayStore', {
This is a very simple store that contains the two fields that correspond to the two values for each record in the Now create the combobox itself, again, within the var combo = Ext.create('Ext.form.field.ComboBox', {
If you refresh your browser, you should see a combobox that, when clicked, shows a list of languages bundled with Ext JS. When one of these languages is selected, the page refreshes and appends
After the creation of the combobox, we're going to check to see if any language has been previously selected and act accordingly by checking the URL with Ext's urlDecode function. var params = Ext.urlDecode(window.location.search.substring(1)); Note: We're loading the files with an AJAX request, so the files will have to be uploaded to a server otherwise they'll fail to load due to browser security measures. Here you can see why we have the onFailure: function() {
Ext.Msg.alert('Failure', 'Failed to load locale file.');
this.setup();
},
The
onSuccess: function(response) {
eval(response.responseText);
this.setup();
},
The AJAX call that we made returns a few parameters. We use JavaScript's However, there's nothing in
setup: function() {
Ext.create('Ext.FormPanel', {
renderTo: 'datefield',
frame: true,
title: 'Date picker',
width: 380,
defaultType: 'datefield',
items: [{
fieldLabel: 'Date',
name: 'date'
}]
});
}
Now, if you click on the calendar icon you'll see the month in the specified language as well as the first letter of each day.
To show more of Ext JS's localization features we'll now create an e-mail field and a month browser. Inside the setup method, write the following:
Ext.create('Ext.FormPanel', {
renderTo: 'emailfield',
labelWidth: 100,
frame: true,
title: 'E-mail Field',
width: 380,
defaults: {
msgTarget: 'side',
width: 340
},
defaultType: 'textfield',
items: [{
fieldlabel: 'Email',
name: 'email',
vtype: 'email'
}]
});
var monthArray = Ext.Array.map(Ext.Date.monthNames, function (e) { return [e]; });
var ds = Ext.create('Ext.data.Store', {
fields: ['month'],
remoteSort: true,
pageSize: 6,
proxy: {
type: 'pagingmemory',
data: monthArray,
reader: {
type: 'array'
}
}
});
Ext.create('Ext.grid.Panel', {
renderTo: 'grid',
width: 380,
height: 203,
title:'Month Browser',
columns:[{
text: 'Month of the year',
dataIndex: 'month',
width: 240
}],
store: ds,
bbar: Ext.create('Ext.toolbar.Paging', {
pageSize: 6,
store: ds,
displayInfo: true
})
});
// trigger the data store load
ds.load();
Remember that Notice that when typing in fields, a warning icon is displayed that, when hovered over, reveals context-specific information in the native language as a tooltip.
An excellent example of what localization means beyond translation can be seen by selecting Polish and seeing how the order of the date field changes from DD-MM-YYYY to YYYY-MM-DD. Another is selecting Finnish and seeing how instead of dashes (-), periods (.) are used to separate day from month from year and the months are not capitalized. It's details like this that Ext takes care for you with it's comprehensive locale files. ConclusionIn this tutorial we have looked at how to load different locale files included with Ext JS by using AJAX requests that reload the application in the desired language along with subtle cultural conventions. Your users will benefit from a more native experience and appreciate the extra lengths that you've gone to to ensure a better experience. |
ExtJS 4 指南Ext Core手册资料快速下载
概技术普及,始能进步。稍有累积,颇愿意与大家分享。
本站内容无论翻译或非翻译,除绝少声明转载的外,大多均属原创。 转载时请注明本页信息及“Ext中文网 ajaxjs.com”。转载内容请遵循 Creative Commons 署名-非商业性使用 2.5 方可。
|