Theppitak Karoonboonyanan ([info]theppitak) wrote,
@ 2008-08-22 14:24:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Entry tags:c++, localization, programming

iostream String Formatting Considered Harmful

When learning to code in C++, I was convinced by the advantage of iostream over C printf, such as better type checking. C++ syntactic features have been used to devise the stream operators and manipulators to match what printf provides. But one important thing is missing: format string localization.

Some examples are shown in GNOME #548950 for Ekiga trunk.

For example, to print how many users are found online, with printf and gettext we get (plural form is omitted here for simplicity):

  printf (_("%d users found\n"), nUsers);

But with iostream we get:

  std::cout << nUsers << " " << _("users found") << std::endl;

In printf case, translators are free to reorder words according to different grammars. For example, in case of Thai, it can be translated like:

  msgid "%d users found\n"
  msgstr "พบผู้ใช้ %d คน\n"

(Literally, the Thai msgstr reads "found users %d persons".)

But this is impossible for iostream case. One must end up with very weird language usage like:

  msgid "users found"
  msgstr "ผู้ใช้ถูกพบ"

(Thai msgstr here literally reads "users are found".)

And "3 ผู้ใช้ถูกพบ" sounds weird and unnatural to Thai readers. That is, with iostream, word orders in messages are tied to English.

I don't know how to solve this with iostream. It seems beyond what C++ syntax can achieve.

When combined with another case I found when working with POSIX file descriptor, for which fstream constructor has been dropped from C++ standard library, using iostream is simply a wrong decision in the first place. And the solution is to migrate the file manipulating codes to plain stdio C library.

Lesson learned: Don't use C++ iostream unless you really have limited use cases.




(Read 5 comments) - (Post a new comment)


[info]bugmeno.myopenid.com
2008-09-12 12:44 pm UTC (link)
* POSIX file descriptor is not standard [ANSI] C library?
* the standard [ANSI] C library is just fopen()?
* fdopen(), fileno(), open() is not standard [ANSI] C library?
* notice that MSVC++ has _fdopen(), _fileno(), _open(), not fdopen(), fileno(), open().

> - The standard C++ iostream library now doesn't support
> using fstream with FILE* or file descriptor.

* It had ever can do that? (มันเคยทำได้ด้วยเหรอ?)

> /me never feels disappointed with C++ like this before.

* To "include " doesn't mean that you are writing C.
* It is just a C-style code, which is one important part of C++.
* To be able to write C-style code is one advantage of C++.
* Every advantage having in C, will also have in C++ too (advantage ทุกอันที่มีใน C ก็จะมีใน C++ ด้วย), since C-style is a subset of C++. So, C++ will never worse than C (ด้วยตรรกะนี้, C++ จะไม่มีทางด้อยกว่า C).

(Reply to this) (Thread)


[info]bugmeno.myopenid.com
2008-09-12 12:45 pm UTC (link)
(ลืมลงชื่อ)

----
อานนท์

(Reply to this) (Parent)


(Anonymous)
2008-09-12 12:48 pm UTC (link)
(fix previous error)
* To "include <cstdio>" doesn't mean that you are writing C.

----
อานนท์

(Reply to this) (Parent)


(Read 5 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…