Skip to Content
author's profile photo Former Member
Former Member

SAPUI5 Language switcher

Hello everybody,

I'm currently trying to implement a language switcher in SAPUI5 but I have a few problems/needs for enhancements there.

The idea is to create a dropdownbox that'll let the user decide on the language he wants to see for the page.

I'm wondering if my current method of switching the language is really the best solution or if there are any better solutions to this?

My current code looks like this:

// Create a ListBox
var oListBox1 = new sap.ui.commons.ListBox("Languages", {
items: [new sap.ui.core.ListItem("Lang1", {
text: "German"
}), new sap.ui.core.ListItem("Lang2", {
text: "English"
})]
});// Create a DropdownBox
var oDropdownBox2 = new sap.ui.commons.DropdownBox("DropdownBox2", {
tooltip: "Language",
"association:listBox": oListBox1,
value: "German"
});
oDropdownBox2.attachChange(function () {
// TODO Change locale here
if (oDropdownBox2.getValue() == "German") {
var oBundle = jQuery.sap.resources({
url: "res/i18n.properties",
locale: "de"
});
updateTexts(oBundle);
} else {
var oBundle = jQuery.sap.resources({
url: "res/i18n.properties",
locale: "en"
});
updateTexts(oBundle);
}
});function updateTexts(oBundle) {
oLinkPassword.setText(oBundle.getText("LINK_PASSWORD"));
oTvPasswordInfo.setText(oBundle.getText("MSG_PASSWORD"));
oLinkPin.setText(oBundle.getText("LINK_PIN"));
oTvPinInfo.setText(oBundle.getText("MSG_PIN"));
oLinkRegister.setText(oBundle.getText("LINK_REGISTRATION"));
oLinkContact.setText(oBundle.getText("LINK_CONTACT"));
oLinkImpressum.setText(oBundle.getText("LINK_IMPRESSUM"));
oLinkThemeSwitch.setText(oBundle.getText("LINK_THEMESWITCH"));
oTvUser.setText(oBundle.getText("USER"));
oTvPassword.setText(oBundle.getText("PASSWORD"));
oBSend.setText(oBundle.getText("SEND_BUTTON"));
oTVSuccessMsg.setText(oBundle.getText("Checking..."));
oTVSuccessMsg.setText(oBundle.getText("LOGON_SUCCESS"));
oTVSuccessMsg.setText(oBundle.getText("LOGON_FAILED"));
}

Edit: Created a separate thread for the CSS problem (http://scn.sap.com/thread/3271946).

Message was edited by: Department Portals

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Sep 20, 2013 at 09:48 AM

    Hello Department Portals,

    in my application i resolved this problem with following class:

    sap.ui.model.resource.ResourceModel

    Simply instantiate it with:

    var oMyResourceModel = new sap.ui.model.resource.ResourceModel({bundleUrl: "res/i18n.properties", bundleLocale: "en"});

    Then you set the model to your view/core/element with a specific key:

    sap.ui.getCore().setModel(oMyResourceModel, "myResourceKey");

    Now all your bindings have to look like this:

    var oButton = new sap.ui.commons.Button({

    text: "{myResourceKey>my_button_text}"

    ]);

    my_button_text should be contained in your i18n.properties file of course ;-)

    Now you are able to change the language with only instantiating a new ResourceModel and set it to your core/view/control or whatever :-).

    I hope i could help you.

    Kind regards,

    Andreas

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Dec 09, 2013 at 04:48 PM

    There may also be a backend language consideration if any of the UI texts are sourced from the backend and have a language dependency.

    I have encountered some issues with using the normal channels for language settings, e.g. the language you log in with is stored in a cookie and set s the backend language. After that it can be hard to get a different language accepted if one is sent in an OData request.

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.