Class Lexicon
Lexicon object that call Dialog.Translate
for all texts of an item (caption, context help, items, tab labels...), and reports missing entries in lexicon file.
Difference between this class and the built-in Dialog.Translate(..)
is that you can use translation outside dialog.
There is also a reporting of missing entries to help you fully translate your script.
By convention, lexicon files are named lex_en.txt (English), lex_fr.txt (French), lex_es.txt (Spanish) and are placed in the same directory than the script.
The file content is like this:
-- comment or blank first line, there is a bug that ignore first line -- You can use itemName==The item caption Button 1==Ouvrir Button 2==Fermer -- you can add context help to your buttons Button 1.Help==The contextual help of button 1 -- You can use the captions set in the dialog builder instead Open==Ouvrir Close==Fermer -- SMDialog objects are handled, of course myButton.Caption==A two lines\ncaption mySlider.Help==[[A very long very very long text]]
SMDialog automatically use lexicon, you don't need to instantiate Lexicon object, it's done for you.
If you want to debug your script in Spanish without switching Harmony's language to Spanish, you can force it this way:
IncludeOnce("SMLexicon") Lexicon:new(dialog, "es")
If you want to use lexicon without SMDialog:
IncludeOnce("SMLexicon") local lex = Lexicon:new(dialog) local myTranslatedText = dialog.UserTable.Lexicon:translate("myText")
If you want to translate messages in a script without screen (.mys) in console or alerts:
IncludeOnce("SMLexicon") local lex = Lexicon:new() -- Lexicon:new(nil, "es") to try Spanish translation local myTranslatedText = lex:translate("myText")
Summary
Return type | Function and summary |
---|---|
Lexicon | new(Dialog dialog, string language, string path, string prefix) Handle lexicon for the Dialog, in the given language |
addEntries(string lang, table entries) Add entries "on the fly", convenient way for small scripts without lexicon file. | |
boolean | entryExist(string entry) Check if entry exists in lexicon If entry is numeric, it's considered as existing in lexicon, even if it doesn't need translation. |
reportMissingEntries() In debug mode, add this code to Exit function function Exit(dialog) dialog.UserTable.Lexicon:reportMissingEntries() endThis will create a file lex_[language]_missing.txt you can copy/past into lex_[language].txt. | |
string | translate(entry, string defaultValue) Translate entry, and if missing from lexicon, use defaultValue. |
translateItem(DialogItem item) Translate captions and help of a DialogItem. |
Methods
Lexicon:new(Dialog dialog, string language, string path, string prefix)
Handle lexicon for the Dialog, in the given language
Parameter | Type | Default | Description |
---|---|---|---|
dialog | Dialog | nil | Dialog for script with window (.myf and .myc), nil for script without window (.mys) Lexicon is then available in Dialog.UserTable.Lexicon |
language | string | Application.InterfaceLanguage | "en", "fr", "es"... nil use Harmony's interface language. |
path | string | GetCurrentPath() | Path to search lexicon file for, nil looks in the script directory. |
prefix | string | "lex_" | Prefix of file names. File to open = prefix.."_"..language ..".txt" |
- Return
- Lexicon: Lexicon
Lexicon:addEntries(string lang, table entries)
Add entries "on the fly", convenient way for small scripts without lexicon file.
This is not usable for lexicon on Dialog.
Parameter | Type | Default | Description |
---|---|---|---|
lang | string | Language 2-char code "fr", "es", "it"... | |
entries | table | Table where each item is a 2-items table. |
- Example
lex = Lexicon:new() lex:addEntries("fr", { {"Please open a score before runing this script", "Veuillez ouvrir une partition avant de lancer ce script"}, {"English term", "French term"} }) lex:addEntries("it", {{"english term 1", "italian term 1"}, {"english term 2", "italian term 2"}})
Lexicon:entryExist(string entry)
Check if entry exists in lexicon If entry is numeric, it's considered as existing in lexicon, even if it doesn't need translation.
Parameter | Type | Default | Description |
---|---|---|---|
entry | string | entry |
- Return
- boolean:
true
if exists,nil
for false otherwise. - Error
- if entry is nil or not a string
Lexicon:translate(entry, string defaultValue)
Translate entry, and if missing from lexicon, use defaultValue.
If entry is numeric, it's returned as is, no translation needed.
Parameter | Type | Default | Description |
---|---|---|---|
entry | One entry (string) or multiple entry (table of string). The first found is returned. | ||
defaultValue | string | nil | Default value if no entry is found.. If nil and no entry found, return the first entry not translated. |
- Return
- string: Translation, or defaultValue.
- Error
- if on of the entries is nil or not a string
- Example
- local item = dialog.GetItem("ButtonOpen") item.Caption = dialog.UserTable.Lexicon:translate({ item.Name, "Open"}, "Open") --> if ButtonOpen==Choisir un fichier is found, then return "choisir un fichier" in priority. --> else if Open==Ouvrir is found, it returns Ouvrir --> else return "Open", the default value, and will create ButtonOpen==Open in lex_fr_missing.txt.
Lexicon:reportMissingEntries()
In debug mode, add this code to Exit function
function Exit(dialog) dialog.UserTable.Lexicon:reportMissingEntries() endThis will create a file lex_[language]_missing.txt you can copy/past into lex_[language].txt.
It also print infos in the console to help you. enjoy
Lexicon:translateItem(DialogItem item)
Translate captions and help of a DialogItem.
- Caption
- ContextHelp
- ExtraValues for popup menus, lists, and tab views
Parameter | Type | Default | Description |
---|---|---|---|
item | DialogItem | item |
- Example
local item = Dialog.FirstDialogItem while item do dialog.UserTable.Lexicon:translateItem(item) item = item.Next end