msgcat.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:11k
- '"
- '" 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 NAME
- msgcat - Tcl message catalog
- .SH SYNOPSIS
- fBpackage require Tcl 8.2fR
- .sp
- fBpackage require msgcat 1.3.4fR
- .sp
- fB::msgcat::mc fIsrc-stringfR ?fIarg arg ...fR?
- .sp
- fB::msgcat::mcmax ?fIsrc-string src-string ...fR?
- .sp
- fB::msgcat::mclocale fR?fInewLocalefR?
- .sp
- fB::msgcat::mcpreferencesfR
- .sp
- fB::msgcat::mcload fIdirnamefR
- .sp
- fB::msgcat::mcset fIlocale src-string fR?fItranslate-stringfR?
- .sp
- fB::msgcat::mcmset fIlocale src-trans-listfR
- .sp
- fB::msgcat::mcunknown fIlocale src-stringfR
- .BE
- .SH DESCRIPTION
- .PP
- The fBmsgcatfR package provides a set of functions
- that can be used to manage multi-lingual user interfaces.
- Text strings are defined in a ``message catalog'' which
- is independent from the application, and
- which can be edited or localized without modifying
- the application source code. New languages
- or locales are provided by adding a new file to
- the message catalog.
- .PP
- Use of the message catalog is optional by any application
- or package, but is encouraged if the application or package
- wishes to be enabled for multi-lingual applications.
- .SH COMMANDS
- .TP
- fB::msgcat::mc fIsrc-stringfR ?fIarg arg ...fR?
- Returns a translation of fIsrc-stringfR according to the
- user's current locale. If additional arguments past fIsrc-stringfR
- are given, the fBformatfR command is used to substitute the
- additional arguments in the translation of fIsrc-stringfR.
- .PP
- fB::msgcat::mcfR will search the messages defined
- in the current namespace for a translation of fIsrc-stringfR; if
- none is found, it will search in the parent of the current namespace,
- and so on until it reaches the global namespace. If no translation
- string exists, fB::msgcat::mcunknownfR is called and the string
- returned from fB::msgcat::mcunknownfR is returned.
- .PP
- fB::msgcat::mcfR is the main function used to localize an
- application. Instead of using an English string directly, an
- application can pass the English string through fB::msgcat::mcfR and
- use the result. If an application is written for a single language in
- this fashion, then it is easy to add support for additional languages
- later simply by defining new message catalog entries.
- .TP
- fB::msgcat::mcmax ?fIsrc-string src-string ...fR?
- Given several source strings, fB::msgcat::mcmaxfR returns the length
- of the longest translated string. This is useful when designing
- localized GUIs, which may require that all buttons, for example, be a
- fixed width (which will be the width of the widest button).
- .TP
- fB::msgcat::mclocale fR?fInewLocalefR?
- This function sets the locale to fInewLocalefR. If fInewLocalefR
- is omitted, the current locale is returned, otherwise the current locale
- is set to fInewLocalefR. msgcat stores and compares the locale in a
- case-insensitive manner, and returns locales in lowercase.
- The initial locale is determined by the locale specified in
- the user's environment. See fBLOCALE SPECIFICATIONfR
- below for a description of the locale string format.
- .TP
- fB::msgcat::mcpreferencesfR
- Returns an ordered list of the locales preferred by
- the user, based on the user's language specification.
- The list is ordered from most specific to least
- preference. The list is derived from the current
- locale set in msgcat by fB::msgcat::mclocalefR, and
- cannot be set independently. For example, if the
- current locale is en_US_funky, then fB::msgcat::mcpreferencesfR
- returns fB{en_US_funky en_US en}fR.
- .TP
- fB::msgcat::mcload fIdirnamefR
- Searches the specified directory for files that match
- the language specifications returned by fB::msgcat::mcpreferencesfR
- (note that these are all lowercase), extended by the file
- extension ``.msg''. Each matching file is
- read in order, assuming a UTF-8 encoding. The file contents are
- then evaluated as a Tcl script. This means that Unicode characters
- may be present in the message file either directly in their UTF-8
- encoded form, or by use of the backslash-u quoting recognized by Tcl
- evaluation. The number of message files which matched the specification
- and were loaded is returned.
- .TP
- fB::msgcat::mcset fIlocale src-string fR?fItranslate-stringfR?
- Sets the translation for fIsrc-stringfR to fItranslate-stringfR
- in the specified fIlocalefR and the current namespace. If
- fItranslate-stringfR is not specified, fIsrc-stringfR is used
- for both. The function returns fItranslate-stringfR.
- .TP
- fB::msgcat::mcmset fIlocale src-trans-listfR
- Sets the translation for multiple source strings in
- fIsrc-trans-listfR in the specified fIlocalefR and the current
- namespace.
- fIsrc-trans-listfR must have an even number of elements and is in
- the form {fIsrc-string translate-stringfR ?fIsrc-string
- translate-string ...fR?} fB::msgcat::mcmsetfR can be significantly
- faster than multiple invocations of fB::msgcat::mcsetfR. The function
- returns the number of translations set.
- .TP
- fB::msgcat::mcunknown fIlocale src-stringfR
- This routine is called by fB::msgcat::mcfR in the case when
- a translation for fIsrc-stringfR is not defined in the
- current locale. The default action is to return
- fIsrc-stringfR. This procedure can be redefined by the
- application, for example to log error messages for each unknown
- string. The fB::msgcat::mcunknownfR procedure is invoked at the
- same stack context as the call to fB::msgcat::mcfR. The return value
- of fB::msgcat::mcunknownfR is used as the return value for the call
- to fB::msgcat::mcfR.
- .SH "LOCALE SPECIFICATION"
- .PP
- The locale is specified to fBmsgcatfR by a locale string
- passed to fB::msgcat::mclocalefR.
- The locale string consists of
- a language code, an optional country code, and an optional
- system-specific code, each separated by ``_''. The country and language
- codes are specified in standards ISO-639 and ISO-3166.
- For example, the locale ``en'' specifies English and ``en_US'' specifies
- U.S. English.
- .PP
- When the msgcat package is first loaded, the locale is initialized
- according 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 the
- initial locale. The value is parsed according to the XPG4 pattern
- .CS
- language[_country][.codeset][@modifier]
- .CE
- to extract its parts. The initial locale is then set by calling
- fB::msgcat::mclocalefR with the argument
- .CS
- language[_country][_modifier]
- .CE
- On Windows, if none of those environment variables is set, msgcat will
- attempt to extract locale information from the
- registry. If all these attempts to discover an initial locale
- from the user's environment fail, msgcat defaults to an initial
- locale of ``C''.
- .PP
- When a locale is specified by the user, a ``best match'' search is
- performed during string translation. For example, if a user specifies
- en_GB_Funky, the locales ``en_GB_Funky'', ``en_GB'', and ``en'' are
- searched in order until a matching translation string is found. If no
- translation string is available, then fB::msgcat::mcunknownfR is
- called.
- .SH "NAMESPACES AND MESSAGE CATALOGS"
- .PP
- Strings stored in the message catalog are stored relative
- to the namespace from which they were added. This allows
- multiple packages to use the same strings without fear
- of collisions with other packages. It also allows the
- source string to be shorter and less prone to typographical
- error.
- .PP
- For example, executing the code
- .CS
- fB::msgcat::mcsetfR en hello "hello from ::"
- namespace eval foo {
- fB::msgcat::mcsetfR en hello "hello from ::foo"
- }
- puts [fB::msgcat::mcfR hello]
- namespace eval foo {puts [fB::msgcat::mcfR hello]}
- .CE
- will print
- .CS
- hello from ::
- hello from ::foo
- .CE
- .PP
- When searching for a translation of a message, the
- message catalog will search first the current namespace,
- then the parent of the current namespace, and so on until
- the global namespace is reached. This allows child namespaces
- to "inherit" messages from their parent namespace.
- .PP
- For example, executing (in the ``en'' locale) the code
- .CS
- fB::msgcat::mcsetfR en m1 ":: message1"
- fB::msgcat::mcsetfR en m2 ":: message2"
- fB::msgcat::mcsetfR en m3 ":: message3"
- namespace eval ::foo {
- fB::msgcat::mcsetfR en m2 "::foo message2"
- fB::msgcat::mcsetfR en m3 "::foo message3"
- }
- namespace eval ::foo::bar {
- fB::msgcat::mcsetfR en m3 "::foo::bar message3"
- }
- namespace import fB::msgcat::mcfR
- puts "[fBmcfR m1]; [fBmcfR m2]; [fBmcfR m3]"
- namespace eval ::foo {puts "[fBmcfR m1]; [fBmcfR m2]; [fBmcfR m3]"}
- namespace eval ::foo::bar {puts "[fBmcfR m1]; [fBmcfR m2]; [fBmcfR m3]"}
- .CE
- will print
- .CS
- :: message1; :: message2; :: message3
- :: message1; ::foo message2; ::foo message3
- :: message1; ::foo message2; ::foo::bar message3
- .CE
- .SH "LOCATION AND FORMAT OF MESSAGE FILES"
- .PP
- Message files can be located in any directory, subject
- to 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:
- .CS
- es.msg -- spanish
- en_gb.msg -- United Kingdom English
- .CE
- .IP [3]
- The file contains a series of calls to fBmcsetfR and
- fBmcmsetfR, setting the necessary translation strings
- for the language, likely enclosed in a fBnamespace evalfR
- so that all source strings are tied to the namespace of
- the package. For example, a short fBes.msgfR might contain:
- .CS
- namespace eval ::mypackage {
- fB::msgcat::mcsetfR es "Free Beer!" "Cerveza Gracias!"
- }
- .CE
- .SH "RECOMMENDED MESSAGE SETUP FOR PACKAGES"
- .PP
- If a package is installed into a subdirectory of the
- fBtcl_pkgPathfR and loaded via fBpackage requirefR, the
- following procedure is recommended.
- .IP [1]
- During package installation, create a subdirectory
- fBmsgsfR under your package directory.
- .IP [2]
- Copy your *.msg files into that directory.
- .IP [3]
- Add the following command to your package
- initialization script:
- .CS
- # load language files, stored in msgs subdirectory
- fB::msgcat::mcloadfR [file join [file dirname [info script]] msgs]
- .CE
- .SH "POSITIONAL CODES FOR FORMAT AND SCAN COMMANDS"
- .PP
- It is possible that a message string used as an argument
- to fBformatfR might have positionally dependent parameters that
- might need to be repositioned. For example, it might be
- syntactically desirable to rearrange the sentence structure
- while translating.
- .CS
- format "We produced %d units in location %s" $num $city
- format "In location %s we produced %d units" $city $num
- .CE
- .PP
- This can be handled by using the positional
- parameters:
- .CS
- format "We produced %1\$d units in location %2\$s" $num $city
- format "In location %2\$s we produced %1\$d units" $num $city
- .CE
- .PP
- Similarly, positional parameters can be used with fBscanfR to
- extract values from internationalized strings.
- .SH CREDITS
- .PP
- The message catalog code was developed by Mark Harrison.
- .SH "SEE ALSO"
- format(n), scan(n), namespace(n), package(n)
- .SH KEYWORDS
- internationalization, i18n, localization, l10n, message, text, translation