.properties
files, which are essentially lists of key/value pairs, one per line, and separated by an equals sign:
greetingAction=Say Hello
greeting=Hello {0}!
To actually setup a i18n model using these texts, you can explicitly instantiate a sap.ui.model.resource.ResourceModel
:
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/resource/ResourceModel"
], function(Controller, ResourceModel){
"use strict";
return Controller.extend("my.app.App", {
onInit: function(){
var i18nModel = new ResourceModel({
bundleName: "just.bi.apps.JustBiApp.i18n.i18n"
});
this.getView().setModel(i18nModel, "i18n")
}
});
});
Or, you can have your application instantiate the model by listing it in the models property of the sap.ui5 entry in the manifest.json
application descriptor file:
"models": {
"i18n": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "just.bi.apps.JustBiApp.i18n.i18n"
}
}
}
In many cases, the text is required for the static labels of ui elements like input fields, menus and so on. Inside a view, static texts may be retrieved from the i18n model through special data binding syntax, like so:
<Button text="{i18n>greetingAction}"/>
.getText()
getter on the resource bundle object. The resource bundle which may be obtained from the ResourceModel
object using the .getResourceBundle()
getter:var bundle = this.getModel("i18n").getResourceBundle();
var text = bundle.getText("greeting", ["World"]);
.properties
file for each locale that you want to support. The framework discovers which locale is required by the client and use that to find the best matching i18n .properties
files appropriate for the client's locale.i18n_de.properties
file, and the English texts in a i18n_en.properties
file, and if you want to distinguish between British and American English, you'd create both a i18n_en_GB.properties
and i18n_en_US.properties
file.i18n .properties
files apply, but from what I've seen so far I think it's safe to assume that you can use the two-letter lowercase ISO 639-1 code for the language and the two-letter uppercase ISO 3166-1 code for the country).properties
files (one for each locale).
sap.ui.define([
"sap/ui/core/mvc/Controller",
"sap/ui/model/resource/ResourceModel"
], function(Controller, ResourceModel){
"use strict";
var controller = Controller.extend("just.bi.apps.components.basecontroller.BaseController", {
onInit: function(){
this._initI18n();
},
_initI18n: function(){
var i18n = "i18n";
//create bundle descriptor for this controllers i18n resource data
var metadata = this.getMetadata(this);
var nameParts = metadata.getName().split(".");
nameParts.pop();
nameParts.push(i18n);
nameParts.push(i18n);
var bundleData = {bundleName: nameParts.join(".")};
//Use the bundledata to create or enhance the i18n model
var i18nModel = this.getModel(i18n);
if (i18nModel) {
i18nModel.enhance(bundleData);
}
else {
i18nModel = new ResourceModel(bundleData);
}
//set this i18n model.
this.setModel(i18nModel, i18n);
},
getModel: function(modelname){
var view = this.getView();
var model = view.getModel.apply(view, arguments);
if (!model) {
var ownerComponent = this.getOwnerComponent();
if (ownerComponent) {
model = ownerComponent.getModel(modelname);
}
}
return model;
},
setModel: function(model, modelName){
var view = this.getView();
view.setModel.apply(view, arguments);
}
});
return controller;
});
BaseController
initializes the i18 model by calling the _init118n()
method. In this method we extract the className of the this object from its metadata (using the .getName()
getter on the metadata obtained using the .getMetadata()
getter), and we pop off the unqualified classname to obtain its namespace. We then add the string "i18n"
twice - once for the folder, and once for the files inside it. We use this to create the bundlename which we use to instantiate the actual ResourceModel
.getModel()
. This is a utility method that gets the model from this controller's associated view, or of the component that "owns" the view and this controller..enhance()
method on it, rather than replacing it. This way, any texts defined at a higher level are still available in this controller and view. This gives us a functional i18n model, which we then set using setModel()
, which simply calls setModel()
on the view associated with this controller.BaseController
, we extend it when creating a "real" controller:
sap.ui.define([
"just/bi/apps/components/basecontroller/BaseController"
], function(Controller){
"use strict";
var controller = Controller.extend("just.bi.apps.components.mainpanel.MainPanel", {
onInit: function(){
Controller.prototype.onInit.call(this);
...
}
});
return controller;
});
onInit()
method, we need to first call the onInit()
method of the base controller, or rather, of whatever class we're extending. Fortunately, since we are extending it we already have a reference to it (in the snippet above, it's the Controller
parameter injected into our definition), so we call its onInit()
method via the prototype
while using the controller that is currently being defined (this
) as scope.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 | |
6 | |
4 | |
3 | |
3 | |
3 | |
2 | |
2 | |
2 |