Unicode xgettext problems with _() over several code lines Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
hgiritzer
Knows some wx things
Knows some wx things
Posts: 34
Joined: Wed Mar 09, 2005 10:24 am
Location: Austria

Unicode xgettext problems with _() over several code lines

Post by hgiritzer » Mon Mar 06, 2006 7:07 pm

Is there any recommendable practice to spread long _() sentences over several code lines without getting troubles with the Unicode compiler and/or xgettext?

MSVC in Unicode builds complains about strings like:

Code: Select all

    _("text1" \
      "text2")
saying "mismatched wide string concatenation".
Putting them as:

Code: Select all

    _("text1")
    _("text2")
make them appear as two separed strings to xgettext
(but should be one as they belong together)...
doing:

Code: Select all

    _("text1\
       text2")
would solve both problems above but the resulting string would be

Code: Select all

"text1         text2"
instead of

Code: Select all

"text1 text2"
.

Maybe the only simple way to solve this might be:

Code: Select all

_("text1 text2")
even if the string goes > 80 columns...

chris
I live to help wx-kind
I live to help wx-kind
Posts: 150
Joined: Fri Oct 08, 2004 2:05 pm
Location: Europe

Post by chris » Mon Mar 06, 2006 9:52 pm

Hi,

did you try

Code: Select all

_("text1"
"text2");
?

Chris
this->signature=NULL;

leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio » Tue Mar 07, 2006 1:35 am

I did that in my project. VS2005 was bitchy about it in unicode builds.
Currently I ended up doing

Code: Select all

_("text1"
wxT("text2")
);
But I fear that xgettext won't parse that as I'd wish it to. My project isn't yet set up for i18n.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/

frm
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Sep 09, 2004 7:29 pm
Location: Modena, ITALY
Contact:

Post by frm » Tue Mar 07, 2006 8:14 pm

No way.

I've tried both your suggested way

_("text1"
"text2");

but this still gives the C2308 error:mismatched wide string concatenation

And

_("text1"
wxT("text2")
);

works but is not recognized at all by xgettext.

I'm really unsure how to handle this... :(

hgiritzer
Knows some wx things
Knows some wx things
Posts: 34
Joined: Wed Mar 09, 2005 10:24 am
Location: Austria

Post by hgiritzer » Thu Mar 09, 2006 3:32 pm

How do others work with Unicode and xgettext (poEdit)?

Do you write your _("xxx") items always into one line of code, regardless of how long this code line then might become?

Is there no practicable way to split long _("xxx") items onto two or more lines of code to keep the code somewhat readable?

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Sun Mar 12, 2006 9:11 pm

Hummm! In my project I have the following code:

wxString s = _("text 1"
"text 2"
"text n");

I thought it was a good way to solve the problem you mention and I didn't get compiler errors. So I checked my code and I realized that currently I am not generating a Unicode build.

I remember having generated Unicode builds with the code like this:

wxString s = _( _T("text 1")
_T("text 2")
_T("text n"));


With no problems. I will try to do some testing and tell you my results.

hgiritzer
Knows some wx things
Knows some wx things
Posts: 34
Joined: Wed Mar 09, 2005 10:24 am
Location: Austria

Post by hgiritzer » Mon Mar 13, 2006 7:03 am

Thank you for your efforts.
Please keep in mind, that we need a solution for Unicode build in combination with xgettext translation (poEdit).

It's easy to use only one of them, but problematic when using both together.

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Mon Mar 13, 2006 7:19 am

As promised, here are my results:

To me the best solution is:

Code: Select all

wxString s = wxGetTranslation(
     _T("line 1")
     _T("line 2")
     _T("line n")
);
It is important to use 'wxGetTranslation() as macro _() DOES NOT WORK. You get error 'Symbol LL not defined'

frm
Knows some wx things
Knows some wx things
Posts: 47
Joined: Thu Sep 09, 2004 7:29 pm
Location: Modena, ITALY
Contact:

Post by frm » Mon Mar 13, 2006 2:58 pm

wxString s = wxGetTranslation(
_T("line 1")
_T("line 2")
_T("line n")
);
I've tried this but it's not recognized by xgettext... even using the -kwxGetTranslation option it doesn't 'see' the strings...

hgiritzer
Knows some wx things
Knows some wx things
Posts: 34
Joined: Wed Mar 09, 2005 10:24 am
Location: Austria

Post by hgiritzer » Mon Mar 13, 2006 3:09 pm

Well, it seems to me that it is the best practice to write the whole sentence into a single long line of code.

The code might become a bit harder to read, but as we can see in this discussion there is no 100% working alternative for this.

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Mon Mar 13, 2006 5:46 pm

I've tried this but it's not recognized by xgettext... even using the -kwxGetTranslation option it doesn't 'see' the strings...
Sorry! This morning I didn't do tests with poEdit, I just tried find a solution to compile. I did not take poEdit into account :oops:

So, probably hgiritzer answer:
the best practice to write the whole sentence into a single long line of code.
is the best answer for now

I was doing more test this evening but I did not find any better answer! poEdit does recognizes wxGetTranslation (when instructed) but the problem seem to be in the inside _T() macro.

I do not have more ideas. If any one finds a solution, please, post it.

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Wed Mar 22, 2006 6:15 pm

This is my best attempt. It is better than a single long line:
_("This program is free software; you can redistribute it and/or modify it \
under the terms of the GNU General Public License as published by the Free \
Software Foundation; either version 2 of the License, or (at your option) \
any later version.");
The only point to take into account is that continuation lines must start at column 1.

Cryogen
In need of some credit
In need of some credit
Posts: 7
Joined: Mon Aug 17, 2009 2:57 pm

Same problem

Post by Cryogen » Thu Dec 03, 2009 4:36 pm

Hi Guys,

I need a solution to this, too, and this doesn't work for me. I have:

const wxString s = _("some text");

and this

const wxString s2 = _("some much, much, much \
, much, much, much, much, much \
, much, much, much longer text");

in a header file. A translation exists, the catalogue is loaded and other strings are translated but these refuse to budge. Any ideas?

Thanks.

User avatar
doublemax
Moderator
Moderator
Posts: 15830
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax » Thu Dec 03, 2009 5:29 pm

This probably doesn't work because constant and static variables are initialized before the actual code execution starts, so translation files are not loaded yet.
Use the source, Luke!

cecilio
I live to help wx-kind
I live to help wx-kind
Posts: 189
Joined: Fri Dec 03, 2004 8:44 am
Location: spain
Contact:

Post by cecilio » Thu Dec 03, 2009 7:45 pm

Hi,

quote]This probably doesn't work because constant and static variables are initialized before the actual code execution starts, so translation files are not loaded yet.[/quote]

Yes, it doesn't work.

My approach to solve this is to initialize all const strings in the object constructor. Something as:

Code: Select all

myobj::myobj()
{
    InitLanguageDependentStrings();
}

void myobj::InitLanguageDependentStrings()
{
    static fInitialized = false;
    if (!fInitialized)
    {
        fInitialized = true;
        m_string1 = _("String to translate");
    }
}

Cecilio

Post Reply