Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator
Accepted Solution

Create a localized ListView

Hi, I created a custom ListView in QML which retrieves

data from XML (has a XML file as a data model).

 

I think I know how to support localized string in typical situations,

e.g., in QML I add qsTr keyword to the strings I want to be

translated to different languages.

 

But how to do same with the XML data source?

 

Thank you.

 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Create a localized ListView

[ Edited ]

There are several options;

 

1) use qrTr in your QML file passing the returned ListDataItem.xxx

 

2) Supply a translation within the XML file as a separate field.

 

3) Supply an additional XML file for each language you are going to support then load the appropriate one.

 

You might find 3 is the easiest to maintain going forward as there is an obvious disjoint between the XML file and any translation file which without proper testing could lead to missed translations. i.e. any text you place in your xml file will not be automatically picked up by the translation collection service at build time.

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Create a localized ListView

[ Edited ]

Hi BBSJDev,

 

Yes, I think I was going to mention something similar to what you said.

 

I think the way these translation files work is that when you put a qsTr("Keyword") in the QML file

a corresponding entry is created automatically in the .ts file during building.

 

If I put qsTr(ListItemDataComponent.xxx), how will the corresponding entries be created

in the .ts file???

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Create a localized ListView

Yes that's correct and the fact that a corresponding entry will NOT be created automatically (this is the disjoint) is the reason I suggested you went with option 3.

 

Choosing option 1 means having to manually add the text in the XML file to the translation file yourself along with its translation.

 

Having to add the same data in two places is often a recipe for disaster.

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Create a localized ListView

Yes, I see how I could go with option 3 and thanks for that.

 

Just in such a scenario I can't see anymore how one can implement option 1 - or can we?

 

I think I didn't understand what you meant here exactly:

>>"Choosing option 1 means having to manually add the text in the XML file to the translation file yourself along with its translation."

 

ps. But this is a general problem with dynamic data right? What solutions does cascade have for translating data that

is coming on run time? (not specified as qsTr keywords in advance?).

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Create a localized ListView

[ Edited ]

As a very simple example you can do,

 

property variant tMessage: "hello"

Label {
  text: qsTr(tMessage)
}

So I'm not sure it's a problem with dynamic data as it's possible to pass dynamic data to the translation function.

For an IDE to collect dynamic data at build time is just not feasible, just imagine huge SQL databases and you'll see what I mean or imagine the case where a string is made up of returned external data.

 

My assumption with option 1 is that at run-time it will look up the text in tMessage and if it finds it in the translation file return the translation.

 

The collection of qsTr and tr text is after all only a convenience to save some time having to collect the strings yourself and write your own lookup function.

 

Before sending this auto collected translation file off to the translation aency you would just add your own text that is dynamically created, in this example you would have to add 'hello'.

 

I haven't tested this as I think this approach is flawed to begin with, as you are having to duplicate the same data in two different files and that can lead to data being missed.  However the documentation seems to suggest it is just a lookup table accessed at run-time so as long as that dynamic text (passed in qsTr()) exists within the translation file the correct translation should be returned.

 

Having written all this I do recommend not to take this approach, option 3 is easier and less prone to errors.

When you send you .tr files just send the XML files with them for translation, then within your code look up the language and if it's supported load that XML file instead.

 

 

 

 

 

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Create a localized ListView

[ Edited ]

Hi,

 

this is entry which was created by IDE when it encountered qsTr("helloworld") keyword in QML.

 

 <message>
        <source>helloworld</source>
        <translation type="unfinished"></translation>
    </message>

 As far as I am concerned you can't create such an entry manually in the .ts file, if the IDE does

not encounter the qsTr("helloworld") keyword somewhere in the source code.

 

Do you suggest I add such fake qsTr("helloworld"), qsTr("helloworld1") entries in the source code,

such that when the data is loaded dynamically, e.g., qsTr(dynamicData) -- the IDE will look

if "dynamicData" is already in the translation files and translate it? (the "dynamicData" could

happen to be in the translation files because we added it in a "fake" way as I mentioned before -- by hardcoding it in the source code beforehand).

Did I understand you right?

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Create a localized ListView

[ Edited ]

Okay first of all I am not suggesting to do it this way *at all* for the reasons stated above, I recommend option 3.

 

If you really want to do it the option 1 way then why call it fake?

 

The file created is just an xml file that holds entries for translation there is no reason whatsoever that you can't add you own entries to the .tr file, send it off to be translated, put it back in your project and then build it with your own entries in.

 

The build is just collecting the data for you in a human readable form that your translator can use to add the translations.

 

So just to clarify you need to add any dynamically created text (i,e, 'fake') translations to the .tr translation file before sending it off for translation.

 

So for the example I gave above you would add...

 

<message>
        <source>hello</source>
        <translation type="unfinished"></translation>
</message>

 

 

By explaining all this I really think I am sending you in the wrong direction, please use seperate XML files.

 

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Create a localized ListView

Hi, I will go for option 3, I am just trying to understand others.

 

Hardcoding qsTr("helloworld") in source code for me (in this case) is a way

to add an entry to the .ts file, which I can translate later. (To my understanding

this is the only way to add items to the .ts file, you can't add items there

by yourself - I think this is the only place where we disagree?).

I called it "fake" but it is just a means of terminology :smileyhappy: so it is ok.

 

Now, I think we are on the same track on this part, we can have a

qsTr(dynamicValue). dynamicValue at some point when read from XML

can take value of "helloworld" - the IDE will then lookup the .ts file and

see that translation item for "helloworld" has been created there (because

we added it there by ourselves as above - 2nd paragraph). Is it right? :smileyhappy:)))

 

 

Please use plain text.
Developer
BBSJdev
Posts: 6,089
Registered: ‎07-05-2012
My Device: Playbook, Z10 LE, Dev Alpha C
My Carrier: Orange

Re: Create a localized ListView

[ Edited ]

Good if we are only going down this road for exploration I'm happy to do so. :smileyhappy:

 

At this point it would need testing to see if there are any blocks to adding your own text, I don't see why there would be as it would complicate what is basically a simple lookup table.

 

If I get some time I'll have a look and see if it's possible.  Your code is probably more set up to test this than mine, could you try adding your own to the .tr file and see if it works or not?

 

As for your second paragraph, there is no lookup by the IDE at build time it is only done at runtime and of course the IDE is not present at runtime.

 

The IDE does two things for you in this situation;

 

 1) scans the qml and C++ source code for qsTr() and tr() and rips the text from the parameter into a .ts file

 2) on building replaces the qsTr and tr macros with code that then looks up that passed text in the translated .ts XML file

 

It's the app that then does the lookup at runtime.

 

Sorry but the only other language I can work in is Japanese and BB10 doesn't support it, my girlfriend speaks French and Hindi but she's not here and wouldn't be interested in what we are doing anyway. :smileyvery-happy:

 

 

 

 

 

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Please use plain text.