|
Boost : |
Subject: Re: [boost] [Boost.Locale] Upcoming Boost.Locale version
From: Artyom (artyomtnk_at_[hidden])
Date: 2010-08-24 09:22:54
> I like to think in terms of [serialized] objects and not strings.
> Besides having to internationalize images sometimes I need to do the
> same to arrays of strings or any type of object.
> In the use case of array of strings, I may in English
> have a selection or radii
> ["25 miles", "50 miles", "75 miles" "100 miles", "150 miles", "200 miles"].
> However in a different language I may have ["50km", "100km", "150km",
>"250km"].
> Because the unit is different I may more or fewer items in the array.
> The items in the array may not even be strings but a unit object that has both
>
>runtime scaler and runtime unit.
>
There is a general approach to solve such issues, even if this is not
supported by the library.
You use following:
double factor = atof(dgettext("Distance conversion factor for
km","1.0").c_str());
int local_distance = static_cast<int>(metric_distance * factor);
std::cout <<format(translate("{1,num} kms","{1,num} kms",local_distance)) %
local_distance
That's it.
You just define some translation string and use it as localization reasource.
And tanslator
translates this factor to ~1.6 and kms to miles according to locale.
> So using ICU how would I store and retrieve an array of objects using boost
>serialization into one field.
For serialization you should always use locale insensitive data. For example
metric system.
As for time we alawys use POSIX time wich always relates to UTC.
> Does ICU support any kind of contenttype, metadata or filename extension that
>could identify such items
> to tools? Will your Locale library have any add on or enhancement to simplify
>this use-case;
>
> perhaps translate<object type> versus translate?
This is done differently.
Lets define following:
class distance {
int meters;
};
std::ostream &operator<<(std::ostream &out,distance const &d)
{
// get stream specific locale
std::locale l=out.getloc();
// get stream specific factor
double factor = atof(dgettext("Distance conversion factor for
km","1.0",l).c_str());
int local_distance = static_cast<int>(metric_distance * factor);
// format the distance is stream specific units.
out <<format(translate("{1,num} kms","{1,num} kms",local_distance)) %
local_distance
}
Note: locaization is different for serialization and it is wrong way
to look on them this way. Serialization is provided for computer-to-computer
interface
while localization is provided for computer-to-human interface and it is much
more compilcated.
For example, for serialization of time point it is enought to write it as number
representing
POSIX time to stream.
time_t time_point=time(0);
std::cout << time_point;
// would be something like 32452345
For human you would write
time_t time_point=time(0);
std::cout << as::date_time << time_point;
// may be something like 03/02/2010 12:30:02
Artyom
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk