Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to next message
mdelfede is currently offline  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 Go to previous messageGo to next message
mdelfede is currently offline  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 #24308 is a reply to message #24304] Sun, 10 January 2010 14:26 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
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. Does calling GetLngString directly so much difference?
Re: Helper for internazionalize arrays of literals [message #24310 is a reply to message #24308] Sun, 10 January 2010 14:40 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
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 Go to previous messageGo to next message
mdelfede is currently offline  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
Re: Helper for internazionalize arrays of literals [message #24330 is a reply to message #24325] Mon, 11 January 2010 11:09 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
BTW, I had to change it like this :
struct StringTable {
	const char *s;

	String ToString() const       { return GetLngString(s); }
	operator const char *() const { return ToString(); }
	operator String() { return ToString(); }
	operator Value() { return ToString(); }
};


Otherwise it didn't work here :

DropList d;
int i = 0;
d.Add(i, myStringTable[i]);


Don't know exactly why he didn't pick automatically the char * --> Value conversion. Even adding the String() operator wasn't enough.

Ciao

Max
Re: Helper for internazionalize arrays of literals [message #24331 is a reply to message #24330] Mon, 11 January 2010 11:12 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mdelfede wrote on Mon, 11 January 2010 05:09

BTW, I had to change it like this :
struct StringTable {
	const char *s;

	String ToString() const       { return GetLngString(s); }
	operator const char *() const { return ToString(); }
	operator String() { return ToString(); }
	operator Value() { return ToString(); }
};


Otherwise it didn't work here :

DropList d;
int i = 0;
d.Add(i, myStringTable[i]);


Don't know exactly why he didn't pick automatically the char * --> Value conversion. Even adding the String() operator wasn't enough.

Ciao

Max



Well, I believe it is "one-conversion-operator" rule: C++ never goes through more than single conversion. tt_char -> const char * -> Value is two conversions. (And String does not change much there...).

Mirek
Re: Helper for internazionalize arrays of literals [message #24332 is a reply to message #24331] Mon, 11 January 2010 11:17 Go to previous message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
So, why did it work before ? I had just the 'String operator[]' in my previous class, not a Value operator.

Max
Previous Topic: GCC compilation options
Next Topic: Template class factory
Goto Forum:
  


Current Time: Thu Mar 28 15:03:00 CET 2024

Total time taken to generate the page: 0.01084 seconds