Sətir 868: |
Sətir 868: |
| | | |
| return name; -- name from data tables or nil | | return name; -- name from data tables or nil |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< _ L A N G >-------------------------------------------------------------------- |
| + | |
| + | entry point for {{lang}} |
| + | |
| + | there should be no reason to set parameters in the {{lang}} {{#invoke:}} |
| + | <includeonly>{{#invoke:lang|lang}}</includeonly> |
| + | |
| + | parameters are received from the template's frame (parent frame) |
| + | |
| + | ]] |
| + | |
| + | local function _lang (args) |
| + | local out = {}; |
| + | local language_name; -- used to make category names |
| + | local category_name; -- same as language_name except that it retains any parenthetical disambiguators (if any) from the data set |
| + | local subtags = {}; -- IETF subtags script, region, variant, and private |
| + | local code; -- the language code |
| + | local msg; -- for error messages |
| + | local tag = 'span'; -- initial value for make_text_html() |
| + | local template = args.template or 'Lang'; |
| + | |
| + | validate_cat_args (args); -- determine if categorization should be suppressed |
| + | |
| + | if args[1] and args.code then |
| + | return make_error_msg ('conflicting: {{{1}}} and |code=', args, template); |
| + | else |
| + | args.code = args[1] or args.code; -- prefer args.code |
| + | end |
| + | |
| + | if args[2] and args.text then |
| + | return make_error_msg ('conflicting: {{{2}}} and |text=', args, template); |
| + | else |
| + | args.text = args[2] or args.text; -- prefer args.text |
| + | end |
| + | |
| + | msg = validate_text (template, args); -- ensure that |text= is set |
| + | if is_set (msg) then -- msg is an already-formatted error message |
| + | return msg; |
| + | end |
| + | |
| + | args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate html tag with text trimmed accordingly |
| + | |
| + | args.rtl = args.rtl == 'yes'; -- convert to boolean: 'yes' -> true, other values -> false |
| + | |
| + | args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag |
| + | if msg then |
| + | return make_error_msg (table.concat ({'invalid |proto=: ', args.proto}), args, template); |
| + | end |
| + | |
| + | code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (args.code); -- |script=, |region=, |variant= not supported because they should be part of args.code ({{{1}}} in {{lang}}) |
| + | |
| + | if msg then |
| + | return make_error_msg ( msg, args, template); |
| + | end |
| + | |
| + | args.italic, msg = validate_italic (args); |
| + | if msg then |
| + | return make_error_msg (msg, args, template); |
| + | end |
| + | |
| + | if nil == args.italic then -- nil when |italic= absent or not set or |italic=default; args.italic controls |
| + | if ('latn' == subtags.script) or -- script is latn |
| + | (this_wiki_lang ~= code and not is_set (subtags.script) and not has_poem_tag (args.text) and unicode.is_Latin (args.text)) then -- text not this wiki's language, no script specified and not in poem markup but is wholly latn script (auto-italics) |
| + | args.italic = 'italic'; -- DEFAULT for {{lang}} templates is upright; but if latn script set for font-style:italic |
| + | else |
| + | args.italic = 'inherit'; -- italic not set; script not latn; inherit current style |
| + | end |
| + | end |
| + | |
| + | if is_set (subtags.script) then -- if script set, override rtl setting |
| + | if in_array (subtags.script, lang_data.rtl_scripts) then |
| + | args.rtl = true; -- script is an rtl script |
| + | else |
| + | args.rtl = false; -- script is not an rtl script |
| + | end |
| + | end |
| + | |
| + | args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles |
| + | language_name = language_name_get (args.code, code, true); -- get language name; try ietf tag first, then code w/o variant then code w/ variant |
| + | |
| + | if 'invert' == args.italic and 'span' == tag then -- invert only supported for in-line content |
| + | args.text = invert_italics (args.text) |
| + | end |
| + | |
| + | args.text = proto_prefix (args.text, language_name, args.proto); -- prefix proto-language text with a splat |
| + | |
| + | table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, language_name)); |
| + | table.insert (out, make_category (code, language_name, args.nocat)); |
| + | table.insert (out, render_maint(args.nocat)); -- maintenance messages and categories |
| + | |
| + | return table.concat (out); -- put it all together and done |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< L A N G >---------------------------------------------------------------------- |
| + | |
| + | entry point for {{lang}} |
| + | |
| + | there should be no reason to set parameters in the {{lang}} {{#invoke:}} |
| + | <includeonly>{{#invoke:lang|lang}}</includeonly> |
| + | |
| + | parameters are received from the template's frame (parent frame) |
| + | |
| + | ]] |
| + | |
| + | local function lang (frame) |
| + | local args = getArgs (frame, { -- this code so that we can detect and handle wiki list markup in text |
| + | valueFunc = function (key, value) |
| + | if 2 == key or 'text' == key then -- the 'text' parameter; do not trim wite space |
| + | return value; -- return untrimmed 'text' |
| + | elseif value then -- all other values: if the value is not nil |
| + | value = mw.text.trim (value); -- trim whitespace |
| + | if '' ~= value then -- empty string when value was only whitespace |
| + | return value; |
| + | end |
| + | end |
| + | return nil; -- value was empty or contained only whitespace |
| + | end -- end of valueFunc |
| + | }); |
| + | |
| + | return _lang (args); |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< L A N G _ X X >---------------------------------------------------------------- |
| + | |
| + | For the {{lang-xx}} templates, the only parameter required to be set in the template is the language code. All |
| + | other parameters can, usually should, be written in the template call. For {{lang-xx}} templates for languages |
| + | that can have multiple writing systems, it may be appropriate to set |script= as well. |
| + | |
| + | For each {{lang-xx}} template choose the appropriate entry-point function so that this function knows the default |
| + | styling that should be applied to text. |
| + | |
| + | For normal, upright style: |
| + | <includeonly>{{#invoke:lang|lang_xx_inherit|code=xx}}</includeonly> |
| + | For italic style: |
| + | <includeonly>{{#invoke:lang|lang_xx_italic|code=xx}}</includeonly> |
| + | |
| + | All other parameters should be received from the template's frame (parent frame) |
| + | |
| + | Supported parameters are: |
| + | |code = (required) the IANA language code |
| + | |script = IANA script code; especially for use with languages that use multiple writing systems |
| + | |region = IANA region code |
| + | |variant = IANA variant code |
| + | |text = (required) the displayed text in language specified by code |
| + | |link = boolean false ('no') does not link code-spcified language name to associated language article |
| + | |rtl = boolean true ('yes') identifies the language specified by code as a right-to-left language |
| + | |nocat = boolean true ('yes') inhibits normal categorization; error categories are not affected |
| + | |cat = boolian false ('no') opposite form of |nocat= |
| + | |italic = boolean true ('yes') renders displayed text in italic font; boolean false ('no') renders displayed text in normal font; not set renders according to initial_style_state |
| + | |lit = text that is a literal translation of text |
| + | |label = 'none' to suppress all labeling (language name, 'translit.', 'lit.') |
| + | any other text replaces language-name label - automatic wikilinking disabled |
| + | |
| + | for those {{lang-xx}} templates that support transliteration (those templates where |text= is not entirely latn script): |
| + | |translit = text that is a transliteration of text |
| + | |translit-std = the standard that applies to the transliteration |
| + | |translit-script = ISO 15924 script name; falls back to code |
| + | |
| + | For {{lang-xx}}, the positional parameters are: |
| + | {{{1}}} text |
| + | {{{2}}} transliterated text |
| + | {{{3}}} literal translation text |
| + | no other positional parameters are allowed |
| + | |
| + | ]] |
| + | |
| + | local function _lang_xx (args) |
| + | local out = {}; |
| + | local language_name; -- used to make display text, article links |
| + | local category_name; -- same as language_name except that it retains any parenthetical disambiguators (if any) from the data set |
| + | local subtags = {}; -- IETF subtags script, region, and variant |
| + | local code; -- the language code |
| + | |
| + | local translit_script_name; -- name associated with IANA (ISO 15924) script code |
| + | local translit; |
| + | local translit_title; |
| + | local msg; -- for error messages |
| + | local tag = 'span'; -- initial value for make_text_html() |
| + | local template = args.template or 'Lang-xx'; |
| + | |
| + | if args[1] and args.text then |
| + | return make_error_msg ('conflicting: {{{1}}} and |text=', args, template); |
| + | else |
| + | args.text = args[1] or args.text; -- prefer args.text |
| + | end |
| + | |
| + | msg = validate_text (template, 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 |
| + | |
| + | args.text, tag = html_tag_select (args.text); -- inspects text; returns appropriate html tag with text trimmed accordingly |
| + | |
| + | if args[2] and args.translit then |
| + | return make_error_msg ('conflicting: {{{2}}} and |translit=', args, template); |
| + | else |
| + | args.translit = args[2] or args.translit -- prefer args.translit |
| + | end |
| + | |
| + | if args[3] and (args.translation or args.lit) then |
| + | return make_error_msg ('conflicting: {{{3}}} and |lit= or |translation=', args, template); |
| + | elseif args.translation and args.lit then |
| + | return make_error_msg ('conflicting: |lit= and |translation=', args, template); |
| + | else |
| + | args.translation = args[3] or args.translation or args.lit; -- prefer args.translation |
| + | end |
| + | |
| + | if args.links and args.link then |
| + | return make_error_msg ('conflicting: |links= and |link=', args, template); |
| + | else |
| + | 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 |
| + | |
| + | code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (args.code, args.script, args.region, args.variant); -- private omitted because private |
| + | |
| + | if msg then -- if an error detected then there is an error message |
| + | return make_error_msg (msg, args, template); |
| + | end |
| + | |
| + | args.italic, msg = validate_italic (args); |
| + | if msg then |
| + | return make_error_msg (msg, args, template); |
| + | end |
| + | |
| + | if nil == args.italic then -- args.italic controls |
| + | if is_set (subtags.script) then |
| + | if 'latn' == subtags.script then |
| + | args.italic = 'italic'; -- |script=Latn; set for font-style:italic |
| + | else |
| + | args.italic = initial_style_state; -- italic not set; script is not latn; set for font-style:<initial_style_state> |
| + | end |
| + | else |
| + | args.italic = initial_style_state; -- here when |italic= and |script= not set; set for font-style:<initial_style_state> |
| + | end |
| + | end |
| + | |
| + | if is_set (subtags.script) then -- if script set override rtl setting |
| + | if in_array (subtags.script, lang_data.rtl_scripts) then |
| + | args.rtl = true; -- script is an rtl script |
| + | else |
| + | args.rtl = false; -- script is not an rtl script |
| + | end |
| + | end |
| + | |
| + | args.proto, msg = validate_proto (args.proto); -- return boolean, or nil, or nil and error message flag |
| + | if msg then |
| + | return make_error_msg (table.concat ({'invalid |proto=: ', args.proto}), args, template); |
| + | end |
| + | |
| + | args.code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles |
| + | language_name = language_name_get (args.code, code, true); -- get language name; try ietf tag first, then code w/o variant then code w/ variant |
| + | |
| + | category_name = language_name; -- category names retain IANA parenthetical diambiguators (if any) |
| + | language_name = language_name:gsub ('%s+%b()', ''); -- remove IANA parenthetical disambiguators or qualifiers from names that have them |
| + | |
| + | if args.label then |
| + | if 'none' ~= args.label then |
| + | table.insert (out, table.concat ({args.label, ': '})); -- custom label |
| + | end |
| + | else |
| + | if 'no' == args.link then |
| + | table.insert (out, language_name); -- language name without wikilink |
| + | else |
| + | if language_name:find ('languages') then |
| + | table.insert (out, make_wikilink (language_name)); -- collective language name uses simple wikilink |
| + | elseif lang_data.article_name[code] then |
| + | table.insert (out, make_wikilink (lang_data.article_name[code][1], language_name)); -- language name with wikilink from override data |
| + | else |
| + | table.insert (out, make_wikilink (language_name .. ' language', language_name)); -- language name with wikilink |
| + | end |
| + | end |
| + | table.insert (out, ': '); -- separator |
| + | end |
| + | |
| + | if 'invert' == args.italic then |
| + | args.text = invert_italics (args.text) |
| + | end |
| + | |
| + | args.text = proto_prefix (args.text, language_name, args.proto); -- prefix proto-language text with a splat |
| + | |
| + | table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, nil)) |
| + | |
| + | if is_set (args.translit) and not unicode.is_Latin (args.text) then -- transliteration (not supported in {{lang}}); not supported when args.text is wholly latn text (this is an imperfect test) |
| + | table.insert (out, ', '); -- comma to separate text from translit |
| + | if 'none' ~= args.label then |
| + | table.insert (out, '<small>'); |
| + | if script_table[args['translit-script']] then -- when |translit-script= is set, try to use the script's name |
| + | translit_script_name = script_table[args['translit-script'][1]]; |
| + | else |
| + | translit_script_name = language_name; -- fall back on language name |
| + | end |
| + | translit_title = mw.title.makeTitle (0, table.concat ({'Romanization of ', translit_script_name})); -- make a title object |
| + | if translit_title.exists and ('no' ~= args.link) then |
| + | table.insert (out, make_wikilink ('Romanization of ' .. translit_script_name or language_name, 'romanized') .. ':'); -- make a wikilink if there is an article to link to |
| + | else |
| + | table.insert (out, 'romanized:'); -- else plain text |
| + | end |
| + | table.insert (out, ' </small>'); -- close the small tag |
| + | end |
| + | |
| + | translit = make_translit (args.code, language_name, args.translit, args['translit-std'], args['translit-script']) |
| + | if is_set (translit) then |
| + | table.insert (out, translit); |
| + | else |
| + | return make_error_msg (table.concat ({'invalid translit-std: \'', args['translit-std'] or '[missing]'}), args, template); |
| + | end |
| + | end |
| + | |
| + | if is_set (args.translation) then -- translation (not supported in {{lang}}) |
| + | table.insert (out, ', '); |
| + | if 'none' ~= args.label then |
| + | table.insert (out, '<small>'); |
| + | if 'no' == args.link then |
| + | table.insert (out, '<abbr title="literal translation">lit.</abbr>'); |
| + | else |
| + | table.insert (out, make_wikilink ('Literal translation', 'lit.')); |
| + | end |
| + | table.insert (out, " </small>"); |
| + | end |
| + | table.insert (out, table.concat ({''', args.translation, '''})); -- use html entities to avoid wiki markup confusion |
| + | end |
| + | |
| + | table.insert (out, make_category (code, category_name, args.nocat)); |
| + | table.insert (out, render_maint(args.nocat)); -- maintenance messages and categories |
| + | |
| + | return table.concat (out); -- put it all together and done |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< L A N G _ X X _ A R G S _ G E T >---------------------------------------------- |
| + | |
| + | common function to get args table from {{lang-??}} templates |
| + | |
| + | returns table of args |
| + | |
| + | ]] |
| + | |
| + | local function lang_xx_args_get (frame) |
| + | local args = getArgs(frame, |
| + | { |
| + | parentFirst= true, -- parameters in the template override parameters set in the {{#invoke:}} |
| + | valueFunc = function (key, value) |
| + | if 1 == key then -- the 'text' parameter; do not trim wite space |
| + | return value; -- return untrimmed 'text' |
| + | elseif value then -- all other values: if the value is not nil |
| + | value = mw.text.trim (value); -- trim whitespace |
| + | if '' ~= value then -- empty string when value was only whitespace |
| + | return value; |
| + | end |
| + | end |
| + | return nil; -- value was empty or contained only whitespace |
| + | end -- end of valueFunc |
| + | }); |
| + | |
| + | return args; |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< L A N G _ X X _ I T A L I C >-------------------------------------------------- |
| + | |
| + | Entry point for those {{lang-xx}} templates that call lang_xx_italic(). Sets the initial style state to italic. |
| + | |
| + | ]] |
| + | |
| + | local function lang_xx_italic (frame) |
| + | local args = lang_xx_args_get (frame); |
| + | |
| + | initial_style_state = 'italic'; |
| + | return _lang_xx (args); |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< _ L A N G _ X X _ I T A L I C >------------------------------------------------ |
| + | |
| + | Entry point ffrom another module. Sets the initial style state to italic. |
| + | |
| + | ]] |
| + | |
| + | local function _lang_xx_italic (args) |
| + | initial_style_state = 'italic'; |
| + | return _lang_xx (args); |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< L A N G _ X X _ I N H E R I T >------------------------------------------------ |
| + | |
| + | Entry point for those {{lang-xx}} templates that call lang_xx_inherit(). Sets the initial style state to inherit. |
| + | |
| + | ]] |
| + | |
| + | local function lang_xx_inherit (frame) |
| + | local args = lang_xx_args_get (frame); |
| + | |
| + | initial_style_state = 'inherit'; |
| + | return _lang_xx (args); |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< _ L A N G _ X X _ I N H E R I T >---------------------------------------------- |
| + | |
| + | Entry point from another module. Sets the initial style state to inherit. |
| + | |
| + | ]] |
| + | |
| + | local function _lang_xx_inherit (args) |
| + | initial_style_state = 'inherit'; |
| + | return _lang_xx (args); |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< _ I S _ I E T F _ T A G >------------------------------------------------------ |
| + | |
| + | Returns true when a language name associated with IETF language tag exists; nil else. IETF language tag must be valid. |
| + | |
| + | All code combinations supported by {{lang}} and the {{lang-xx}} templates are supported by this function. |
| + | |
| + | Module entry point from another module |
| + | |
| + | ]] |
| + | |
| + | local function _is_ietf_tag (tag) -- entry point when this module is require()d into another |
| + | local c, s, r, v, p, err; -- code, script, region, variant, private, error message |
| + | c, s, r, v, p, err = get_ietf_parts (tag); -- disassemble tag into constituent part and validate |
| + | |
| + | return ((c and not err) and true) or nil; -- return true when code portion has a value without error message; nil else |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< I S _ I E T F _ T A G >-------------------------------------------------------- |
| + | |
| + | Module entry point from an {{#invoke:}} |
| + | |
| + | ]] |
| + | |
| + | local function is_ietf_tag (frame) |
| + | return _is_ietf_tag (getArgs(frame)[1]); -- args[1] is the ietf language tag to be tested; getArgs() so we also get parent frame |
| + | end |
| + | |
| + | |
| + | --[[--------------------------< _ N A M E _ F R O M _ T A G >-------------------------------------------------- |
| + | |
| + | Returns language name associated with IETF language tag if valid; error message else. |
| + | |
| + | 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. |
| + | |
| + | Module entry point from another module |
| + | |
| + | ]] |
| + | |
| + | local function _name_from_tag (args) |
| + | local subtags = {}; -- IETF subtags script, region, variant, and private |
| + | local raw_code = args[1]; -- save a copy of the input IETF subtag |
| + | local link = 'yes' == args['link']; -- make a boolean |
| + | local label = args.label; |
| + | local code; -- the language code |
| + | local msg; -- gets an error message if IETF language tag is malformed or invalid |
| + | local language_name = ''; |
| + | |
| + | code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (raw_code); |
| + | if msg then |
| + | local template = (args['template'] and table.concat ({'{{', 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 |
| + | |
| + | raw_code = format_ietf_tag (code, subtags.script, subtags.region, subtags.variant, subtags.private); -- format to recommended subtag styles; private omitted because private |
| + | language_name = language_name_get (raw_code, code); -- get language name; try ietf tag first, then code w/o variant then code w/ variant |
| + | |
| + | 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 |
| + | language_name = make_wikilink (language_name, label); -- collective language name uses simple wikilink |
| + | elseif lang_data.article_name[code] then |
| + | language_name = make_wikilink (lang_data.article_name[code][1], label or language_name); -- language name with wikilink from override data |
| + | else |
| + | language_name = make_wikilink (language_name .. ' language', label or language_name); -- language name with wikilink |
| + | end |
| + | end |
| + | |
| + | return language_name; |
| end | | end |