String table

Posted on 2002-04-17
Last Modified: 2008-03-17
I want to hold all visible strings for my project in a table, probably in a series of header files.  The aim is to replace IDC_MEMORY_ERROR with "You are out of memory".  How should I do this?  I would rather use something other than #define, but if that's the best way, then I can live with it.

It must work on any platform (so no VC++ resources etc.)
Question by:IainHere
LVL 86

Accepted Solution

jkr earned 100 total points
ID: 6947962
Well, the simplest way would be to load your text resources at runtime from a text file into an array and have IDC_MEMORY_ERROR as an index into that array. If you want it to be bullet-proof, use a map that maps the integer constant to a string...

Author Comment

ID: 6948098
With that method, I'd have to keep the strings in the same order, otherwise I'd have to recode the section that reads in the strings.  Is there a way for me to avoid it, or am I missing something?

Also, using #define will fix the strings at compile time - is there a special advantage/disadvantage to this?

My ideal would be to have a text file that read

IDC_MEMORY_ERROR    "you're outta memory"
IDC_BORED_ERROR     "you're too bored"

and refer to the IDC_ in the program.  Thanks for your help.
LVL 86

Expert Comment

ID: 6948272
>>Also, using #define will fix the strings at compile
>>time - is there a special advantage/disadvantage
>>to this?

Well, there's a disadvantage: You'll have to supply a seperate binary for each language...
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 6948306
Oh yeah - I forgot about that whole section in the install process "choose language".  As you might guess, this is my first go at multi-language support.

So, discounting that option, if I use the map solution, my file will look like

10001   "Error String"

and code will look like:
const int IDC_STRING_ERROR = 10001;

And I implement a method of equating the two.  Is this right?

How would you normally store strings for different languages?  Is it acceptable to put the strings in an unencoded file on the end user's machine?

Increasing points to 100.
LVL 86

Expert Comment

ID: 6948319
>>Is it acceptable to put the strings in an unencoded file on the end user's machine?

I've seen that with several commercial products - just don't name them '*.txt', so the users don't tamper with them :o)

BTW, regarding the solution you mentioned - this is ideal for using a map, as a map also allows "gaps", IOW, doesn't require consecutive numbering...

Expert Comment

ID: 6949326
>>Is it acceptable to put the strings in an unencoded file on the end user's machine?

anouther opption i have seen is placing your error codes in a dll resorce. you could write a function you pass an error code to and have it return the error string. all that would be required then would be a new dll for each language.

if you want to do something like this a map would be ideal for you.

and if you want to go that rought you could move all your text resorces to that file and worry about transltion later.

Expert Comment

ID: 6952404
Actually we've implemented something like this.  We are parsing .rc files and all string resources into binary with it' numeric ids.  Then file translated (using small tool) it into several languages.  Then application reads file user specifies and automatically translate all text into that language.

To hold data in memory I've used vector, that hold user defined type, something like this:

class LanguageToken
UINT     m_nResourceId;
CString  m_csResourceString;

STL map or any other map can be used also, like it was mentioned before it is very convinient to associate resource id with resource text .

It works pretty well, we just have couple issues - like not all items in menus have ID.  So menu's have to translated by position, or contracted on a fly from string in the string table.  One other thing that all labels on all dialogs are placed with IDC_STATIC. ( or -1).  So you have to make sure that all labels you want to translate have to have own unique id.

Small probem with the way you want it implemented

IDC_BORED_ERROR     "you're too bored"

is that I don;t see the way how you going to find out wich control/text has IDC_BORED_ERROR associated with.  I couldn;t figure it out - so I resorted to storing it's numeric value.  That of cause forced me to parse resource.h file :-(.  

Of cause if all you want to do is just simplify translation process it is not an issue for you.

Hope it helps a little.

Author Comment

ID: 6963215
Thanks jkr.

mblat - good info.  I've dropped some points off for you at 


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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. …
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.
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.

837 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