473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

parameterized translations strings;what is the solution?


How are parameterized translation strings commonly handled?

Suppose I need to create a string like:

"file %1 failed to open", where %1 exists at runtime, but I need to
create it in a way that can be internationalized.

If I do something like this

str = _("file ") + filename + _(" failed to open");

The string to be translated is broken into pieces and the translators
can see what is going on. Besides different languages have different
word order. You can't necessarily hack tranlated phrases from a
different language together in the same way that you do in the native
language. Some things need to be translated as a whole.

Also there is UTF-8 and unicode.

Bjarne Stroustrup properly points out that sprintf and format
are not typesafe.

So what is the solution?

Gtkmm documentation recommends the compose library, but
that seems to be included in almost no Linux distributions,
so that can not be commonly used.

What is the solution that is used everyday in the real world?
--
Paul Elliott 1(512)837-1096
pe******@io.com PMB 181, 11900 Metric Blvd Suite J
http://www.io.com/~pelliott/pme/ Austin TX 78758-3117
May 27 '07 #1
2 1922
On May 27, 9:43 pm, Paul Elliott <pelli...@hrnowl.io.comwrote:
How are parameterized translation strings commonly handled?
Suppose I need to create a string like:
"file %1 failed to open", where %1 exists at runtime, but I need to
create it in a way that can be internationalized.
If I do something like this
str = _("file ") + filename + _(" failed to open");
The string to be translated is broken into pieces and the translators
can see what is going on. Besides different languages have different
word order. You can't necessarily hack tranlated phrases from a
different language together in the same way that you do in the native
language. Some things need to be translated as a whole.
Also there is UTF-8 and unicode.
Bjarne Stroustrup properly points out that sprintf and format
are not typesafe.
There are typesafe implementations of format. At least mine and
the one in Boost support the X/Open positional parameters as
well.
So what is the solution?
It depends on what the requirements are. For simple error
messages, it's often possible to organize them so that the
variable parts are more or less separate from the rest, and
there is at most one of them. So that while English, French and
German have different word orders:

std::cout << "The following file could not be opened: " <<
filename ;
std::cout << "Le fichier suivant n'a pas pu être ouvert : " <<
filename ;
std::cout << "Folgende Datei könnte nicht geöffnet werden: " <<
filename ;

works in all three.

Obviously, the operative phrase above is "for SIMPLE error
messages"---this solution only goes so far.

Historically, a long time ago, X/Open extended printf et al.
with positional parameters. (See the printf specification at
http://www.unix.org/single_unix_specification/.) This solution
has been ported to C++, first by me, later by Samuel Krempp, for
Boost. I've dropped all maintenance on my version, however,
because it doesn't really work in practice. Not only does word
order vary, but just about every other aspect of basic grammar:
in French or German, for example, adjectives need to reflect the
gender and number of the noun they modify, where as they are
invariable in English (and that's just the tip of the iceberg).
So you end up making compromizes like the above anyway, to avoid
the problem cases. In the end, the only solution I've found is
to define a function for each message, and put them into a
separate dynamically loaded object, and rewrite the code for the
functions for each language.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 27 '07 #2
On 27 Maj, 21:43, Paul Elliott <pelli...@hrnowl.io.comwrote:
How are parameterized translation strings commonly handled?

Suppose I need to create a string like:

"file %1 failed to open", where %1 exists at runtime, but I need to
create it in a way that can be internationalized.

If I do something like this

str = _("file ") + filename + _(" failed to open");

The string to be translated is broken into pieces and the translators
can see what is going on. Besides different languages have different
word order. You can't necessarily hack tranlated phrases from a
different language together in the same way that you do in the native
language. Some things need to be translated as a whole.

Also there is UTF-8 and unicode.

Bjarne Stroustrup properly points out that sprintf and format
are not typesafe.

So what is the solution?

Gtkmm documentation recommends the compose library, but
that seems to be included in almost no Linux distributions,
so that can not be commonly used.

What is the solution that is used everyday in the real world?
If your main problem with printf() is the fact that it's not typesafe
the you can create your own, simplified version.

std::string createMessage(std::string msg, std::vector<std::string>
params);

Where msg would be on the form "Error %1 in %2: Failed to open %3.",
and params would be a list of strings such that the first string would
replace %1 in msg, the second would replace %2 and so on. This of
course requires that all parameters are converted to strings first and
added to a vector.

--
Erik Wikström

May 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Joe | last post by:
I'm using Python 2.4 on Windows XP SP2. I'm trying to receive a command line argument that is a newline (\n) Here is the command line to use sample.py "\n" Here is a sample.py script
2
by: J.W. | last post by:
How do I compare strings in javascript? The "==" double equals or "!=" doesn't seem to work in this case. I'm sure string comparison has been explained before but searching Google didn't find...
6
by: Avon | last post by:
Hello there. I'm (kind of) new to programming as you can see, and I have a question. I want the user to input a string (for example: a product code) but store it dynamically. There must be an...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.