[Development] Introducing discussion about QStringFormatter

Eike Ziller Eike.Ziller at qt.io
Tue May 16 11:58:32 CEST 2017


> On May 16, 2017, at 10:12 AM, Edward Welbourne <edward.welbourne at qt.io> wrote:
> 
> Jason H (15 May 2017 23:52):
>> Having worked with Python's format(), I much prefer the alterative {}
>> syntax, where {} defers to the next paramter.
>> '{}{}{}'.format('a', 'b', 'c) == 'abc'
>> Which makes maintence much easier,
> 
> However, it can be problematic for translation; some languages shall
> change word order, which is why it's desirable to provide *some* way for
> the order of substitutions to depart from the order of appearance in the
> format string.
> 
>> partocuclarly when adding a parameter to the beginning. Otherwose your
>> statements get all out of whack:
>> '{3}{1}{2}'.format('b', 'c', 'a') == 'abc'
> 
> where, I take it, the real-world example has lots of text between the
> formatters, 'the {3} brown {1} jumped over the lazy {2}' % ('fox',
> 'dog', 'quick') and renumbering the formatters to 'the {1} brown {2}
> jumped over the lazy {3}' % ('quick', 'fox', 'dog') gets to be tedious
> and susceptible to errors, particularly when the renumbering has to be
> propagated to every translation.  None the less, it's a small price to
> pay for being translatable.
> 
>> I's like the named formatter to take a QVariantMap (JS Object):
>> QVariantMap object;
>> object["name"]="World"; or object= {"name: 'World'} // from QML
>> QStringFormatter("Hello, {name}").arg(object);
>> 
>> I'd also like to see QML get fomr formatting love as well.
> 
> The named approach is, indeed, the best of both worlds; translation
> adapts naturally and additions don't require re-indexing.  I suspect C++
> programmers shall find it a bit heavy-weight (needing a hash, of some
> variety, to do simple formatting) but it likely feels much more natural
> at the QML level (just as it does in python).

With C++11 this can be written as
QStringFormatter(“Hello, {name}”).arg({{“name”, “World”}});
which is at least still relatively concise.

-- 
Eike Ziller
Principal Software Engineer

The Qt Company GmbH
Rudower Chaussee 13
D-12489 Berlin
eike.ziller at qt.io
http://qt.io
Geschäftsführer: Mika Pälsi,
Juha Varelius, Mika Harjuaho
Sitz der Gesellschaft: Berlin, Registergericht: Amtsgericht Charlottenburg, HRB 144331 B



More information about the Development mailing list