Solved

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

Posted on 2014-01-09
3
474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

739 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