Anonymous user
Module:Lang: Difference between revisions
Jump to navigation
Jump to search
synch from sandbox;
(fix variant length test;) |
(synch from sandbox;) |
||
Line 8:
local p = {};
local initial_style_state; -- set by
local getArgs = require ('Module:Arguments').getArgs;
Line 37:
Returns true if all of text argument is written using Latn script for letters, numbers and punctuationset; false else.
]]
p.is_latn = require ('Module:Unicode data').is_Latin
Line 162 ⟶ 115:
]]
local function validate_italic (
local properties = {['yes'] = 'italic', ['no'] = 'normal', ['unset'] = 'inherit', ['invert'] = 'invert', ['default'] = nil};
local count = 0
for _, arg in pairs {'italic', 'italics', 'i'} do
if args[arg] then
count = count + 1
end
end
if
return nil, '
end
return properties[args.italic or args.italics or args.i], nil;
end
--[=[--------------------------< V A L I D A T E _ C A T _ A R G S >----------------------------------------------------------
Default behavior of the {{lang}} and {{lang-xx}} templates is to add categorization when the templates are used in mainspace.
This default functionality may be suppressed by setting |nocat=yes or |cat=no. This function selects one of these two parameters
to control categorization.
Because having two parameters with 'opposite' names and 'opposite' values is confusing, this function accepts only affirmative
values for |nocat= and only negative values for |cat=; in both cases the 'other' sense (and non-sense) is not accepted and the
parameter is treated as if it were not set in the template.
Sets args.nocat to true if categorization is to be turned off; to nil if the default behavior should apply.
Accepted values for |nocat= are the text strings:
'yes', 'y', 'true', 't', on, '1' -- [[Module:Yesno]] returns logical true for all of these; false or nil else
for |cat=
'no', 'n', 'false', 'f', 'off', '0' -- [[Module:Yesno]] returns logical false for all of these; true or nil else
]=]
local function validate_cat_args (args)
if not (args.nocat or args.cat) then -- both are nil, so categorize
return;
end
local yesno = require "Module:Yesno";
if false == yesno (args.cat) or true == yesno (args.nocat) then
args.nocat = true; -- set to true when args.nocat is affirmative; nil else (as if the parameter were not set in the template)
else -- args.nocat is the parameter actually used.
args.nocat = nil;
end
end
Line 248 ⟶ 241:
because those parameters are superfluous to the IETF subtags in |code=)
returns six values; all lower case. Valid parts are returned as themselves; omitted parts are returned as empty strings, invalid
parts are returned as nil; the sixth returned item is an error message (if an error detected) or nil.
Line 270 ⟶ 263:
elseif source:match ('^%a%a%a?%-%a%a%a%a%-%d%d%d%-%d%d%d%d$') then -- ll-Ssss-DDD-variant (where region is 3 digits; variant is 4 digits)
code, script, region, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)%-(%d%d%d%d)$');
elseif source:match ('^%a%a%a?%-%a%a%a%a%-%a%a%-
code, script, region, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(%a%a)%-(
elseif source:match ('^%a%a%a?%-%a%a%a%a%-%d%d%d%-
code, script, region, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)%-(
elseif source:match ('^%a%a%a?%-%a%a%a%a%-%d%d%d%d$') then
code, script, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d%d)$');
elseif source:match ('^%a%a%a?%-%a%a%a%a%-
code, script, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(
elseif source:match ('^%a%a%a?%-%a%a%-%d%d%d%d$') then
code, region, variant = source:match ('^(%a%a%a?)%-(%a%a)%-(%d%d%d%d)$');
elseif source:match ('^%a%a%a?%-%d%d%d%-%d%d%d%d$') then
code, region, variant = source:match ('^(%a%a%a?)%-(%d%d%d)%-(%d%d%d%d)$');
elseif source:match ('^%a%a%a?%-%a%a%-
code, region, variant = source:match ('^(%a%a%a?)%-(%a%a)%-(
elseif source:match ('^%a%a%a?%-%d%d%d%-
code, region, variant = source:match ('^(%a%a%a?)%-(%d%d%d)%-(
elseif source:match ('^%a%a%a?%-%d%d%d%d$') then -- ll-variant (where variant is 4 digits)
code, variant = source:match ('^(%a%a%a?)%-(%d%d%d%d)$');
elseif source:match ('^%a%a%a?%-
code, variant = source:match ('^(%a%a%a?)%-(
elseif source:match ('^%a%a%a?%-%a%a%a%a%-%a%a$') then -- ll-Ssss-RR
Line 299 ⟶ 292:
code, script, region = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)$');
elseif source:match ('^%a%a%a?%-%a%a%a%a$') then
code, script = source:match ('^(%a%a%a?)%-(%a%a%a%a)$');
elseif source:match ('^%a%a%a?%-%a%a$') then
code, region = source:match ('^(%a%a%a?)%-(%a%a)$');
elseif source:match ('^%a%a%a?%-%d%d%d$') then
code, region = source:match ('^(%a%a%a?)%-(%d%d%d)$');
elseif source:match ('^%a%a%a?$') then
code = source:match ('^(%a%a%a?)$');
elseif source:match ('^%a%a%a?%-x%-
code, private = source:match ('^(%a%a%a?)%-x%-(
else
Line 420 ⟶ 413:
table.insert (out, '</span>');
if (0 == namespace) and not
table.insert (out, table.concat ({'[[Category:', category, ' template errors]]'}));
end
Line 666 ⟶ 659:
Italic rendering is controlled by the |italic= template parameter so italic markup should never appear in args.text
either as ''itself''' or as '''''bold italic''''' unless |italic=unset or |italic=invert.
]=]
Line 679 ⟶ 672:
end
local style = args.italic
if ('unset' ~= style) and ('invert' ~=style) then
if args.text:find ("%f[\']\'\'%f[^\']") or args.text:find ("%f[\']\'\'\'\'\'%f[^\']") then -- italic but not bold, or bold italic
Line 707 ⟶ 699:
end
if (0 < #maint_cats) and (0 == namespace) and not
for _, cat in ipairs (maint_cats) do
table.insert (maint, table.concat ({'[[Category:', cat, ']]'})); -- format and add the categories
Line 769 ⟶ 761:
return msg;
end
validate_cat_args (args); -- determine if categorization should be suppressed
args.rtl = args.rtl == 'yes'; -- convert to boolean: 'yes' -> true, other values -> false
Line 778 ⟶ 772:
end
args.italic, msg = validate_italic (args
if msg then
return make_error_msg (msg, args, 'lang');
Line 909 ⟶ 903:
args.link = args.link or args.links; -- prefer args.link
end
validate_cat_args (args); -- determine if categorization should be suppressed
args.rtl = args.rtl == 'yes'; -- convert to boolean: 'yes' -> true, other values -> false
Line 918 ⟶ 914:
end
args.italic, msg = validate_italic (args
if msg then
return make_error_msg (msg, args, 'lang-xx');
Line 1,065 ⟶ 1,061:
All code combinations supported by {{lang}} and the {{lang-xx}} templates are supported by this function.
Set invoke's |link= parameter to yes to get wikilinked version of the language name.
]]
Line 1,070 ⟶ 1,068:
function p.name_from_code (frame)
local subtags = {}; -- IETF subtags script, region, variant, and private
local raw_code =
local link = 'yes' == frame.args['link']; -- save a copy of the link-enable positional parameter (value can be anything)
local code; -- the language code
local msg; --
local language_name = '';
code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (raw_code);
if msg then
local template = (frame.args['template'] and table.concat ({'{{', frame.args['template'], '}}: '})) or ''; -- make template name (if provided by the template)
return table.concat ({'<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: ', template, msg, '</span>'});
end
Line 1,095:
language_name = language_name:gsub ('%s+%b()', ''); -- remove IANA parenthetical disambiguators or qualifiers from names that have them
if link then -- when |link=yes, wikilink the language name
if language_name:find ('languages') then
return make_wikilink (language_name); -- collective language name uses simple wikilink
else
return make_wikilink (language_name .. ' language', language_name); -- language name with wikilink
end
end
return language_name;
Line 1,103 ⟶ 1,111:
--[[--------------------------< T R A N S L >------------------------------------------------------------------
]]
Line 1,145 ⟶ 1,153:
end
args.italic, msg = validate_italic (args
if msg then
return make_error_msg (msg, args, 'transl');
|