String table

Posted on 2002-04-17
Medium Priority
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
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
LVL 86

Accepted Solution

jkr earned 400 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...

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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 http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20292724 


Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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.
Suggested Courses

764 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