December 3, 2010

agroyer agroyer
Lab Rat
6 posts

Dynamic text using QString::arg() in QML

 

The following web page mentions how to achieve dynamic text using QString::arg() :
http://doc.qt.nokia.com/4.7/internationalization.html#use-qstring-arg-for-dynamic-text

This looks wonderful for localized text where the arguments are not always at the same position:

  1. label.setText(tr("%1 of %2 files copied.\nCopying: %3")
  2.                    .arg(done)
  3.                    .arg(total)
  4.                    .arg(currentFile));

How to achieve the same thing in QML ?
Is there a QML interface for QString::arg ?
Do I need to create one ?
I wrote this little qml file that does the job in ECMAScript but I am not really happy with it:
  1. import Qt 4.7
  2.  
  3. Rectangle {
  4.     width: 100
  5.     height: 62
  6.  
  7.     function stringFormat(str, args) {
  8.         for(var i=0; i<args.length; i++)
  9.             str = str.replace(eval("/%"+(i+1)+"/"), args[i]);
  10.  
  11.         return str;
  12.     }
  13.  
  14.     Component.onCompleted: {
  15.         var str = stringFormat(qsTr("%1 of %2 files copied.-Copying: %3"), new Array("5", "10", "file.txt"));
  16.  
  17.         console.log("str="+str);
  18.     }
  19. }

7 replies

December 3, 2010

digitalsurgeon digitalsurge..
Lab Rat
15 posts

i think javascript does not support those argument like functionality for strings

December 3, 2010

Kxyu Kxyu
Lab Rat
145 posts
agroyer wrote:
How to achieve the same thing in QML ?

  1. Text {
  2.     text:done+" of "+total+" files copied.-Copying:  "+currentFile
  3.     }  

December 3, 2010

infidel infidel
Lab Rat
2 posts

The question was how to avoid fixing the string structure as agroyer stated in the first place.

Kxyu wrote:
agroyer wrote:
How to achieve the same thing in QML ?

  1. Text {
  2.     text:done+" of "+total+" files copied.-Copying:  "+currentFile
  3.     }  

December 3, 2010

Andre Andre
Robot Herder
6393 posts

Indeed, we need to move further away from fixing strings in this way, not moving back towards it. There are many problems with fixing strings, that multiply with localization. One issue is that not all languages would have all arguments in the same place, and another one is that plural froms may work very differently across languages. There may even be more than one plural form, such as in some Eastern European languages.

December 3, 2010

thp thp
Lab Rat
35 posts

As for a temporary workaround, you could subclass QObject and provide some invokable helper functions that take care of formatting the strings, and use setContextProperty on the QML view to “inject” your string formatting helper. This would also make your QML code easier to read, as you don’t do the formatting in there, and the details of how the string is formatted are hidden.

Imaginary example call:

  1. text: formatter.copyProgress(done, total, currentFile)

October 23, 2012

ErikH ErikH
Lab Rat
5 posts

I know it’s an old thread, but if someone is still interested…

In qml the qsTr() does take arguments in the same way as QString does. You can write qsTr(”%1 copied”).arg(filename).

This will translate the string and then format it using the argument. At least it works fine with string arguments.

October 24, 2012

chrisadams chrisadams
Ant Farmer
259 posts

Yes.

The JavaScript environment in QML is slightly modified from the ECMAScript standard. If you take a look at the implementation of the QScriptEngine::installTranslatorFunctions(const QScriptValue &object) function in Qt4, you’ll note that we actually modify the String prototype to include the .arg() function. In Qt5 we do a similar modification, in QV8Engine.cpp, where we add the arg function to the String prototype (which calls through to a qml builtin function called stringArg).

That is, all JavaScript String objects in QML have a non-standard arg() function, which behaves similarly to the QString::arg() function we’re all familiar with from C++.

The JavaScript environment also has several other modifications, including adding the qsTr and similar functions to the global object.

Cheers,
Chris.

 
  ‹‹ QML and frame rate      Import a javascript file from another javascript file ››

You must log in to post a reply. Not a member yet? Register here!