webmcp
annotate framework/env/locale/_get_translation_table.lua @ 309:4e69ce9a6365
Mechanism to load, compile, and cache Lua code from the filesystem
author | jbe |
---|---|
date | Sun Mar 22 22:25:37 2015 +0100 (2015-03-22) |
parents | 2cb27106aa73 |
children | ba68ef9e7c90 |
rev | line source |
---|---|
jbe/bsw@0 | 1 function locale._get_translation_table() |
jbe/bsw@0 | 2 local language_code = locale.get("lang") |
jbe/bsw@0 | 3 if language_code then |
jbe/bsw@0 | 4 if type(language_code) ~= "string" then |
jbe/bsw@0 | 5 error('locale.get("lang") does not return a string.') |
jbe/bsw@0 | 6 end |
jbe/bsw@0 | 7 local translation_table = locale._translation_tables[language_code] |
jbe/bsw@0 | 8 if translation_table then |
jbe/bsw@0 | 9 return translation_table |
jbe/bsw@0 | 10 end |
jbe@209 | 11 local filename = encode.file_path(WEBMCP_BASE_PATH, "locale", "translations." .. language_code .. ".lua") |
jbe@309 | 12 local func, load_errmsg = loadcached(filename) |
jbe@309 | 13 if not func then |
jbe@309 | 14 error('Could not load translation file "' .. filename .. '": ' .. load_errmsg) |
jbe@64 | 15 end |
jbe/bsw@0 | 16 translation_table = func() |
jbe/bsw@0 | 17 if type(translation_table) ~= "table" then |
jbe/bsw@0 | 18 error("Translation file did not return a table.") |
jbe/bsw@0 | 19 end |
jbe/bsw@0 | 20 locale._translation_tables[language_code] = translation_table |
jbe/bsw@0 | 21 return translation_table |
jbe/bsw@0 | 22 else |
jbe/bsw@0 | 23 return locale._empty_translation_table |
jbe/bsw@0 | 24 end |
jbe/bsw@0 | 25 end |