Declaring language in HTML - World Wide Web Consortium ...

文章推薦指數: 80 %
投票人數:10人

In some parts of your code you may have a problem. If you have multilingual text in the title element, you cannot mark up parts of the text for different ... Question HowshouldIsetthelanguageofthecontentinmyHTMLpage? ThispagedescribeshowtomarkupanHTMLpagesothatitgivesinformationaboutthelanguageofthepage.Itbeginswithanoverallsummary,thenprovidesadditionaldetailsinsubsequentsections. Quickanswer Alwaysusealanguageattributeonthehtmltagtodeclarethedefaultlanguageofthetextinthepage.Thisisinheritedbyallotherelements.Forexample: Notethatyoushouldusethehtmlelementratherthanthebodyelement,sincethebodyelementdoesn'tcoverthetextinsidethedocument'sheadelement. Whenthepagecontainscontentinanotherlanguage,addalanguageattributetoanelementsurroundingthatcontent.Thisallowsyoutostyleorprocessitdifferently.Forexample:

Thetitleis"LeBonUsage". UsethelangattributeforpagesservedasHTML.(ForpagesservedasXML,includingXHTML1.xandHTML5polyglotdocuments,seeChoosingtherightattribute.) UselanguagetagsfromtheIANALanguageSubtagRegistry.YoucanfindsubtagsusingtheunofficialLanguageSubtagLookuptool.(more) Insomepartsofyourcodeyoumayhaveaproblem.Ifyouhavemultilingualtextinthetitleelement,youcannotmarkuppartsofthetextfordifferentlanguagesbecausethetitleattributeonlyallowscharacters–nomarkup.Thesamegoesformultiplelanguagesinattributevalues.Thereisnogoodsolutionforthisatthemoment. Usenestedelementstotakecareofcontentandattributevaluesonthesameelementthatareindifferentlanguages.(more) Youshouldneveruseametaelementwiththehttp-equivattributesettoContent-Languagetoindicatethelanguageofapage,butincertaincircumstancesyoumaywanttoservelanguageinformationwiththeHTTPheadertoindicatetheintendedaudienceofyourpage.WhetherornotyouusetheHTTPheader,youshouldalwaysdeclarethelanguageofthetextinapageusingalanguageattributeonthehtmltag.Formoreinformationseethecompanionarticle,HTTPheaders,metaelementsandlanguageinformation. Details ThissectionprovidesmoredetailedinformationonavarietyoftopicsrelatedtodeclaringlanguageinHTML. Don'tforgettoalsousethedirattributewhendealingwithbidirectionaltext,suchasArabic,Hebrew,etc. Whatifelementcontentandattributevaluesareindifferentlanguages? Occasionallythelanguageofthetextinan attributeandtheelementcontentareindifferentlanguages.Forexample,atthetoprightcornerofthisarticletherearelinkstotranslatedversionsofthispage.The linktextshowsthelanguageofthetargetpageusingthelanguageofthetargetpage,butanassociatedtitleattributecontainsahintinthe languageofthecurrentpage: Ifyourcodelooksasfollows,thelanguage attributeswouldactuallyindicatethatnotonlythecontentbutalsothetitleattributetextisinSpanish. Thisisobviouslyincorrect. Badcode.Don'tcopy! Español Instead,movetheattributecontainingtextinadifferentlanguagetoanotherelement,asshowninthisexample,wheretheaelementinheritsthedefaultensettingofthehtmlelement. Español Whatifthere'snoelementtohangyourattributeon? Ifyouwanttospecifythelanguageofsomecontentbutthereisnomarkuparoundit,useanelementsuchasspan,bdiordivaroundthecontent. Hereisanexample:

You'dsaythatinChineseas中国科学院文献情报中心.

Choosinglanguagevalues Tobesurethatalluseragentsrecognizewhichlanguageyoumean,youneedtofollowastandardapproachwhenprovidinglanguageattributevalues. Youalsoneedtoconsiderhowtoreferinastandardwaytodialectaldifferencesbetweenlanguages,suchasthedifferencebetweenUSEnglishand BritishEnglish,whichdivergesignificantlyintermsofspellingandpronunciation. TherulesforcreatinglanguageattributevaluesaredescribedbyanIETFspecificationcalledBCP47.Inadditiontospecifyinghowtousesimplelanguagetags,suchasenforEnglishorfrforFrench,BCP47describes howtocomposelanguagetagsthatallowyouspecifyregionaldialects,scriptsandothervariantsrelatedtothatlanguage. BCP47incorporates,butgoesbeyond,theISOsetsoflanguageandcountrycodes.TofindrelevantcodesyoushouldconsulttheIANALanguageSubtagRegistry. AnunofficialLanguageSubtagLookuptoolprovidesauser-friendlyfront-endtooltotheIANAregistry. ForagentlebutfairlythoroughintroductiontothesyntaxofBCP47tags,readLanguage tagsinHTMLandXML.Forhelpinchoosingtherightlanguagetagoutofthemanypossibletagsandcombinations,seeChoosingalanguagetag. Choosingtherightattribute IfyourdocumentisHTML(ie.servedastext/html),usethelangattributetosetthelanguageofthe documentorarangeoftext.Forexample,thefollowingsetsthedefaultlanguagetoFrench: WhenservingXHTML1.xorpolyglotpagesastext/html,useboththelangattributeandthexml:langattributetogethereverytimeyouwanttosetthelanguage.Thexml:langattributeisthestandardwaytoidentifylanguageinformationinXML.Ensurethatthevaluesforbothattributesareidentical. Thexml:langattributeisnotactuallyusefulforhandlingthefileasHTML,buttakesoverfromthelangattributeanytimeyouprocessorservethedocumentasXML.ThelangattributeisallowedbythesyntaxofXHTML,andmayalsoberecognizedbybrowsers.WhenusingotherXMLparsers,however(suchasthelang()functioninXSLT)youcan'trelyonthelangattributebeingrecognized. IfyouareservingyourpageasXML(ie.usingaMIMEtypesuchasapplication/xhtml+xml),youdo notneedthelangattribute.Thexml:langattributealonewillsuffice. Additionalinformation Theinformationinthissectionislesslikelytobeuseful,butisprovidedforcompleteness. Specifyingmetadataabouttheaudiencelanguage Inadditiontoincludinganin-pagelanguageattributeonthehtmltag(whichyoushouldalwaysdo),youmayalsohavecomeacrosslanguagedeclarationsintheHTTPheader(whichisservedwiththepage),orasmetaelements. Importantly,thein-pagelanguagedeclarationalwaysoverridestheHTTPinformationwhenitcomestodeterminingtheactuallanguageofthetext,buttheHTTPinformationmayprovidemoregeneralinformationabouttheintendeduseoftheresource.UseofmetaelementsintheHTMLpagefordeclaringlanguageisnotrecommended. ForinformationaboutContent-LanguageinHTTPandinmetaelementsseeHTTPheaders,metaelementsandlanguageinformation. Variousthingsthatareirrelevant Justforgoodmeasure,andforthesakeofthoroughness,itisperhapsworthmentioningafewotherpointsthatarenotrelevanttothisdiscussion. Firstly,itisnotpossibletodeclarethelanguageoftextusingCSS. Secondly,theDOCTYPEthatshouldstartanyHTMLfilemaycontainwhatlookstosomepeoplelikealanguagedeclaration.TheDOCTYPEintheexamplebelowcontainsthetextEN,whichstandsfor'English'.This,however,indicatesthelanguageoftheschemaassociatedwiththisdocument–ithasnothingtodowiththelanguageofthedocumentitself. -//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Thirdly,sometimespeopleassumethatinformationaboutnaturallanguagecouldbeinferredfromthecharacterencoding.However,acharacterencodingdoesnotenableunambiguousidentificationofanaturallanguage:theremustbeaone-to-onemappingbetweenencodingandlanguageforthisinferencetowork,andthereisn'tone.Forexample,asinglecharacterencodingcouldbeusedformanylanguages,eg.Latin1(ISO-8859-1)couldencodebothFrenchandEnglish,aswellasagreatmanyotherlanguages.Inaddition,thecharacterencodingcanvaryoverasinglelanguage,forexampleArabiccoulduseencodingssuchas'Windows-1256'or'ISO-8859-6'or'UTF-8'. Alltheseencodingexamples,however,arenowadaysmoot,sinceallcontentshouldbeauthoredinUTF-8,whichcoversallbuttherarestoflanguagesinasinglecharacterencoding. Insomescripts,suchasArabicandHebrew,displayedtextisreadpredominantlyfromrighttoleft,althoughwithinthatflow,numbersandtextfromotherscriptsaredisplayedfromlefttoright.Markupsuchasthedirattributeisneededtosettheoverallright-to-leftcontext,andinsomecircumstancesmarkupisneededtocorrectlyrenderbidirectionaltext,butthiscannotbedoneusinglanguagemarkup. Thesamegoesfortextdirection.Aswithencodingsandlanguage,thereisnotalwaysaone-to-onemappingbetweenlanguageandscript,andthereforedirectionality.Forexample,Azerbaijanicanbewrittenusingbothright-to-left(Arabic)andleft-to-right(LatinorCyrillic)scripts,andthelanguagecodeazcanberelevantforeither.Inaddition,textdirectionmarkupusedwithinlinetextappliesarangeofdifferentvaluestothetext,whereaslanguageisasimpleswitchthatisnotuptothetasksrequired. Furtherreading Gettingstarted?LanguageontheWeb Tutorial,WorkingwithlanguageinHTML Relatedlinks,AuthoringHTML&CSS Language Usingattributestodeclarelanguage Choosinglanguagevalues Identifyingin-documentlanguagechanges



請為這篇文章評分?