C ++ conditional includes file runtime

I am working on a game that is encoded in C ++, and I would like you to change the language at runtime. Currently, a language is selected at compile time, including a header file (which has language definitions), for example:

#include "lan_eng.h" 

Therefore, the game does not allow changing the language after the client has been compiled. My question is, is there a way to include files at runtime at runtime? I am new to C ++, so at first I thought 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 

Of course, this simplifies maintenance, but the problem is that I believe that it only works at compile time. I would like to be able to store the selected language in a variable (which changes at runtime), and then use this variable to choose which header file to include. Is there a way to do this with header files, or will I have to make a class?

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

Thanks in advance!

+4
source share
4 answers

You cannot change #include at runtime because they are evaluated only at compile time.

Instead, you can use "language sheets". You can create a system that you refer to at run time, or you can have several text documents (such as .xml files) that you read and save when you want to change languages. Performing this method also allows your application to be extended by software users in the future, as it will not be hard-coded.

If you create it this way, you can save all your β€œlanguage sheets” in one folder, and then ask your program to check this folder for all available language sheets. This will allow users to add languages ​​to your program.

What you basically need to do is create a table of strings that can be executed as part of a hard-coded method or as part of a more dynamic method (referred to as language sheets).

+5
source

You cannot do this with #include , just for compilation. More specifically, it is only this pre-processor that occurs before compilation.

To get the change at runtime, you have to move your translations to a text file (maybe something like xml, but plain text works) and configure the system to load the file at startup to fill in the lines in the code that needs translation.

It also means that during startup all your lines will have a dynamic size, so the initialization period of the code will increase. But this sometimes means flexibility.

+3
source

Perhaps the easiest way I've been thinking about is:

 struct language { virtual str::string greeting() =0; virtual str::string greeting(const std::string& name) =0; virtual str::string goodbye() =0; virtual ~language() {} }; struct English_language { virtual str::string greeting() {return "Hello";} virtual str::string greeting(const std::string& name) {return "Hello "+name;} virtual str::string goodbye() {return "Goodbye";} } English; struct German_language { virtual str::string greeting() {return "Hallo";} virtual str::string greeting(const std::string& name) {return name+" Hallo";} virtual str::string goodbye() {return "Auf Wiedersehen";} } German; language* CurLanguage = &English; int main() { std::cout << CurLanguage->greeting("Steve") << '\n'; CurLanguage = &German; std::cout << CurLanguage->goodbye() << '\n'; } 

[EDIT] I rewrote it from scratch, as I realized that pure virtual functions are a way of compile-time error if you miss the sentence, which simplifies maintenance. This version also has the ability to handle variables (dates, names, times, numbers, etc.) neatly. This concept is based on what we use at my work for over 2900 phrases / sentences in ~ 20 languages.

+3
source

Pre-processing (#include, #ifdef, #define, etc.) actually happens before compilation. You can think of preprocessing as a replacement for text, the output of which is the source code supplied to the compiler. #defines etc. occupy a separate namespace from the variables in your program, and by the time your program is compiled, all this is set in stone. In other words, what you ask is IMPOSSIBLE.

To better understand this, look at the parameters of your compiler and find an option that allows you to save the pre-processed output.

Instead, you need to change the way you handle strings. Instead of changing lines at compile time, you need to do something at runtime.

Test your platform - on most platforms there are APIs for localization. However, they vary across the platform, so if you are building a cross-platform application, you may have to minimize your own.

0
source

Source: https://habr.com/ru/post/1391885/


All Articles