Solved

C++ array of std::wstrings? Or Dictionary? i18n

Posted on 2014-01-09
3
459 Views
Last Modified: 2014-01-09
I need a way to fetch a string, based on the current language.

Say I have 3 languages: ENGLISH, SPANISH, FRENCH

And I want to display a string representing either Metric or US units.


I was thinking maybe an array such I could access as:
unitString[SPANISH][METRIC];

Open in new window

would give me the Spanish name for the metric units (of whatever I'm measuring).

But I don't really like that idea. Maybe it's OK.

(If this was .NET code I would look into using Dictionary. But it's unmanaged C++ code.)

Would be nice if all the strings can be initialized in one place. The strings don't change, they're static.

Well so far a 2-D array of std::wstrings is the only idea I've come up with. Is there a better way? How would I define a 2-D static array of std::wstrings and initialize them?


(My other even worse idea was create a whole bunch of strings:
SpanishMetric = "..."
EnglishMetric = "...";
FrenchMetric = "...";
EnglishUS = "...";
SpanishUS = "...";
FrenchUS = "...";

then a nasty case statement:
switch(language)
{
    case ENGLISH:
    switch(unit)
    {
       case METRIC:
           return EnglishMetric;
       case US:
           return EnglishUS;
    }
   case SPANISH:
     ...
    }
well that's not nice at all. Very hard to add a language to that mess...
Must be a better way...
0
Comment
Question by:deleyd
3 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 39769465
Well, one way or another you'll need a lookup table, but I'd be inclined to store this in a file using JSON or XML (or something similar) and then load this at runtime. As for the data structure, maybe a map of pairs?

std::map<std::wstring, std::pair<std::wstring, std::wstring>> lookup_table;

...

auto const & metric = lookup_table[L"spanish"].first;
auto const & imperial = lookup_table[L"spanish"].second;

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39769545
BTW, if you are on Windows, you might be reinventig the wheel, since the system stores that info as well, e.g.

TCHAR buf[256];
GetLocalInfo(MAKELCID(MAKELANGID(LANG_SPANISH,SUBLANG_DEFAULT),LOCALE_IMEASURE,buf,sizeof(buf));

Open in new window

0
 

Author Closing Comment

by:deleyd
ID: 39769557
OK std::map looks good. I'll look into that.

(Sorry target isn't Microsoft Windows, though we develop the code on Windows.)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now