0

I have an SAPUI5 (V1.24) master-detail application wherein I have to display a list of about 25 static items and each item displays a static image when clicked.

I have the list titles stored in an i18n file which is instantiated as a ResourceBundle within the Component.js file.

Now instead of adding 25 rows of StandardListItem objects in my Master.xml.view file I was wondering if I could store all titles in a JSON file under mockdata folder and bind a JSONModel to my sap.m.List. But since the values in my JSON "key":"value"are nothing but the list titles I was looking for a way to bind the i18n texts with the JSON. Something like this:

{
  "List": [
    {
      "Key": "'{i18n>value1}'"
    },
    {
      "Key": "'{i18n>value2}'"
    },
    ...
  ]
}

But it didn't work at runtime. Instead it displayed the value as-is, as shown below: Master-Detail demo

Adding as many list items in the view doesn't feel right. What if tomorrow the list increases from 25 to 50? Please help.

Thanks.

Lalit
  • 839
  • 2
  • 14
  • 26
  • Is dynamically creating the JSON model an option for you (and not a separate mockdata file)? – Marc Feb 18 '16 at 10:25
  • I had indeed thought about this but then is there a way I can loop through my i18n texts? In practice, the keys are not named "value1", "value2" but more meaningful texts without any observable pattern. Thanks. – Lalit Feb 18 '16 at 10:28
  • You can prefix them. `Masterlist.Apple = An Apple`, `Masterlist.Banana = A banana` etc – Marc Feb 18 '16 at 12:55
  • Marc, I didn't follow. Can you elaborate a little bit? – Lalit Feb 18 '16 at 13:10
  • You said that there will be no pattern because the name will be meaningful (and not value1 to value25). But you can prefix your meaningful names with a pattern (`MyPattern.value1` ...) and then select every element in your i18n file that begins with `MyPattern.` – Marc Feb 18 '16 at 14:11
  • Yeah I find the second part tricky. I tried using my i18n ResourceModel to loop through but couldn't get it to work. How can I "read" the i18n file from the Master controller? – Lalit Feb 18 '16 at 14:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103855/discussion-between-kumar-and-marc). – Lalit Feb 18 '16 at 14:34

2 Answers2

1

After our chat discussion I came up with the following solution

var aAllKeys = [],
    aMasterKeys = [],
    oProperties = {},
    oJSON = {
        items: []
    };
// Get the current locale (for example "de-DE")
var sCurrentLocale = sap.ui.getCore().getConfiguration().getLanguage();
// This creates an array of locale fallback solutions.
// For example ["de-DE", "de", "en", ""]
var aFallbacks = jQuery.sap.resources._getFallbackLocales(sCurrentLocale);
// iterate all locales
for (var i = 0; i < aFallbacks.length; ++i) {
    var sLocale = aFallbacks[i];
    // try to load i18n file for each locale
    oProperties = jQuery.sap.properties({
        url: "i18n/i18n" + (sLocale ? "_" + sLocale : "") + ".properties"
    });
    // if the i18n file exists (i. e. contains keys)
    if (oProperties.getKeys().length > 0) {
        aAllKeys = oProperties.getKeys();
        break;
    }
}

// find all keys of items to display in master (the prefixed ones)
for (i = 0; i < aAllKeys.length; ++i) {
    if (aAllKeys[i].indexOf("MyPrefix.") > -1) {
        aMasterKeys.push(aAllKeys[i]);
    }
}

// find all values of items to display in master
for (i = 0; i < aMasterKeys.length; ++i) {
    oJSON.items.push({
        key: aMasterKeys[i],
        value: oProperties.getProperty(aMasterKeys[i])
    });
}

You can then use oJSON to create a new JSON model which can be bound to your masterlist


Edit: I modified the beginning of the snippet. This adds a fallback solution if there is no i18n file for the current locale. This is tested against SAPUI5 v1.30.

Marc
  • 6,051
  • 5
  • 26
  • 56
  • Marc, apparently the getText() is indeed required for the fallback. I must have done it earlier on my browser debugging console that day so it wasn't required to do it once more (or maybe things work differently there). But when I used this solution in my code I had to "fall back" to the non-empty array to get things started. I did something like oBundle.getText("A") because that's not there in my resource bundle. Although this works perfectly fine for me, do you think there could be another way? Thanks again! – Lalit Feb 22 '16 at 10:45
  • I updated my solution.. as I told you in chat, I expected this to happen :( But this extended solution should hopefully work. – Marc Feb 22 '16 at 13:19
0

See the solution I have provided in a similar topic: How is localized data binding set up with JSON files and XML views?

The best part, it requires only a single-line helper method :-)

Community
  • 1
  • 1
Qualiture
  • 4,900
  • 7
  • 27
  • 38