Jump to content
Wikipedia The Free Encyclopedia

Module:Lang-zh

From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page template-protected This module is currently protected from editing.
See the protection policy and protection log for more details. Please discuss any changes on the talk page; you may submit an edit request to ask an administrator to make an edit if it is uncontroversial or supported by consensus. You may also request that this page be unprotected.
Warning This Lua module is used on approximately 82,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them.
This module depends on the following other modules:

Usage

This is the module for the template {{Lang-zh }}. Its main features are described on that template's documentation page, refer to that for how to use it in articles. This documentation describes some particular features of the module.

The first table in the module lists the articles in which the template will put traditional Chinese first. Add articles to this list in the same fashion (the order doesn't matter) to include them. If you cannot edit the module because its protected use the {{edit template-protected }} template on its talk page to request a change.

Use from other modules

The module can be called directly from another module as well as a template. To do this first load the module

localZh=require("Module:Lang-zh")._Zh

Then call it as follows, with the arguments in a table

localoutput=Zh{["c"]="中国",["p"]="zhōngguó"}

Tracking categories

The above documentation is transcluded from Module:Lang-zh/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

 require('strict')

 localp={}

 -- articles in which traditional Chinese preceeds simplified Chinese
 localt1st={
 ["228 Incident"]=true,
 ["Chinese calendar"]=true,
 ["Lippo Centre, Hong Kong"]=true,
 ["Republic of China"]=true,
 ["Republic of China at the 1924 Summer Olympics"]=true,
 ["Taiwan"]=true,
 ["Taiwan (island)"]=true,
 ["Taiwan Province"]=true,
 ["Wei Boyang"]=true,
 }

 -- the labels for each part 
 locallabels={
 ["c"]="Chinese",
 ["s"]="simplified Chinese",
 ["t"]="traditional Chinese",
 ["p"]="pinyin",
 ["tp"]="Tongyong Pinyin",
 ["w"]="Wade–Giles",
 ["j"]="Jyutping",
 ["cy"]="Cantonese Yale",
 ["sl"]="Sidney Lau",
 ["poj"]="Pe̍h-ōe-jī",
 ["tl"]="Tâi-lô",
 ["zhu"]="Zhuyin Fuhao",
 ["l"]="lit.",
 ["tr"]="trans.",
 }

 -- article titles for wikilinks for each part
 localwlinks={
 ["c"]="Chinese language",
 ["s"]="simplified Chinese characters",
 ["t"]="traditional Chinese characters",
 ["p"]="pinyin",
 ["tp"]="Tongyong Pinyin",
 ["w"]="Wade–Giles",
 ["j"]="Jyutping",
 ["cy"]="Yale romanization of Cantonese",
 ["sl"]="Sidney Lau romanisation",
 ["poj"]="Pe̍h-ōe-jī",
 ["tl"]="Tâi-uân Lô-má-jī Phing-im Hong-àn",
 ["zhu"]="Bopomofo",
 ["l"]="Literal translation",
 ["tr"]="Translation",
 }

 -- for those parts which are to be treated as languages their ISO code
 localISOlang={
 ["c"]="zh",
 ["t"]="zh-Hant",
 ["s"]="zh-Hans",
 ["p"]="zh-Latn",
 ["tp"]="zh-Latn-tongyong",
 ["w"]="zh-Latn-wadegile",
 ["j"]="yue-Latn-jyutping",
 ["cy"]="yue-Latn",
 ["sl"]="yue-Latn",
 ["poj"]="nan-Latn",
 ["tl"]="nan-Latn-tailo",
 ["zhu"]="zh-Bopo",
 }

 localitalic={
 ["p"]=true,
 ["tp"]=true,
 ["w"]=true,
 ["j"]=true,
 ["cy"]=true,
 ["sl"]=true,
 ["poj"]=true,
 ["tl"]=true,
 }

 localsuperscript={
 ["w"]=true,
 ["sl"]=true,
 }

 -- Categories for different kinds of Chinese text
 localcats={
 ["c"]="[[Category:Articles containing Chinese-language text]]",
 ["s"]="[[Category:Articles containing simplified Chinese-language text]]",
 ["t"]="[[Category:Articles containing traditional Chinese-language text]]",
 }

 functionp.Zh(frame)
 -- load arguments module to simplify handling of args
 localgetArgs=require('Module:Arguments').getArgs

 localargs=getArgs(frame)
 returnp._Zh(args)
 end

 functionp._Zh(args)
 ifargs["link"]thenargs["links"]=args["link"];end
 ifargs["label"]thenargs["labels"]=args["label"];end

 localuselinks=args["links"]~="no"-- whether to add links
 localuselabels=args["labels"]~="no"-- whether to have labels
 localcapfirst=args["scase"]~=nil
 localout=nil-- which term to put before the brackets
 localusebrackets=0-- whether to have bracketed terms
 localnumargs=0
 localregionalvariant=nil

 ifargs["out"]then
 out=args["out"]
 usebrackets=1
 end

 localt1=false-- whether traditional Chinese characters go first
 localj1=false-- whether Cantonese Romanisations go first
 localpoj1=false-- whether Hokkien Romanisations go first
 localtestChar
 if(args["first"])then
 fortestCharinmw.ustring.gmatch(args["first"],"%a+")do
 if(testChar=="t")then
 t1=true
 end
 if(testChar=="j")then
 j1=true
 end
 if(testChar=="poj")then
 poj1=true
 end
 end
 end
 if(t1==false)then
 localtitle=mw.title.getCurrentTitle()
 t1=t1st[title.text]==true
 end

 -- based on setting/preference specify order
 localorderlist={"c","s","t","p","tp","w","j","cy","sl","poj","tl","zhu","l","tr"}
 if(t1)then
 orderlist[2]="t"
 orderlist[3]="s"
 end
 if(j1)then
 orderlist[4]="j"
 orderlist[5]="cy"
 orderlist[6]="sl"
 orderlist[7]="p"
 orderlist[8]="tp"
 orderlist[9]="w"
 end
 if(poj1)then
 orderlist[4]="poj"
 orderlist[5]="tl"
 orderlist[6]="p"
 orderlist[7]="tp"
 orderlist[8]="w"
 orderlist[9]="j"
 orderlist[10]="cy"
 orderlist[11]="sl"
 end

 -- rename rules. Rules to change parameters and labels based on other parameters
 ifargs["hp"]then
 -- hp an alias for p ([hanyu] pinyin)
 args["p"]=args["hp"]
 end
 ifargs["tp"]then
 -- if also Tongyu pinyin use full name for Hanyu pinyin
 labels["p"]="Hanyu Pinyin"
 end

 -- specify traditional Chinese variants
 ifargs["t_hk"]then
 args["t"]=args["t_hk"]
 regionalvariant="HK"
 elseifargs["t_tw"]then
 args["t"]=args["t_tw"]
 regionalvariant="TW"
 end

 if(args["s"]andargs["s"]==args["t"])then
 -- Treat simplified + traditional as Chinese if they're the same
 args["c"]=args["s"]
 args["s"]=nil
 args["t"]=nil
 ifout=="s"orout=="t"then
 out="c"
 end
 elseif(not(args["s"]andargs["t"]))then
 -- use short label if only one of simplified and traditional
 labels["s"]=labels["c"]
 labels["t"]=labels["c"]
 end
 ifoutthen
 fori,vinipairs(orderlist)do-- shift `out` to the beginning of the order list
 ifv==outthen
 table.remove(orderlist,i)
 table.insert(orderlist,1,v)
 break
 end
 end
 end

 if(out=="c"andargs["s"])thenusebrackets=2;end

 localbody=""-- the output string
 localparams-- for creating HTML spans
 locallabel-- the label, i.e. the bit preceeding the supplied text
 localval-- the supplied text

 -- go through all possible fields in loop, adding them to the output
 fori,partinipairs(orderlist)do
 if(args[part])then
 numargs=numargs+1
 -- build label
 label=""
 if(uselabels)then
 label=labels[part]
 if(capfirst)then
 label=mw.language.getContentLanguage():ucfirst(label)
 capfirst=false
 end
 if(uselinksandpart~="l"andpart~="tr")then
 label="[["..wlinks[part].."|"..label.."]]"
 end
 if(part=="l"orpart=="tr")then
 label="<abbr title=\""..wlinks[part].."\"><small>"..label.."</small></abbr>"
 else
 label=label.."&colon;"
 end
 label=label.." "
 end
 -- build value
 val=args[part]
 if(cats[part])andmw.title.getCurrentTitle().namespace==0then
 -- if has associated category AND current page in article namespace, add category
 val=cats[part]..val
 end
 if(ISOlang[part])then
 -- add span for language if needed
 params={["lang"]=ISOlang[part]..(regionalvariantand"-"..regionalvariantor"")}
 val=mw.text.tag({name="span",attrs=params,content=val})
 elseif(part=="l")then
 localterms=""
 -- put individual, potentially comma-separated glosses in single quotes
 -- (first strip leading and trailing whitespace and quotes, including bold/italic markup)
 forterminval:gmatch("[^;,]+")do
 term=mw.text.trim(term,"%s\"")
 terms=terms.."&apos;"..term.."&apos;, "
 end
 val=string.sub(terms,1,-3)
 elseif(part=="tr")then
 -- put translations in double quotes
 -- (first strip leading and trailing spaces and quotes, including bold/italic markup)
 val=mw.text.trim(val,"%s\"'")
 val="&quot;"..val.."&quot;"
 end
 if(italic[part])then
 -- italicise
 val="<i>"..val.."</i>"
 end
 ifstring.match(val,"</?sup>")thenval=val.."[[Category:Pages using template Zh with sup tags]]"end
 if(superscript[part])then
 -- superscript
 val=val:gsub("(%d)","<sup>%1</sup>"):gsub("(%d)</sup>%*<sup>(%d)","%1*%2"):gsub("<sup><sup>([%d%*]+)</sup></sup>","<sup>%1</sup>")
 end
 -- add both to body
 ifnumargs==usebracketsthen
 -- opening bracket after the `out` term
 body=body..label..val.." ("
 else
 body=body..label..val.."; "
 end
 end
 end

 if(body>"")then-- check for empty string
 body=string.sub(body,1,-3)-- chop off final semicolon and space
 ifoutandnumargs>usebracketsthen
 -- closing bracket after the rest of the terms
 body=body.."&rpar;"
 end
 returnbody
 else--no named parameters; see if there's a first parameter, ignoring its name
 if(args[1])then
 -- if there is treat it as Chinese
 label=""
 if(uselabels)then
 label=labels["c"]
 if(uselinks)then
 label="[["..wlinks["c"].."|"..label.."]]"
 end
 label=label.."&colon; "
 end
 -- default to show links and labels as no options given
 ifmw.title.getCurrentTitle().namespace==0then
 -- if current page in article namespace
 val=cats["c"]..args[1]
 else
 val=args[1]
 end
 params={["lang"]=ISOlang["c"]}
 val=mw.text.tag({name="span",attrs=params,content=val})
 returnlabel..val
 end
 return""
 end
 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /