webmcp

changeset 563:2c31275322db

Allow inheritance in translation tables using __parent
author jbe
date Sun Mar 07 16:58:59 2021 +0100 (2021-03-07)
parents 328f120924a2
children 6141ab127861
files framework/bin/langtool.lua framework/env/__init.lua framework/env/locale/_get_translation_table.lua
line diff
     1.1 --- a/framework/bin/langtool.lua	Fri Feb 05 15:51:39 2021 +0100
     1.2 +++ b/framework/bin/langtool.lua	Sun Mar 07 16:58:59 2021 +0100
     1.3 @@ -123,9 +123,7 @@
     1.4  end
     1.5  
     1.6  local function update_translation(key, value)
     1.7 -  if #directories > 0 then
     1.8 -    if translations[key] ~= nil then translations[key] = value end
     1.9 -  else
    1.10 +  if translations[key] ~= nil or #directories == 0 or key == "__parent" then
    1.11      translations[key] = value
    1.12    end
    1.13  end
     2.1 --- a/framework/env/__init.lua	Fri Feb 05 15:51:39 2021 +0100
     2.2 +++ b/framework/env/__init.lua	Sun Mar 07 16:58:59 2021 +0100
     2.3 @@ -13,6 +13,9 @@
     2.4  
     2.5  Hint: Lua's syntax rules allow to write _"text" as a shortcut for _("text"), or _'text' instead of _('text') respectivly.
     2.6  
     2.7 +Translation files are located in the "locale" directory of the application and must have a name following the schema "translations.LANG.lua", where LANG is the language code set with locale.set{lang = ...}.
     2.8 +The special string "__parent" in the translation file may point to a different language code of which all translations are inherited from.
     2.9 +
    2.10  --]]--
    2.11  
    2.12  function _G._(text, replacements)
     3.1 --- a/framework/env/locale/_get_translation_table.lua	Fri Feb 05 15:51:39 2021 +0100
     3.2 +++ b/framework/env/locale/_get_translation_table.lua	Sun Mar 07 16:58:59 2021 +0100
     3.3 @@ -17,6 +17,15 @@
     3.4      if type(translation_table) ~= "table" then
     3.5        error("Translation file did not return a table.")
     3.6      end
     3.7 +    local parent_code = translation_table["__parent"]
     3.8 +    if parent_code then
     3.9 +      local parent_table = locale._get_translation_table(parent_code)
    3.10 +      for key, value in pairs(parent_table) do
    3.11 +        if translation_table[key] == nil then
    3.12 +          translation_table[key] = value
    3.13 +        end
    3.14 +      end
    3.15 +    end
    3.16      locale._translation_tables[language_code] = translation_table
    3.17      return translation_table
    3.18    else

Impressum / About Us