Runtime language switch for labs: dictionaries, lookup and number format. More...
| Import Statement: | import Clayground.Lab |
A lab that is meant to be published to a classroom needs its wording in the reader's language, switchable while it runs. Qt's own qsTr route cannot do that here: retranslating a live QML engine is a C++ call on the engine, and a lab hosted by the dojo (or exported to WASM) never owns that engine. So translation is a runtime dictionary instead, and every string is an ordinary binding that re-evaluates when lang changes.
Dictionaries are registered by whoever owns the vocabulary - a kit registers its part names, the lab registers its own UI copy:
import Clayground.Lab
import "strings.js" as Strings
Component.onCompleted: LabLang.register(Strings.dict)
Text { text: LabLang.t("hint.wire") }
Text { text: LabLang.num(4.32, 2) + " V" } // "4,32 V" in German
See also Lab and LangSwitch.
decimalPoint : string |
Decimal separator of the active language.
lang : string |
Active language code, e.g. "en" or "de".
languages : var |
Language codes offered by the registered dictionaries.
string langName(string code) |
Display name for a language code (the code itself if unknown).
string num(real v, int digits) |
Formats a number in the active language's decimal notation.
digits is optional; without it the value is printed as-is. German (and any language whose separator is not ".") gets a decimal comma, so readouts match what a student writes in their exercise book.
string qty(real v, string unit, int digits) |
A quantity with its SI prefix chosen for readability.
qty(0.05, "A") is "50.0 mA", qty(1500, "Ω") is "1.50 kΩ", and both follow the language's decimal separator. Every lab so far hand-rolled its own mA/A crossover, differently and once per unit; this is that rule, in one place, with a node suite behind it.
digits is optional - without it the value gets three significant figures, which is what an instrument reading is worth. Units that cannot take a prefix ("%", "/min", a bare count) are printed as they stand.
See also num().
var qtyParts(real v, string unit, int digits) |
qty() split into {number, prefix, unit, fullUnit}.
For a readout that sets the number and the unit in different type - a gauge, a big status figure - so it never has to re-split a formatted string to get there.
void register(var dict) |
Merges {lang: {key: text}} into the registry.
Later registrations win on conflicting keys, so a lab may override a kit's wording. Registering also extends languages.
string t(string key) |
The key's text in the active language.
Falls back to English and then to the key itself, so a missing translation shows up as the key rather than as an empty label.
string tf(string key, ...) |
t() with %1, %2 ... replaced by the extra arguments.