Module:Lang/utilities
Appearance
require('Module:No globals');
local getArgs = require ('Module:Arguments').getArgs;
local unicode = require ("Module:Unicode data"); -- for is_latin()
local lang = require ('Module:Lang');
local namespace = mw.title.getCurrentTitle().namespace; -- used for categorization
--[[--------------------------< M A K E _ E R R O R _ M S G >--------------------------------------------------
assembles an error message from template name, message text, help link, and error category.
]]
local function make_error_msg (msg, template)
local out = {};
local category = 'Lang and lang-xx';
table.insert (out, table.concat ({'<span style=\"color:#d33\">Error: {{', template, '}}: '}));
table.insert (out, msg);
table.insert (out, table.concat ({' ([[:Category:Lang and lang-xx template errors|help]])'}));
table.insert (out, '</span>');
if (0 == namespace or 10 == namespace) and not args.nocat then -- categorize in article space (and template space to take care of broken usages)
table.insert (out, table.concat ({'[[Category:Lang and lang-xx template errors]]'}));
end
return table.concat (out);
end
--[[--------------------------< G E T _ S E P A R A T O R >----------------------------------------------------
allowed separators are comma, semicolon, virgule, or a quoted string of text
]]
local function get_separator (sep_param_val)
if not sep_param_val then return ', ' end; -- not specified, use default
if ',' == sep_param_val then return ', ' end;
if ';' == sep_param_val then return '; ' end;
if '/' == sep_param_val then return '/' end;
local str;
if sep_param_val:match ('^%b\"\"$') then -- if quoted string and uses double quotes
str = sep_param_val:match ('%b\"\"'):gsub ('^"', ''):gsub ('"$', '');
return str; -- return the contents of the quote
elseif sep_param_val:match ('^%b\'\'$') then -- if quoted string and uses single quotes
str = sep_param_val:match ('%b\'\''):gsub ('^\'', ''):gsub ('\'$', '');
return str; -- return the contents of the quote
end
return ', '; -- default in case can't extract quoted string
end
--[[--------------------------< _ L A N G _ X 2 >--------------------------------------------------------------
mimics {{lang|<code>|<text1>|<text2>}} except that <text2> is not a romanization of <text1> (which is always the
Latin-script form). Intended for languages where two scripts are 'official' or 'native and equal in status' (sh
is and sr may be candidates for this; are there others?)
{{lang_x2|<code>|<text1>|<text2>|first=[1|2]|script2=<script>|separator=[,|;|/|<quoted string>]}}
<code> - (required) language code for both of <text1> and <text2>
<text1> - (required) Latin-script text (always)
<text2> - (required) non-Latin-script text (always)
|first= - the assigned value specifies which of <text1> or <text2> is rendered first; accepts '1' or '2'; default '1' (Latin-script text);
|script2= - ISO 15924 script tag for <text2>
|separator = when single character ,;/ uses ', ' (default), '; ', '/'; when quoted string (first and last characters must be matching single or double quotes) uses that string
this function calls:
Module:Lang functions
_lang_xx_inherit - when non-Latin <text2> renders first
_lang_xx_italic - when Latin <text1> renders first
_lang - to render <text2>
Module:Unicode data function:
is_Latin - error checking to make sure that <text1> is Latin-script text
]]
local function _lang_x2 (args_t)
if not (args_t[1] and args_t[2] and args_t[3]) then
return make_error_msg ('missing a required argument', 'lang-x2');
end
if not unicode.is_Latin (args_t[2]) then
return make_error_msg ('<code style="color: inherit; background: inherit; border: none; padding: inherit;"><text1></code> is not Latin script', 'lang-x2');
end
if unicode.is_Latin (args_t[3]) then
return make_error_msg ('<code style="color: inherit; background: inherit; border: none; padding: inherit;"><text2></code> is Latin script', 'lang-x2');
end
local first = args_t.first or '1';
local out = {};
local code = args_t[1];
local latin = args_t[2];
local text2 = args_t[3];
local script2 = (args_t.script2 and ('-' .. args_t.script2) or '');
if '1' == first then -- Latin script renders first
table.insert (out, lang._lang_xx_italic ({['code']=code, ['text']=latin}));
table.insert (out, get_separator (args_t.separator));
table.insert (out, lang._lang ({['code']=code..script2, ['text']=text2}));
else -- non-Latin script renders first
table.insert (out, lang._lang_xx_inherit ({['code']=code..script2, ['text']=text2}));
table.insert (out, get_separator (args_t.separator));
table.insert (out, lang._lang ({['code']=code, ['text']=latin}));
end
return table.concat (out)
end
--[[--------------------------< L A N G _ X 2 >----------------------------------------------------------------
template entry point
]]
local function lang_x2 (frame)
local args_t = getArgs (frame);
return _lang_x2 (args_t);
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
return {
lang_x2 = lang_x2,
}