Module:Lang: Difference between revisions

documentation; better error messages; comment out code that is redundant;
m (documentation;)
(documentation; better error messages; comment out code that is redundant;)
Line 127:
if not is_set (source) then
return nil, nil, nil, nil, 'missing language tag';
end
 
if source:match ('^%a%a%a?%-%a%a%a%a%-%a%a%-%d%d%d%d$') then -- ll-Ssss-RR-variant (where variant is 4 digits)
code, script, region, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(%a%a)%-(%d%d%d%d)$');
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%-[%a%d][%a%d][%a%d][%a%d][%a%d]+$') then -- ll-Ssss-RR-variant (where variant is 5-8 alnum characters)
Line 139:
code, script, region, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-(%d%d%d)%-([%a%d][%a%d][%a%d][%a%d][%a%d][%a%d]?[%a%d]?[%a%d]?)$');
 
elseif source:match ('^%a%a%a?%-%a%a%a%a%-%d%d%d%d$') then -- ll-Ssss-variant (where variant is 4 digits)
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%-[%a%d][%a%d][%a%d][%a%d][%a%d]+$') then -- ll-Ssss-variant (where variant is 5-8 alnum characters)
code, script, variant = source:match ('^(%a%a%a?)%-(%a%a%a%a)%-([%a%d][%a%d][%a%d][%a%d][%a%d][%a%d]?[%a%d]?[%a%d]?)$');
 
elseif source:match ('^%a%a%a?%-%a%a%-%d%d%d%d$') then -- ll-RR-variant (where variant is 4 digits)
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 -- ll-DDD-variant (where region is 3 digits; variant is 4 digits)
Line 178:
 
else
return nil, nil, nil, nil, table.concat {'unrecognized language tag: ', source}; -- don't know what we got but it is malformed
end
 
code = code:lower(); -- ensure that we use and return lower case version of this
if not (lang_data.override[code] or lang_name_table.lang[code]) then
return nil, nil, nil, nil, table.concat {'unrecognized language code: ', code}; -- invalid language code, don't know about the others (don't care?)
end
 
if is_set (script) then
if is_set (args_script) then
return code, nil, nil, nil, 'redundant script tag'; -- both code with script and |script= not allowed
end
else
Line 198:
script = script:lower(); -- ensure that we use and return lower case version of this
if not lang_name_table.script[script] then
return code, nil, nil, nil, table.concat {'unrecognized script: ', script, ' for code: ', code}; -- language code ok, invalid script, don't know about the others (don't care?)
end
end
Line 204:
if is_set (region) then
if is_set (args_region) then
return code, nil, nil, nil, 'redundant region tag'; -- both code with region and |region= not allowed
end
else
Line 213:
region = region:lower(); -- ensure that we use and return lower case version of this
if not lang_name_table.region[region] then
return code, script, nil, nil, table.concat {'unrecognized region: ', region, ' for code: ', code};
end
end
Line 219:
if is_set (variant) then
if is_set (args_variant) then
return code, nil, nil, nil, 'redundant variant tag'; -- both code with variant and |variant= not allowed
end
else
Line 227:
if is_set (variant) then
variant = variant:lower(); -- ensure that we use and return lower case version of this
if not lang_name_table.variant[variant] then -- make sure variant is valid
return code, script, region, nil, table.concat {'unrecognized variant: ', variant};
end -- does this duplicate/replace tests in lang() and lang_xx()?
if is_set (script) then -- if script set it must be part of the 'prefix'
if not in_array (code, lang_name_table.variant[variant]['prefixes']) and not in_array (table.concat ({code, '-', script}), lang_name_table.variant[variant]['prefixes']) then
if not in_array (table.concat ({code, '-', script}), lang_name_table.variant[subtags.variant]['prefixes']) then
return code, script, region, nil;
return code, script, region, nil, table.concat {'unrecognized variant: ', variant, ' for code-script pair: ', code, '-', script};
end
else
if not in_array (code, lang_name_table.variant[variant]['prefixes']) then
return code, script, region, nil, table.concat {'unrecognized variant: ', variant, ' for code: ', code};
end
end
-- if not lang_name_table.variant[variant] then
-- return code, script, region, nil;
-- end -- does this duplicate/replace tests in lang() and lang_xx()?
-- if not in_array (code, lang_name_table.variant[variant]['prefixes']) and not in_array (table.concat ({code, '-', script}), lang_name_table.variant[variant]['prefixes']) then
-- return code, script, region, nil;
-- end
end
 
Line 281 ⟶ 293:
 
--[[--------------------------< M A K E _ T E X T _ S P A N >--------------------------------------------------
 
TODO: replace wiki italic markup with html tags?
TODO: if wikimarkup replaced with html tags, don't need span tag, put lang= attribute in the html: <i lang="language name"> ...
TODO: add support for block: div tags instead of span tags; would need some sort of proper parameter to control the switch
 
]]
Line 505 ⟶ 521:
local subtags = {};
local code;
local msg;
 
code, subtags.script, subtags.region, subtags.variant, msg = get_ietf_parts (args.code); -- |script=, |region=, |variant= not supported becusebecause they should be part of args.code ({{{1}}})
 
if not (code and subtags.script and subtags.region and subtags.variant) then
-- return make_error_msg (table.concat ({'{{lang}}: unknown language code: ', args.code or 'missing'}), args.nocat);
return make_error_msg (table.concat ({'{{lang}}: invalid code-variant combination: ', code, '-', subtags.variantmsg}), args.nocat);
end
 
Line 522 ⟶ 540:
end
local msg = validate_text ('lang', args); -- ensure that |text= is set (italic test disabled for the time being)
if is_set (msg) then
return msg;
Line 535 ⟶ 553:
end
-- TODO: this not required any longer because done in get_ietf_parts()?
if is_set (subtags.variant) then -- special case test for |variant=
-- if is_set (subtags.scriptvariant) then -- special case test for |variant=
-- if not in_arrayis_set (table.concat ({code, '-', subtags.script}), lang_name_table.variant[subtags.variant]['prefixes']) then
-- returnif not make_error_msgin_array (table.concat ({'{{lang}}: invalid code-variant combination: ', code, '-', subtags.script}), '-', lang_name_table.variant[subtags.variant}]['prefixes']), args.nocat);then
-- return make_error_msg (table.concat ({'{{lang}}: invalid code-variant combination: ', code, '-', subtags.script, '-', subtags.variant}), args.nocat);
end
-- else end
-- else
if not in_array (code, lang_name_table.variant[subtags.variant]['prefixes']) then
-- if not in_array (code, lang_name_table.variant[subtags.variant]['prefixes']) then
return make_error_msg (table.concat ({'{{lang}}: invalid code-variant combination: ', code, '-', subtags.variant}), args.nocat);
-- return make_error_msg (table.concat ({'{{lang}}: invalid code-variant combination: ', code, '-', subtags.variant}), args.nocat);
end
-- end
-- end
-- end
 
args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant); -- format to recommended subtag styles
Line 640 ⟶ 659:
local translit;
local translit_title;
local msg;
 
code, subtags.script, subtags.region, subtags.variant, msg = get_ietf_parts (args.code, args.script, args.region, args.variant);
 
if not (code and subtags.script and subtags.region and subtags.variant) then
-- return make_error_msg (table.concat ({'{{lang-xx}}: unknown language code: ', args.code or 'missing'}), args.nocat) .. ' ' .. msg;
return make_error_msg (table.concat ({'{{lang-xx}}: ', msg}), args.nocat);
end
Line 657 ⟶ 678:
end
local msg = validate_text ('lang-xx', args); -- ensure that |text= is set, does not contain italic markup and is protected from improper bolding
if is_set (msg) then
return msg;
end
-- TODO: this not required any longer?
-- for name, value in pairs (subtags) do -- spin through the subtags table and consolidate code and parameter-provided language subtags
-- subtags[name], msg = subtag_check (name, args.code, value, args[name], args.nocat);
-- if not subtags[name] then
-- return msg; -- some sort of error detected, return the error message
-- end
-- end
 
if is_set (subtags.script) then -- if script set override rtl setting
Line 676 ⟶ 697:
end
end
-- TODO: this not required any longer because done in get_ietf_parts()?
-- if is_set (subtags.variant) then -- special case test for |variant=
-- if is_set (subtags.script) then
-- if not in_array (table.concat ({code, '-', subtags.script}), lang_name_table.variant[subtags.variant]['prefixes']) then
-- return make_error_msg (table.concat ({'{{lang-xx}}: invalid code-variant combination: ', code, '-', subtags.script, '-', subtags.variant}), args.nocat);
-- end
-- else
-- if not in_array (code, lang_name_table.variant[subtags.variant]['prefixes']) then
-- return make_error_msg (table.concat ({'{{lang-xx}}: invalid code-variant combination: ', code, '-', subtags.variant}), args.nocat);
-- end
-- end
-- end
 
args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant); -- format to recommended subtag styles
 
if lang_data.override[code] then
Line 695 ⟶ 716:
elseif not is_set (subtags.variant) then
if lang_name_table.lang[code] then
language_name = lang_name_table.lang[code][1]; -- table entries sometimes have multiple names, always take the first one
end
else -- TODO: is this the right thing to do: take language display name from variants table?
else
if lang_name_table.variant[subtags.variant] then -- TODO: there is some discussion at Template talk:Lang about having a label parameter for use when variant name is not desired among other things
language_name = lang_name_table.variant[subtags.variant]['descriptions'][1]; -- table entries sometimes have multiple names, always take the first one
end
end
Anonymous user