The class is designed for localization of messages in MQL5 programs. In brief, all that outputs in the log with the help of Print() function and also text messages displayed in Alert(), Comment(), MessageBox() can be done multilingual in one code.
- bool init(string progName, ENUM_LANGUAGES lang) - initialize the object. In the entry we set the name of the program and the code of the language from tabulation in the LanguagesEnum.mqh file.
- string tr(string str) - translates the str* string.
- void print(string str) - types the translation of the str* string in the log.
- void alert(string str) - displays the alert with the translated string*.
- void comment(string str) - displays the comment with the translated string*.
- int messageBox(string text, string caption=NULL, int flags=0) - displays the translated text* in the message window and translates the title of the window*. Returns one of the MessageBox() return codes. As well as the MessageBox(), this method can not be fetched from user indicators.
- * - If the language file does not have the equivalent translation then it returns/displays entering string.
- Download Translator.mqh and LanguagesEnum.mqh files to the MetaTrader 5\MQL5\Include folder. The class itself is located in the first file, the list of languages is located in the second file in accordance with ISO 639 standard (at this stage only the languages to which the terminal is translated are listed in the file. If necessary you can add the code of any other language, preferably in accordance with ISO 639 standard to avoid confusion);
- Create a text file translation for "its" language in the format of Alias = Translation, where Alias is an aliased name of the translatable string and also a string-argument by which class methods are subsequently called, Translation is the translation of the text which will be eventually displayed;
- Language file(s) coding: UTF8(without BOM);
- The file name must strictly correspond to the following format: <MQL5 code name>.<Language code in latin capital letter>.ini. For example, the text file with the translation into Russian for MyCode code must be called MyCode.RU.ini. Eventually, the number of language files must correspond to the number of languages into which you want to translate messages;
- Place created language files to MetaTrader 5\MQL5\Files\Languages folder.
In the code:
- Connect the translator file (# include <Traslator.mqh>) in the code (The Expert Advisor, indicator, script);
- Create an object. For example, CTranslator ru;
- Add an input parameter for the translation. For example, input ENUM_LANGUAGES InpToLanguage = RU;
- Initialize the object. For example, ru.init(MQL5InfoString(MQL5_PROGRAM_NAME), InpToLanguage);
- Use any of these methods if necessary. For example, ru.print("Your text").
It is important if needed file does not exist or the file does not have a suitable alias, then the string passed to the method as an argument will be displayed.
For an illustrative example the Sample.mq5 script and two language files are attached to it to translate into Russian and Japanese. The result of the work of the script (at first it ran in the chart with the parameter to translate into Russian, then into Japanese):
Although aliases can be written in any language and also alias may just be an abbreviation instead of a whole phrase. It is better to write in English and without abbreviation. Thus:
- even if there is no language file, all messages will be displayed in English,
- the need to write a special file for translation into English is eliminated automatically (those who wrote under Joomla CMS extensions probably understand it easier, as the logic is the same).