require('strict');localgetArgs=require('Module:Arguments').getArgs--[[--------------------------< E R R _ M S G _ M A K E >------------------------------------------------------]]localfunctionerr_msg_make(msg)return'<span class="error" style="font-size:100%">error: '..msg..' not recognized</span>'end--[[--------------------------< _ I S _ C O D E >--------------------------------------------------------------local or require()d entry pointreturn true if <code> is a mediawiki recognized code; false else<code> - language code to validate; expected to be lowercase without leading/trailing whitespace<target_lang_code> - language code for target language; expected to be valid; expected to be lowercase without leading/trailing whitespacespecifying <target_lang_code> may be a pointless exercise because of cldr fallback. For example,mw.language.fetchLanguageName (crh, sq) -> Crimean Turkishbecause the Albanian language definitions do not have an Albanian-language version of the language name]]localfunction_is_code(code,target_lang_code)code=mw.language.fetchLanguageName(code,target_lang_code);return''~=code;end--[[--------------------------< _ _ V A L I D A T E _ T A R G E T _ L A N G _ C O D E >------------------------validates target_lang_code as a know language code; returns two valueswhen target_lang_code is valid, first return value holds target_lang_code; second return value is nilwhen target_lang_code is invalid, first retrun value is nil; second return value has error messageif target_lang_code argument is nil, (missing or empty in the invoke) use local wiki's language code]]localfunction__validate_target_lang_code(target_lang_code)localmsg;iftarget_lang_codethen-- not missing or emptyifnot_is_code(target_lang_code)then-- validate target_lang_codemsg=err_msg_make('target language code: '..target_lang_code);target_lang_code=nil;-- unset as invalidendendifnottarget_lang_codethen-- if nil because missing or empty or because invlaid and we set it niltarget_lang_code=mw.getContentLanguage():getCode();-- use local wiki's language codeendreturntarget_lang_code,msg;-- target_lang_code is valid or nil; msg is nil or has an error messageend--[[--------------------------< I S _ C O D E >----------------------------------------------------------------module entry pointargs[1]: language code -> <code>args[2]: optional target language code; same as <target lang code> in {{#language:<code>|<target lang code>}}; defaults to the local wiki languagereturn true if <code> is a mediawiki recognized code; nil else]]localfunctionis_code(frame)localargs=getArgs(frame,{valueFunc=function(key,value)return(valueand''~=value)andvalue:lower():gsub('^%s*(.-)%s*$','%1')ornil;end});localcode=args[1];localtarget_lang_code=__validate_target_lang_code(args[2]);returncodeand_is_code(code,target_lang_code)andtrueornil;end--[[--------------------------< N A M E _ F R O M _ C O D E >--------------------------------------------------module entry pointargs[1]: language codeargs[2]: optional target language code; same as <target lang code> in {{#language:<code>|<target lang code>}}; defaults to the local wiki languagereturn language-name if language-code is a mediawiki recognized code; error message string elsereturned language name not guarenteed to be in target_lang_code (if specified), because mw language lists are incomplete]]localfunctionname_from_code(frame)localargs=getArgs(frame,{valueFunc=function(key,value)return(valueand''~=value)andvalue:lower():gsub('%s*(.-)%s*','%1')ornil;end});localcode=args[1];ifnotcodethenreturnerr_msg_make('code: (empty)');endlocaltarget_lang_code,msg=__validate_target_lang_code(args[2]);ifmsgthenreturnmsg;endlocalname=mw.language.fetchLanguageName(code,target_lang_code);-- returns empty string if code not foundreturn''~=nameandnameorerr_msg_make('language code: '..code);-- return language name or error messageend--[[--------------------------< C O D E _ F R O M _ N A M E >--------------------------------------------------local entry pointargs[1]: language nameargs[2]: optional target language code; instruct this function to fetch language name list in 'this' languagereturn language-code if language-name is a mediawiki recognized name and target language code is valid; error message string elsesecond return value is a boolean used by is_name(); true when name is found; false else]]localfunction_code_from_name(args)localname=args[1];ifnotnamethenreturnerr_msg_make('name: (empty)');endlocaltarget_lang_code,msg=__validate_target_lang_code(args[2]);ifmsgthenreturnmsg;endlocalcode_name_list=mw.language.fetchLanguageNames(target_lang_code,'all');-- get language code / name list in target_lang_code language indexed by language codelocalname_code_list={};-- to hold language name / code list indexed by namefork,vinpairs(code_name_list)do-- spin through the code / name list andname_code_list[v:lower()]=k;-- make a name / code listendifname_code_list[name]thenreturnname_code_list[name],true;-- returns code when name is found and true for is_name()elsereturnerr_msg_make('language name: '..name),false;-- return error message when name not found and false for is_name()endend--[[--------------------------< C O D E _ F R O M _ N A M E >--------------------------------------------------module entry pointargs[1]: language nameargs[2]: optional target language code; instruct this function to fetch language name list in 'this' languagereturn language-code if language-name is a mediawiki recognized name and target language code is valid; error message string else]]localfunctioncode_from_name(frame)localargs=getArgs(frame,{valueFunc=function(key,value)return(valueand''~=value)andvalue:lower():gsub('^%s*(.-)%s*$','%1')ornil;end});localresult,_=_code_from_name(args);-- suppress true/false return used by is_name()returnresult;end--[[--------------------------< I S _ N A M E >----------------------------------------------------------------return true if <name> is a mediawiki recognized language name; false elseargs[1]: language nameargs[2]: optional target language code; instructs _code_from_name to fetch language name list in 'this' languagedefaults to local wiki's language; when this parameter not valid, language name is assumed to be not valid]]localfunctionis_name(frame)localargs=getArgs(frame,{valueFunc=function(key,value)return(valueand''~=value)andvalue:lower():gsub('%s*(.-)%s*','%1')ornil;end});local_,result=_code_from_name(args);-- suppress code return used by code_from_name()returnresultandtrueornil;end--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------]]return{code_from_name=code_from_name,is_code=is_code,is_name=is_name,name_from_code=name_from_code,_is_code=_is_code,-- entry point from another module}