|
|
Home » Developing U++ » U++ Developers corner » Helper for internazionalize arrays of literals
Helper for internazionalize arrays of literals [message #24302] |
Sat, 09 January 2010 14:59 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
Starting to internationalize an app, I faced to this problem :
char *myTable =
{
"ONE",
"TWO",
"THREE"
};
which can't be internationalized by aid of 't_' macro. It can be done with 'tt_' macro, but it need patched code to retrieve the internationalized string with GetLngString macro.
So I've thought about a better way, and coded this :
class StringTable : public Vector<String>
{
public:
StringTable &operator,(const char *s) { Add(s); return *this; }
const char *operator[](int i) { ASSERT(i < GetCount()); return ~At(i); }
};
#define STRINGTABLE(s) StringTable s; INITBLOCK { s,
#define ENDTABLE ; }
This allows to define a character table like that :
STRINGTABLE(myTable)
t_("One"),
t_("Two"),
t_("Three")
ENDTABLE;
Access is as before with myTable[], so no need to code changes; as a small benefit, the string translation is done at load time just once, and not every time the string is needed.
What do you think about ? Do you know a better way to achieve the same purpose ?
Ciao
Max
[Updated on: Sat, 09 January 2010 15:45] Report message to a moderator
|
|
|
Re: Helper for internazionalize arrays of literals [message #24304 is a reply to message #24302] |
Sat, 09 January 2010 21:39 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
This one corrects a subtle bug and delays loading of strings (needed to setup properly the language) :
class StringTable : public Vector<String>
{
public:
StringTable &operator,(const char *s) { Add(s); return *this; }
String operator[](int i) { ASSERT(i < GetCount()); return GetLngString(At(i)); }
};
#define STRINGTABLE(s) StringTable s; INITBLOCK { s,
#define ENDTABLE ; }
Stringtable must be defined with tt_ :
STRINGTABLE(Test)
tt_("One"),
tt_("Two"),
tt_("Three")
ENDTABLE;
The tt_ macros are needed just to make theide export the translation; can be avoided if no need to sync translations.
Ciao
Max
[Updated on: Sun, 10 January 2010 01:46] Report message to a moderator
|
|
|
|
Re: Helper for internazionalize arrays of literals [message #24310 is a reply to message #24308] |
Sun, 10 January 2010 14:40 |
|
mirek
Messages: 13984 Registered: November 2005
|
Ultimate Member |
|
|
[quote title=luzr wrote on Sun, 10 January 2010 08:26]mdelfede wrote on Sat, 09 January 2010 15:39 | This one corrects a subtle bug and delays loading of strings (needed to setup properly the language) :
class StringTable : public Vector<String>
{
public:
StringTable &operator,(const char *s) { Add(s); return *this; }
String operator[](int i) { ASSERT(i < GetCount()); return GetLngString(At(i)); }
};
#define STRINGTABLE(s) StringTable s; INITBLOCK { s,
#define ENDTABLE ; }
Stringtable must be defined with tt_ :
STRINGTABLE(Test)
tt_("One"),
tt_("Two"),
tt_("Three")
ENDTABLE;
The tt_ macros are needed just to make theide export the translation; can be avoided if no need to sync translations.
Ciao
Max
|
Still not sure whether all of this is worth the trouble....
BTW, there IMO could be even more effective solutions. E.g.:
#include <Core/Core.h>
using namespace Upp;
struct tt_char {
const char *s;
String ToString() const { return GetLngString(s); }
operator const char *() const { return ToString(); }
};
CONSOLE_APP_MAIN
{
static tt_char x[] = {
tt_("Aborted by user."),
tt_("Two"),
tt_("Three")
};
SetLanguage(LNG_('I','T','I','T'));
DDUMP(x[0]);
}
|
|
|
Re: Helper for internazionalize arrays of literals [message #24325 is a reply to message #24310] |
Mon, 11 January 2010 08:25 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
luzr wrote on Sun, 10 January 2010 14:40 | BTW, there IMO could be even more effective solutions. E.g.:
#include <Core/Core.h>
using namespace Upp;
struct tt_char {
const char *s;
String ToString() const { return GetLngString(s); }
operator const char *() const { return ToString(); }
};
CONSOLE_APP_MAIN
{
static tt_char x[] = {
tt_("Aborted by user."),
tt_("Two"),
tt_("Three")
};
SetLanguage(LNG_('I','T','I','T'));
DDUMP(x[0]);
}
|
mhhhh.... right, yours is much better. I was wrongly thinking that wasn't possible to use a static initializer with a class.
BTW, imho it's worth the trouble because you don't need to remember using the GetLngString on every places you need it... More, with your solution you prcatically don't have any overhead to achieve this.
Ciao
Max
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Jun 13 06:01:57 CEST 2024
Total time taken to generate the page: 0.02066 seconds
|
|
|