cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 Language switcher

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

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.

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,

where do i have to add the .properties files in the directory?

im working with eclipse, and use the abap team provider to deploy the project.

br alex

Former Member
0 Kudos

Hi,

my .properties files are in the "localization" folder which is in the project(your application name) folder.

So the path for my ResourceBundle is <project_name>/localization/<name>.properties.

You have to specify the relative path down from your "WebContent" folder.

Hope i could help you

Best Regards

Andy

Former Member
0 Kudos

Hi,

thanks for your fast answer!

seems to be a problem by submitting with the team provider, because the team provider submits to a bsp application, and there it reorders the folders. when i create a folder in WebContent, add the file there, it does not upload the file, and so i cannot reach in the application.

br alex

Former Member
0 Kudos

hi andy,

can you post a screenshot of your folder structure with the .properties files?

br alex