Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ conditional include file runtime

Tags:

c++

include

I am working on a game that is coded in C++ and I would like to make it possible to change language at runtime. Currently, the language is chosen at compile time by including a header file (which has language definitions), like so:

#include "lan_eng.h"

Therefore, the game does not allow the language to be changed once the client has been compiled. My question is if there is a way in which I can include files conditionally at runtime? I am new to C++, so at first I was thinking that I could do something like this:

#define DEF_LANGUAGE_ENG
//#define DEF_LANGUAGE_DEN

#ifdef DEF_LANGUAGE_ENG

    #include "lan_eng.h"

#endif

#ifdef DEF_LANGUAGE_DEN

    #include "lan_den.h"

#endif

Sure that makes it easier to maintain, but the problem is that I believe it only works at compile time. I would like to be able to store the chosen language in a variable (which is changed at runtime) and then use that variable to choose which header file to include. Is there a way to do this with header files, or would I be forced to make a class?

I hope it is not a stupid question; my searches did not give me the results I was hoping for.

Thanks in advance!

like image 701
ba0708 Avatar asked Jan 19 '12 17:01

ba0708


1 Answers

You can not change #include's at runtime due to them being evaluated during compile time only.

What you can do instead, is have "language sheets". You can build a system that you reference during runtime, or you can have several text documents (such as .xml files) that you read and store when you want to change languages. Doing it this way also allows your application to be extended upon by users of the software in the future, as it will not be hardcoded.

If you design it in such a way, you could keep all your "language sheets" in one folder, then have your program check that folder for all available language sheets. This will allow users to also add languages to your program.

What you will basically need to do is create a string table, which can be done as part of a hardcoded method, or as part of a more dynamic method (mentioned as the language sheets).

like image 172
josephthomas Avatar answered Sep 21 '22 17:09

josephthomas