Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

721 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