Solved

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

Posted on 2014-01-09
3
451 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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.

772 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