on 02-11-2013 6:29 AM
Hi Everybody,
I'm currently trying to implement how to access the localized text using data binding in SAPUI5.
Reference code is here:
var oModel = new sap.ui.model.resource.ResourceModel({bundleName:"myBundle",bundleLocale:"en"});
var oControl = new sap.ui.commons.Button( {
id : "myButton",
text : "{i18n>MY_BUTTON_TEXT}"
});
oControl.setModel(oModel, "i18n");
I have few doubts:
1) Where do we give the .properties file?
2)Instead of myBundle are we supposed to give the path of the property file?
3)Why do we use the i18n in the text?
4)What does oModel.getResourceBundle() do?
It would be really helpful if someone can give me the exact procedure or a sample code of doing it.
Thanks in advance.
Hi Archana!
@Chris: One use-case for the ResourceModel and for usingdata binding for translated texts is when you are using XMLViews (which are purely declarative and have no code allowing to get a text from a resource bundle). Another might be when the language (or set of texts) needs to be switched often on-the-fly.
Coming to the questions:
1.) You already give the properties file: "bundleName" will be be resolved as UI5 module and ".properties" will then be appended to find the file. Instead, you can also directly give "bundleUrl" where you give the path to the .properties file (the one without any language in the name, the language-specific part will be inserted by the ResourceModel).
2.) answered by 1.)
3.) "i18n" is in this case the name of a named model. To have several models in parallel, you can have one default model and any number of models with a name prefix. "i18n" is a convention here (short for "internationalization"), but you could technically give any name for this model.
4.) getResourceBundle returns the ResourceBundle object which the ResourceModel uses internally and which Chris uses in his answer.
Example snippet:
var oModel = new sap.ui.model.resource.ResourceModel({
bundleUrl: "i18n/i18n.properties"
});
sap.ui.getCore().setModel(oModel, "i18n"); // global model is appropriate when having one translation file for the whole app
This will automatically use the current language; for other languages, you can also give a "bundleLocale" in the ResourceModel constructor.
(The documentation calls this "locale", but this is wrong and being fixed. Thanks!)
You can now bind your text with
text : "{i18n>MY_BUTTON_TEXT}"
and in an XMLView you could now say:
<Button text="{i18n>MY_BUTTON_TEXT}" press="sayHello" />
Regards
Andreas
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
is there a way to use to specify a different domain, from where the ResourceModel is loaded? E.g.
var oModel = new sap.ui.model.resource.ResourceModel({
bundleUrl: "<myServer>/resources/i18n/i18n.properties"
});
At the moment I'm experimenting with i18n within the SplitApp Control and therefore I have to include the https://sapui5.netweaver.ondemand.com/sdk/resources/sap-ui-core.js from the cloud, because it is not available right now for local development.
So https://sapui5.netweaver.ondemand.com/sdk/resources/<myserver>/resources/i18n/i18n.properties will be called at the moment.
I think it is just a matter of time in my special case, until 1.10.x is available for local development. But it might be also interesting for other use cases.
EDIT: Also interesting, when the resource bundles are not in folder "resources", but lets say in folder "i18n" underneath "WebContent". "resources" is always appended in the path.
Regards
Stefan
Hi,
sorry, this reply comes really late because I missed your question, but for reference: instead of a bundleUrl you can also give a bundleName which is a regular UI5 module name. This means all the normal mechanisms available for modules (like registerModulePath()) can be used to define where the bundle should be loaded from. Cross-domain loading may still cause issued due to the same-origin policy, but from UI5 side the locaton can be freely configured.
Regards
Andreas
Thanks @Chris and @Andreas..!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Archana,
I don't know why you are trying to get the localized text via data binding but I did it the normal way and it works for me
jQuery.sap.require( 'jquery.sap.resources' );
oBundle = jQuery.sap.resources( {
url : serverLocation + xyz.properties',
locale : 'en_GB'
} );
...
headerItems : [ new sap.ui.commons.TextView( {
text : "User Name",
tooltip : oBundle.getText( 'headerItems.userName' )
} ) ],
Best Regards
Chris
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 3 | |
| 3 | |
| 2 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.