msgcat.n

来自「tcl是工具命令语言」· N 代码 · 共 300 行

N
300
字号
'\"'\" Copyright (c) 1998 Mark Harrison.'\"'\" See the file "license.terms" for information on usage and redistribution'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.'\" '\" SCCS: @(#) msgcat.n'\" .so man.macros.TH "msgcat" n 1.3 msgcat "Tcl Bundled Packages".BS'\" Note:  do not modify the .SH NAME line immediately below!.SH NAMEmsgcat \- Tcl message catalog.SH SYNOPSIS\fBpackage require Tcl 8.2\fR.sp\fBpackage require msgcat 1.3\fR.sp\fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR?.sp\fB::msgcat::mcmax ?\fIsrc-string src-string ...\fR?.sp\fB::msgcat::mclocale \fR?\fInewLocale\fR?.sp\fB::msgcat::mcpreferences\fR.sp\fB::msgcat::mcload \fIdirname\fR.sp\fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR?.sp\fB::msgcat::mcmset \fIlocale src-trans-list\fR.sp\fB::msgcat::mcunknown \fIlocale src-string\fR.BE.SH DESCRIPTION.PPThe \fBmsgcat\fR package provides a set of functionsthat can be used to manage multi-lingual user interfaces.Text strings are defined in a ``message catalog'' whichis independent from the application, andwhich can be edited or localized without modifyingthe application source code.  New languagesor locales are provided by adding a new file tothe message catalog..PPUse of the message catalog is optional by any applicationor package, but is encouraged if the application or packagewishes to be enabled for multi-lingual applications..SH COMMANDS.TP\fB::msgcat::mc \fIsrc-string\fR ?\fIarg arg ...\fR?Returns a translation of \fIsrc-string\fR according to theuser's current locale.  If additional arguments past \fIsrc-string\fRare given, the \fBformat\fR command is used to substitute theadditional arguments in the translation of \fIsrc-string\fR.\fB::msgcat::mc\fR will search the messages definedin the current namespace for a translation of \fIsrc-string\fR; ifnone is found, it will search in the parent of the current namespace,and so on until it reaches the global namespace.  If no translationstring exists, \fB::msgcat::mcunknown\fR is called and the stringreturned from \fB::msgcat::mcunknown\fR is returned..PP\fB::msgcat::mc\fR is the main function used to localize anapplication.  Instead of using an English string directly, anapplication can pass the English string through \fB::msgcat::mc\fR anduse the result.  If an application is written for a single language inthis fashion, then it is easy to add support for additional languageslater simply by defining new message catalog entries..TP\fB::msgcat::mcmax ?\fIsrc-string src-string ...\fR?Given several source strings, \fB::msgcat::mcmax\fR returns the lengthof the longest translated string.  This is useful when designinglocalized GUIs, which may require that all buttons, for example, be afixed width (which will be the width of the widest button)..TP\fB::msgcat::mclocale \fR?\fInewLocale\fR?  This function sets the locale to \fInewLocale\fR.  If \fInewLocale\fRis omitted, the current locale is returned, otherwise the current localeis set to \fInewLocale\fR.  msgcat stores and compares the locale in acase-insensitive manner, and returns locales in lowercase.The initial locale is determined by the locale specified inthe user's environment.  See \fBLOCALE SPECIFICATION\fRbelow for a description of the locale string format..TP\fB::msgcat::mcpreferences\fRReturns an ordered list of the locales preferred bythe user, based on the user's language specification.The list is ordered from most specific to leastpreference.  The list is derived from the currentlocale set in msgcat by \fBmsgcat::mclocale\fR, andcannot be set independently.  For example, if thecurrent locale is en_US_funky, then \fBmsgcat::mcpreferences\fRreturns {en_US_funky en_US en}..TP\fB::msgcat::mcload \fIdirname\fRSearches the specified directory for files that matchthe language specifications returned by \fB::msgcat::mcpreferences\fR(note that these are all lowercase), extended by the fileextension ``.msg''.  Each matching file is read in order, assuming a UTF-8 encoding.  The file contents arethen evaluated as a Tcl script.  This means that non-Latin charactersmay be present in the message file either directly in their UTF-8encoded form, or by use of the backslash-u quoting recognized by Tclevaluation.  The number of message files which matched the specificationand were loaded is returned..TP\fB::msgcat::mcset \fIlocale src-string \fR?\fItranslate-string\fR?Sets the translation for \fIsrc-string\fR to \fItranslate-string\fRin the specified \fIlocale\fR and the current namespace.  If\fItranslate-string\fR is not specified, \fIsrc-string\fR is usedfor both.  The function returns \fItranslate-string\fR..TP\fB::msgcat::mcmset \fIlocale src-trans-list\fRSets the translation for multiple source strings in\fIsrc-trans-list\fR in the specified \fIlocale\fR and the currentnamespace.\fIsrc-trans-list\fR must have an even number of elements and is inthe form {\fIsrc-string translate-string\fR ?\fIsrc-stringtranslate-string ...\fR?} \fBmsgcat::mcmset\fR can be significantlyfaster than multiple invocations of \fBmsgcat::mcset\fR. The functionreturns the number of translations set..TP\fB::msgcat::mcunknown \fIlocale src-string\fRThis routine is called by \fB::msgcat::mc\fR in the case whena translation for \fIsrc-string\fR is not defined in thecurrent locale.  The default action is to return\fIsrc-string\fR.  This procedure can be redefined by theapplication, for example to log error messages for each unknownstring.  The \fB::msgcat::mcunknown\fR procedure is invoked at thesame stack context as the call to \fB::msgcat::mc\fR.  The return valueof \fB::msgcat::mcunknown\fR is used as the return value for the callto \fB::msgcat::mc\fR.  .SH "LOCALE SPECIFICATION".PPThe locale is specified to \fBmsgcat\fR by a locale stringpassed to \fB::msgcat::mclocale\fR.The locale string consists ofa language code, an optional country code, and an optionalsystem-specific code, each separated by ``_''.  The country and languagecodes are specified in standards ISO-639 and ISO-3166.For example, the locale ``en'' specifies English and ``en_US'' specifiesU.S. English..PPWhen the msgcat package is first loaded, the locale is initializedaccording to the user's environment.  The variables \fBenv(LC_ALL)\fR,\fBenv(LC_MESSAGES)\fR, and \fBenv(LANG)\fR are examined in order.The first of them to have a non-empty value is used to determine theinitial locale.  The value is parsed according to the XPG4 pattern.CSlanguage[_country][.codeset][@modifier].CEto extract its parts.  The initial locale is then set by calling\fBmsgcat::mclocale\fR with the argument .CSlanguage[_country][_modifier].CEOn Windows, if none of those environment variables is set, msgcat willattempt to extract locale information from theregistry.  If all these attempts to discover an initial localefrom the user's environment fail, msgcat defaults to an initiallocale of ``C''..PPWhen a locale is specified by the user, a ``best match'' search isperformed during string translation.  For example, if a user specifiesen_GB_Funky, the locales ``en_GB_Funky'', ``en_GB'', and ``en'' aresearched in order until a matching translation string is found.  If notranslation string is available, then \fB::msgcat::unknown\fR iscalled..SH "NAMESPACES AND MESSAGE CATALOGS".PPStrings stored in the message catalog are stored relativeto the namespace from which they were added.  This allowsmultiple packages to use the same strings without fearof collisions with other packages.  It also allows thesource string to be shorter and less prone to typographicalerror..PPFor example, executing the code.CSmcset en hello "hello from ::"namespace eval foo {mcset en hello "hello from ::foo"}puts [mc hello]namespace eval foo {puts [mc hello]}.CEwill print.CShello from ::hello from ::foo.CE.PPWhen searching for a translation of a message, themessage catalog will search first the current namespace,then the parent of the current namespace, and so on untilthe global namespace is reached.  This allows child namespacesto "inherit" messages from their parent namespace..PPFor example, executing (in the ``en'' locale) the code .CSmcset en m1 ":: message1"mcset en m2 ":: message2"mcset en m3 ":: message3"namespace eval ::foo {    mcset en m2 "::foo message2"    mcset en m3 "::foo message3"}namespace eval ::foo::bar {    mcset en m3 "::foo::bar message3"}puts "[mc m1]; [mc m2]; [mc m3]"namespace eval ::foo {puts "[mc m1]; [mc m2]; [mc m3]"}namespace eval ::foo::bar {puts "[mc m1]; [mc m2]; [mc m3]"}.CEwill print.CS:: message1; :: message2; :: message3:: message1; ::foo message2; ::foo message3:: message1; ::foo message2; ::foo::bar message3.CE.SH "LOCATION AND FORMAT OF MESSAGE FILES".PPMessage files can be located in any directory, subjectto the following conditions:.IP [1]All message files for a package are in the same directory..IP [2]The message file name is a msgcat locale specifier (all lowercase)followed by ``.msg''.  For example:.CSes.msg    -- spanishen_gb.msg -- United Kingdom English.CE.IP [3]The file contains a series of calls to \fBmcset\fR and\fBmcmset\fR, setting the necessary translation stringsfor the language, likely enclosed in a \fBnamespace eval\fRso that all source strings are tied to the namespace ofthe package. For example, a short \fBes.msg\fR might contain:.CSnamespace eval ::mypackage {    ::msgcat::mcset es "Free Beer!" "Cerveza Gracias!"}.CE.SH "RECOMMENDED MESSAGE SETUP FOR PACKAGES".PPIf a package is installed into a subdirectory of the\fBtcl_pkgPath\fR and loaded via \fBpackage require\fR, thefollowing procedure is recommended..IP [1]During package installation, create a subdirectory\fBmsgs\fR under your package directory..IP [2]Copy your *.msg files into that directory..IP [3] Add the following command to your packageinitialization script:.CS# load language files, stored in msgs subdirectory::msgcat::mcload [file join [file dirname [info script]] msgs].CE.SH "POSITIONAL CODES FOR FORMAT AND SCAN COMMANDS".PPIt is possible that a message string used as an argumentto \fBformat\fR might have positionally dependent parameters thatmight need to be repositioned.  For example, it might besyntactically desirable to rearrange the sentence structurewhile translating..CSformat "We produced %d units in location %s" $num $cityformat "In location %s we produced %d units" $city $num.CE.PPThis can be handled by using the positionalparameters:.CSformat "We produced %1\\$d units in location %2\\$s" $num $cityformat "In location %2\\$s we produced %1\\$d units" $num $city.CE.PPSimilarly, positional parameters can be used with \fBscan\fR toextract values from internationalized strings..SH CREDITS.PPThe message catalog code was developed by Mark Harrison..SH "SEE ALSO"format(n), scan(n), namespace(n), package(n).SH KEYWORDSinternationalization, i18n, localization, l10n, message, text, translation

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?