Tcl Source Code

Check-in [674aa977f0]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added mcconfig -mcfilelocale, as TIP-specified.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | tip-404
Files: files | file ages | folders
SHA1: 674aa977f00872b6409cdc83a45511ea7ba84e80
User & Date: dkf 2012-08-28 14:15:01
Context
2012-08-28
14:28
And add the listing of supported options. Closed-Leaf check-in: 21d7b4b106 user: dkf tags: tip-404
14:15
Added mcconfig -mcfilelocale, as TIP-specified. check-in: 674aa977f0 user: dkf tags: tip-404
2012-08-27
17:24
Commit of Harald Oehlmann's TIP 404 patch without TIP 399 pieces and with some added documentation. ... check-in: b7c4598501 user: dkf tags: tip-404
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/msgcat.n.

29
30
31
32
33
34
35


36
37
38
39
40
41
42
.sp
\fB::msgcat::mcmset \fIlocale src-trans-list\fR
.sp
.VS "TIP 404"
\fB::msgcat::mcflset \fIsrc-string \fR?\fItranslate-string\fR?
.sp
\fB::msgcat::mcflmset \fIsrc-trans-list\fR


.VE "TIP 404"
.sp
\fB::msgcat::mcunknown \fIlocale src-string\fR
.BE
.SH DESCRIPTION
.PP
The \fBmsgcat\fR package provides a set of functions







>
>







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
.sp
\fB::msgcat::mcmset \fIlocale src-trans-list\fR
.sp
.VS "TIP 404"
\fB::msgcat::mcflset \fIsrc-string \fR?\fItranslate-string\fR?
.sp
\fB::msgcat::mcflmset \fIsrc-trans-list\fR
.sp
\fB::msgcat::mcconfig\fR ?\fIoption\fR? ?\fIvalue\fR?
.VE "TIP 404"
.sp
\fB::msgcat::mcunknown \fIlocale src-string\fR
.BE
.SH DESCRIPTION
.PP
The \fBmsgcat\fR package provides a set of functions
164
165
166
167
168
169
170























171
172
173
174
175
176
177
current locale.  The default action is to return
\fIsrc-string\fR.  This procedure can be redefined by the
application, for example to log error messages for each unknown
string.  The \fB::msgcat::mcunknown\fR procedure is invoked at the
same stack context as the call to \fB::msgcat::mc\fR.  The return value
of \fB::msgcat::mcunknown\fR is used as the return value for the call
to \fB::msgcat::mc\fR.  























.SH "LOCALE SPECIFICATION"
.PP
The locale is specified to \fBmsgcat\fR by a locale string
passed to \fB::msgcat::mclocale\fR.
The locale string consists of
a language code, an optional country code, and an optional
system-specific code, each separated by







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
current locale.  The default action is to return
\fIsrc-string\fR.  This procedure can be redefined by the
application, for example to log error messages for each unknown
string.  The \fB::msgcat::mcunknown\fR procedure is invoked at the
same stack context as the call to \fB::msgcat::mc\fR.  The return value
of \fB::msgcat::mcunknown\fR is used as the return value for the call
to \fB::msgcat::mc\fR.  
.TP
\fB::msgcat::mcconfig\fR ?\fIoption\fR? ?\fIvalue\fR?
.VS "TIP 404"
This routine is used to query or set the configuration of the package.
Without either \fIoption\fR or \fIvalue\fR, this returns the list of options
that are supported by the msgcat package. With just \fIoption\fR, it returns
the current value of that option, and with both \fIoption\fR and \fIvalue\fR
it sets the particualr option to the value.
.RS
.PP
The following options are currently supported:
.VE "TIP 404"
.TP
\fB\-mcfilelocale\fR
.VS "TIP 404"
This option describes the locale that is assumed (for the purposes of
\fB::msgcat::mcflset\fR and \fB::msgcat::mcflmset\fR) by the processing of the
message catalog file being loaded with \fB::msgcat::mcload\fR. An empty string
indicates the root locale. Note that while this value may be set, it is not
recommended that user code does so, as this value is also directly manipulated
by \fB::msgcat::mcload\fR.
.RE
.VE "TIP 404"
.SH "LOCALE SPECIFICATION"
.PP
The locale is specified to \fBmsgcat\fR by a locale string
passed to \fB::msgcat::mclocale\fR.
The locale string consists of
a language code, an optional country code, and an optional
system-specific code, each separated by

Changes to library/msgcat/msgcat.tcl.

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package require Tcl 8.5
# When the version number changes, be sure to update the pkgIndex.tcl file,
# and the installation directory in the Makefiles.
package provide msgcat 1.5.0

namespace eval msgcat {
    namespace export mc mcload mclocale mcmax mcmset mcpreferences mcset \
	    mcunknown mcflset mcflmset

    # Records the current locale as passed to mclocale
    variable Locale ""

    # Records the list of locales to search
    variable Loclist {}

    # Records the locale of the currently sourced message catalogue file; this
    # would be problematic if anyone were to recursively load a message
    # catalog for a different locale from inside a catalog, but that's not a
    # case that we really need to worry about.
    variable FileLocale

    # Records the mapping between source strings and translated strings.  The
    # dict key is of the form "<locale> <namespace> <src>", where locale and
    # namespace should be themselves dict values and the value is
    # the translated string.
    variable Msgs [dict create]








|











|







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package require Tcl 8.5
# When the version number changes, be sure to update the pkgIndex.tcl file,
# and the installation directory in the Makefiles.
package provide msgcat 1.5.0

namespace eval msgcat {
    namespace export mc mcload mclocale mcmax mcmset mcpreferences mcset \
	    mcunknown mcconfig mcflset mcflmset

    # Records the current locale as passed to mclocale
    variable Locale ""

    # Records the list of locales to search
    variable Loclist {}

    # Records the locale of the currently sourced message catalogue file; this
    # would be problematic if anyone were to recursively load a message
    # catalog for a different locale from inside a catalog, but that's not a
    # case that we really need to worry about.
    variable FileLocale ""

    # Records the mapping between source strings and translated strings.  The
    # dict key is of the form "<locale> <namespace> <src>", where locale and
    # namespace should be themselves dict values and the value is
    # the translated string.
    variable Msgs [dict create]

293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	if {[file exists $langfile]} {
	    incr x
	    set FileLocale [string tolower [file tail [file rootname $langfile]]]
	    if {"root" eq $FileLocale} {
		set FileLocale ""
	    }
	    uplevel 1 [list ::source -encoding utf-8 $langfile]
	    unset FileLocale
	}
    }
    return $x
}

# msgcat::mcset --
#







|







293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
	if {[file exists $langfile]} {
	    incr x
	    set FileLocale [string tolower [file tail [file rootname $langfile]]]
	    if {"root" eq $FileLocale} {
		set FileLocale ""
	    }
	    uplevel 1 [list ::source -encoding utf-8 $langfile]
	    set FileLocale ""
	}
    }
    return $x
}

# msgcat::mcset --
#
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Results:
#	Returns the new locale.

proc msgcat::mcflset {src {dest ""}} {
    variable FileLocale
    variable Msgs

    if {![info exists FileLocale]} {
	return -code error \
	    "must only be used inside a message catalog loaded with ::msgcat::mcload"
    }
    if {[llength [info level 0]] == 2} { ;# dest not specified
	set dest $src
    }

    set ns [uplevel 1 [list ::namespace current]]
    dict set Msgs $FileLocale $ns $src $dest
    return $dest







<
<
<
<







342
343
344
345
346
347
348




349
350
351
352
353
354
355
# Results:
#	Returns the new locale.

proc msgcat::mcflset {src {dest ""}} {
    variable FileLocale
    variable Msgs





    if {[llength [info level 0]] == 2} { ;# dest not specified
	set dest $src
    }

    set ns [uplevel 1 [list ::namespace current]]
    dict set Msgs $FileLocale $ns $src $dest
    return $dest
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Results:
#	Returns the number of pairs processed

proc msgcat::mcflmset {pairs} {
    variable FileLocale
    variable Msgs

    if {![info exists FileLocale]} {
	return -code error \
	    "must only be used inside a message catalog loaded with ::msgcat::mcload"
    }
    set length [llength $pairs]
    if {$length % 2} {
	return -code error "bad translation list:\
		should be \"[lindex [info level 0] 0] locale {src dest ...}\""
    }

    set ns [uplevel 1 [list ::namespace current]]







<
<
<
<







395
396
397
398
399
400
401




402
403
404
405
406
407
408
# Results:
#	Returns the number of pairs processed

proc msgcat::mcflmset {pairs} {
    variable FileLocale
    variable Msgs





    set length [llength $pairs]
    if {$length % 2} {
	return -code error "bad translation list:\
		should be \"[lindex [info level 0] 0] locale {src dest ...}\""
    }

    set ns [uplevel 1 [list ::namespace current]]
463
464
465
466
467
468
469





















470
471
472
473
474
475
476
	set len [string length $translated]
	if {$len>$max} {
	    set max $len
	}
    }
    return $max
}






















# Convert the locale values stored in environment variables to a form
# suitable for passing to [mclocale]
proc msgcat::ConvertLocale {value} {
    # Assume $value is of form: $language[_$territory][.$codeset][@modifier]
    # Convert to form: $language[_$territory][_$modifier]
    #







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
	set len [string length $translated]
	if {$len>$max} {
	    set max $len
	}
    }
    return $max
}

proc msgcat::mcconfig {option {value "\u0000\u0000\u0000"}} {
    # Handle sentinel
    if {$value eq "\u0000\u0000\u0000"} {
	unset value
    }

    switch $option {
	-mcfilelocale {
	    variable FileLocale
	    if {[info exists value]} {
		set FileLocale $value
	    }
	    return $FileLocale
	}
	default {
	    return -code error \
		"unknown property \"$option\": must be -mcfilelocale"
	}
    }
}

# Convert the locale values stored in environment variables to a form
# suitable for passing to [mclocale]
proc msgcat::ConvertLocale {value} {
    # Assume $value is of form: $language[_$territory][.$codeset][@modifier]
    # Convert to form: $language[_$territory][_$modifier]
    #