Modula:BDD/localize
This is a submodule for Behavior-driven development to do localization.
All localizations are found at Special:Prefixindex/Module:BDD/localize/, that is the subpages with names given as language codes.
Usage
rievdatThere is a message "xit", and it can be accessed like the following
local g = require 'Module:BDD/localize'
local xit = g('xit')
The same message can be overridden with a system message Mediawiki:bdd-xit. An override set as a system message is "hard" and will override any message set in the subpages. Usually it is better to redefine the message in the subpages for localization, and only set a message in the Mediawiki space if it is really important to use that spesific form.
-- submodule for Behavior Driven Development testing framework
-- © John Erling Blad, Creative Commons by Attribution 3.0
local localize = {}
-- @var contLang the language used for localization
-- @todo should adapt to the user language when called through the api
localize.contLang = mw.language.getContentLanguage()
-- @var languages the chain of fallback languages to use given the content language
localize.languages = localize.contLang:getFallbackLanguages() or {}
-- the fallback languages must be prepended with the content language
table.insert( localize.languages, 1, localize.contLang:getCode() )
-- @var fallbacks the message sets in the sequence given by the language sequence
localize.fallbacks = {}
-- load each of the message sets in sequence
for i,v in ipairs(localize.languages) do
mw.log(v)
localize.fallbacks[i] = mw.loadData('Module:BDD/localize/'..v) or {}
end
-- metatable for the export
local mt = {
__call = function ( self, key, ...)
local msg = mw.message.new( 'bdd-' .. key, ... )
-- if blank, then try to find in our localization files
if not msg or msg:isBlank() then
local keep = msg
for i,v in ipairs(localize.languages) do
local str = localize.fallbacks[i][key]
if str then
msg = mw.message.newRawMessage( str, ... )
if msg and msg:exists() then
return msg
end
end
end
msg = keep
end
assert(msg, "invalid message: " .. key .. " is not a defined string" )
-- this will also return blank messages!
return msg
end
}
-- install the metatable
setmetatable(localize, mt)
return localize