i18n.properties
files, which contain translations for human readable texts together with the view code wherein these texts appear, rather maintaining just one giant i18.properties file that contains the translations for any and all internationalized texts that appear throughout the application. I think the benefits of this idea are clear from a developer's point of view. If you're interested in more information about this concept, then please check out the two blog posts I wrote prior)extend
-method on the constructor you want to inherit from.
_initI18n: function(){
//first, check if we already constructed the i18n model for this class
if (this.constructor._i18Model) {
//we did! Don't do all that work again, just use the existing one.
this.setModel(this.constructor._i18Model, i18n);
return;
}
//check if the view and controller are in the same directory.
//if they are not, then we need to take the possibility into account
//that the view and the controller might both have their own i18n files.
var stack = [];
var controllerClass = this.getMetadata();
var viewName = this.getView().getViewName();
if (controllerClass.getName() !== viewName) {
//if the view name is different from controller name,
//then we assume the view may have its own i18n
//that overrides those of the controller
stack.push(viewName);
}
//walk the chain of inheritance up to sap.ui.core.mvc.Controller
//store each superclass at the front of the stack
var className, rootControllerClassName = "sap.ui.core.mvc.Controller";
while (true) {
className = controllerClass.getName();
if (className === rootControllerClassName) {
break;
}
stack.unshift(className);
controllerClass = controllerClass.getParent();
}
//walk the stack and create a resourcebundle for each class
//use it to enhance this class' i18n model.
stack.forEach(function(className){
var bundleData;
if (window[className] && window[className]._i18Model) {
bundleData = window[className]._i18Model.getResourceBundle();
}
else {
className = className.split(".");
//snip off the local classname to get the directory name
className.pop();
//add i18n to make the i18n directory name
className.push(i18n);
//add i18n to point to the i18n.properties file(s)
className.push(i18n);
bundleData = {bundleName: className.join(".")};
}
var i18nModel = this.getModel(i18n);
if (i18nModel) {
i18nModel.enhance(bundleData);
}
else {
i18nModel = new ResourceModel(bundleData);
this.setModel(i18nModel, i18n);
}
}.bind(this));
//cache the i18n model for new instances of this class.
this.constructor._i18Model = this.getModel(i18n);
}
_initI18n
-method that appeared in my prior blog posts on this topic. It is also assumed that this method sits in some abstract base controller, which you'll extend to create actual concrete controllers for your views._initI18N
-method:sap.ui.core.mvc.Controller
. This is achieved with this snippet:
var stack = [];
...
var className, rootControllerClassName = "sap.ui.core.mvc.Controller";
while (true) {
className = controllerClass.getName();
if (className === rootControllerClassName) {
break;
}
stack.unshift(className);
controllerClass = controllerClass.getParent();
}
forEach
-iteration of the stack then constructs the resource bundle to enhance the i18n model in the usual way.forEach
-array method will encounter the class names in the desired order, allowing the subclasess to override texts added by superclasses.
var stack = [];
var controllerClass = this.getMetadata();
var viewName = this.getView().getViewName();
if (controllerClass.getName() !== viewName) {
//if the view name is different from controller name,
//then we assume the view may have its own i18n
//that overrides those of the controller
stack.push(viewName);
}
_initI18n
-method:
//first, check if we already constructed the i18n model for this class
if (this.constructor._i18Model) {
//we did! Don't do all that work again, just use the existing one.
this.setModel(this.constructor._i18Model, i18n);
return;
}
...
...
...
//cache the i18n model for new instances of this class.
this.constructor._i18Model = this.getModel(i18n);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
5 | |
4 | |
3 | |
3 | |
3 | |
3 | |
2 | |
2 |