Translation Catalog And Plurals
An EA or panel with any text on it eventually gets asked for a second language, and the usual answer is a tangle of if(language == "ru") branches scattered across the source, one per string, drifting out of sync every time the English text changes. This header separates the text from the code instead: strings live in an external catalog file, one key per line, and the program looks each one up by key. The catalog format is deliberately plain - key = value, with # comments and blank lines ignored, so it can be edited in Notepad and does not require any particular authoring tool. {0}, {1} and {2} placeholders are substituted after lookup via Format1/Format2/Format3, kept as separate small functions rather than one variadic call because neither MQL4 nor MQL5 has variadic functions or templates for an arbitrary argument count. Plural handling covers the common case explicitly: n == 1 looks up the plain key, anything else looks up key.plural, falling back to the singular form if no plural entry exists rather than surfacing a blank string. This is honestly a binary English-style rule, not a full CLDR plural-rules engine - languages with three or more plural categories (Russian, Arabic, Polish) need a caller that selects the right category itself and looks up that key directly; Tn() does not attempt to guess which category a number falls into beyond one-versus-not-one. Every lookup falls back gracefully rather than throwing or crashing: a missing key returns the caller's supplied default, or the key itself if no default was given, so a language pack that has not caught up with the latest strings degrades to readable placeholder text instead of an empty label. MissCount() reports how many lookups fell back, useful for a translator's QA pass to find every untranslated string in one run. Notes and limitations - Plural handling is binary (n==1 vs not), not a CLDR-compliant plural-rules engine. Languages with more plural categories need the caller to pick the category and look up that key directly instead of relying on Tn(). - Placeholder substitution is capped at three positional arguments (Format1/Format2/Format3) because neither platform has variadic functions. Compose a template with fewer placeholders, or extend with a Format4 following the same pattern, if a string genuinely needs more. - Key lookup is a linear scan over the loaded catalog - fine for the hundreds of strings a typical panel or EA needs, not designed as a general-purpose key-value store for tens of thousands of entries. - Files are read from the platform's sandbox (Files\ or a Common\ path if opened with FILE_COMMON), the same restriction every MQL file function has on both platforms - a catalog cannot be loaded from an arbitrary path on disk. - Not thread-relevant: Load() and every lookup run synchronously on the calling thread.
Script · MT5 · v1.0 · Free
About Translation Catalog And Plurals
Translation Catalog And Plurals is a script for MetaTrader 5. MQL source code included.
Files in the download: TranslationCatalog.mq5.mqh, TranslationCatalog.txt, TranslationCatalogDemo.ex5, TranslationCatalogDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Alerts · MT5 Script · MQL5 Source Code