Module:Lang/utilities
Appearance
require('strict');localgetArgs=require('Module:Arguments').getArgs;localunicode=require("Module:Unicode data");-- for is_latin()locallang=require('Module:Lang');localnamespace=mw.title.getCurrentTitle().namespace;-- used for categorizationlocalerr_texts={-- because wrapper templates have Latn/non-Latn text ordered according to the wrapper template's name['lang-sh-cyrl-latn']=true,-- this template has |1=cyrl script |2=latn script}--[[--------------------------< E R R O R _ M S G _ M A K E >--------------------------------------------------assembles an error message from template name, message text, help link, and error category.<span style="font-family: monospace, monospace;">{{', template, '}}</span>]]localfunctionerror_msg_make(msg,template,nocat)localout={};localcategory='Lang and lang-xx';table.insert(out,table.concat({'<span style=\"color:#d33\">Error: <span style="font-family: monospace, monospace;">{{',template,'}}</span>: '}));table.insert(out,msg);table.insert(out,table.concat({' ([[Template:',template,'#Error messages|help]])'}));table.insert(out,'</span>');if(0==namespaceor10==namespace)andnotnocatthen-- 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]]'}));endreturntable.concat(out);end--[[--------------------------< S E P A R A T O R _ G E T >----------------------------------------------------allowed separators are comma, semicolon, virgule, or a quoted string of text]]localfunctionseparator_get(sep_param_val)ifnotsep_param_valthenreturn', 'end;-- not specified, use defaultif','==sep_param_valthenreturn', 'end;if';'==sep_param_valthenreturn'; 'end;if'/'==sep_param_valthenreturn'/'end;localstr;ifsep_param_val:match('^%b\"\"$')then-- if quoted string and uses double quotesstr=sep_param_val:match('%b\"\"'):gsub('^"',''):gsub('"$','');returnstr;-- return the contents of the quoteelseifsep_param_val:match('^%b\'\'$')then-- if quoted string and uses single quotesstr=sep_param_val:match('%b\'\''):gsub('^\'',''):gsub('\'$','');returnstr;-- return the contents of the quoteendreturn', ';-- default in case can't extract quoted stringend--[[--------------------------< P A R A M _ S E L E C T >------------------------------------------------------Selects the appropriate enumerated parameters for <text1> or <text2> according to <swap><args_t> - arguments table from frame<lang_args_t> - arguments table to be supplied to _lang() (whichever text renders second)<lang_xx_args_t> - arguments table to be supplied to _lang_xx_inherit() or _lang_xx_italic() (whichever text renders first)]]localfunctionparam_select(args_t,lang_args_t,lang_xx_args_t,swap)localenum_xx=swapand'2'or'1';localenum=swapand'1'or'2';lang_xx_args_t.script=args_t['script'..enum_xx];-- these for <textn> that renders first (uses {{lang-xx}})lang_xx_args_t.region=args_t['region'..enum_xx];lang_xx_args_t.variant=args_t['variant'..enum_xx];lang_xx_args_t.italic=args_t['italic'..enum_xx];lang_xx_args_t.size=args_t['size'..enum_xx];lang_args_t.italic=args_t['italic'..enum];-- these for <textn> that renders second (uses {{lang}})lang_args_t.size=args_t['size'..enum];end--[[--------------------------< I E T F _ T A G _ M A K E >----------------------------------------------------make ietf tag for _lang() (second rendered <textn>)]]localfunctionietf_tag_make(args_t,tag,swap)localietf_tag_t={tag};-- initialize with <tag>localenum=swapand'1'or'2';-- when <swap> is true latin <text1> is rendered second by _lang()for_,paraminipairs({'script'..enum,'region'..enum,'variant'..enum})doifargs_t[param]thentable.insert(ietf_tag_t,args_t[param])end-- build <ietf_tag> from <enum>erated subtagsendreturntable.concat(ietf_tag_t,'-');-- concatenate subtags with hyphen separator and doneend--[[--------------------------< _ L A N G _ X 2 >--------------------------------------------------------------mimics {{lang|<tag>|<text1>|<text2>}} except that <text2> is not a romanization of <text1> (which is always theLatin-script form). Intended for languages where two scripts are 'official' or 'native and equal in status' (shis and sr may be a candidate for this; are there others?){{lang_x2|<tag>|<text1>|<text2>|swap=yes|script2=<script>|separator=[,|;|/|<quoted string>]}} <tag> - (required) language tag for both of <text1> and <text2> <text1> - (required) Latin-script text (always) <text2> - (required) non-Latin-script text (always) |swap= - when set to 'yes', swaps the rendered order so that <text2> renders first followed by <text1>; default is Latin-script <text1> first |separator = when single character ,;/ uses ', ' (default), '; ', '/'; when quoted string (first and last characters must be matching single or double quotes) uses that stringparameters supported by both of {{lang}} and {{lang-??}} templates are passed along: |cat= |nocat=parameters supported only by {{lang-??}} that are not enumerated: |label= |link=enumerated parameters: |script1= - ISO 15924 script tag for <text1> -- when <text1> renders first, these are passed to _lang_xx_...() individually |region1= - ISO 3166 region tag for <text1> -- when <text1> renders second, these are combined with <tag> to make an IETF tag for _lang() |variant1= - IETF tag for <text1> |script2= - ISO 15924 script tag for <text2> -- when <text2> renders first, these are passed to _lang_xx_...() individually |region2= - ISO 3166 region tag for <text2> -- when <text2> renders second, these are combined with <tag> to make an IETF tag for _lang() |variant2= - IETF tag for <text2> |italic1= |size1= |italic2= |size2=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]]localfunction_lang_x2(args_t)localtag=args_t.tagorargs_t[1];localtext1=args_t.text1orargs_t[2];localtext2=args_t.text2orargs_t[3];localtranslation=args_t.translationorargs_t[4];localtemplate_name=args_t.template_nameor'lang-x2';localnocat=('yes'==args_t.nocat)or('no'==args_t.cat);-- booleanifnot(tagandtext1andtext2)thenreturnerror_msg_make('missing a required argument',template_name,nocat);endiftag:find('-',1,true)thenreturnerror_msg_make('invalid language tag: <span style="font-family: monospace, monospace;">'..tag..'</span>; IETF format not supported',template_name,nocat);endlocalis_latn,pos=unicode.is_Latin(text1);ifnotis_latnthenreturnerror_msg_make('<span style="font-family: monospace, monospace;"><'..((err_texts[template_name:lower()]and'text2')or'text1')..'></span> is not Latin script (pos '..pos..')',template_name,nocat);endis_latn,pos=unicode.is_Latin(text2);ifis_latnthenreturnerror_msg_make('<span style="font-family: monospace, monospace;"><'..((err_texts[template_name:lower()]and'text1')or'text2')..'></span> is Latin script (pos '..pos..')',template_name,nocat);endlocalswap='yes'==args_t.swap;-- booleanlocalout={};localietf_tags=ietf_tag_make(args_t,tag,swap);-- for calls to {{lang}}-- create base-form arguments tables locallang_xx_args_t={['code']=tag,['label']=args_t.label,['link']=args_t.link,['cat']=args_t.cat,['nocat']=args_t.nocat,['cat']=args_t.cat};-- for whichever <textn> renders firstlocallang_args_t={['code']=ietf_tags,['cat']=args_t.cat,['nocat']=args_t.nocat,['cat']=args_t.cat};-- for whichever <textn> renders secondparam_select(args_t,lang_args_t,lang_xx_args_t,swap);-- load <lang_args_t>, <lang_xx_args_t> tables with appropriate enumerated parameters from <args_t> table according to <swap>ifswapthen-- non-Latin script <text2> renders firstlang_xx_args_t.text=text2;lang_args_t.text=text1;table.insert(out,lang._lang_xx_inherit(lang_xx_args_t));-- render non-Latin script <text2> uprightelse-- Latin script <text1> renders firstlang_xx_args_t.text=text1;lang_args_t.text=text2;table.insert(out,lang._lang_xx_italic(lang_xx_args_t));-- render Latin script <text1> in italicsendtable.insert(out,separator_get(args_t.separator));-- insert the separatortable.insert(out,lang._lang(lang_args_t));-- and render the other of <text1> or <text2>iftranslationthen-- if positional parameter 4 (translation of <text1> and <text2>)table.insert(out,lang._translation_make({['translation']=translation,['label']=args_t.label,['link']=args_t.link}));-- add translation to renderingendreturntable.concat(out)end--[[--------------------------< L A N G _ X 2 >----------------------------------------------------------------implements {{lang-x2}}; template entry point]]localfunctionlang_x2(frame)localargs_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,}