Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-09
3
Medium Priority
?
514 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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

926 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