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 typeFunction and summary
Lexiconnew(Dialog dialog, string language, string path, string prefix)
Handle lexicon for the dialog, in the given language
boolentryExist(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()
end
This will create a file lex_[language]_missing.txt you can copy/past into lex_[language].txt.
stringtranslate(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

ParameterTypeDefaultDescription
dialogDialognilDialog for script with window (.myf and .myc), nil for script without window (.mys) Lexicon is then available in Dialog.UserTable.Lexicon
languagestringApplication.InterfaceLanguage"en", "fr", "es"... nil use Harmony's interface language.
pathstringGetCurrentPath()Path to search lexicon file for, nil looks in the script directory.
prefixstring"lex_"Prefix of file names. File to open = prefix.."_"..language ..".txt"
Return
Lexicon:

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.

ParameterTypeDefaultDescription
entrystring  
Return
bool: 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.

ParameterTypeDefaultDescription
entry  One entry (string) or multiple entry (table of string). The first found is returned.
defaultValuestringnilDefault 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()
end
This 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

ParameterTypeDefaultDescription
itemDialogItem  
Example
local item = Dialog.FirstDialogItem
while item do
dialog.UserTable.Lexicon:translateItem(item)
item = item.Next
end