summaryrefslogtreecommitdiff
path: root/app/xlockmore/gettext.tcl
blob: f557e4c2bd438ca3f8a15bfba1fdd27dbec80c3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#
# gettext like message catalogue system
#
# By: YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
#
# $Id: gettext.tcl,v 1.2 2006/11/26 17:54:07 matthieu Exp $

#
# How to use
#
# 1) Create catalogue file named "language.<$LANG>.tcl".
#    If your $LANG environment valuable is "ja_JP.eucJP", you make
#    "language.ja_JP.eucJP.tcl" or "language.ja_JP.tcl" or "language.ja.tcl".
#    Here is some example for English-Japanese translation.
#
#    -------
#    settext "hello" "こんにちは"
#    settext "OK"    "了解"
#    ...
#    ------
#
# 2) Setup auto-load path. You must make file named "tclIndex" before
#    use this system. See "auto_mkindex" Tcl command help.
#
# 3) Add these lines at top of your tcl script file.
#
#    -----
#    set auto_path [linsert $auto_path end .]  ;# All script files in "."
#    set_catalogue [set_language]
#    -----
#
# 4) Use "gettext" command for message transration.
#    You can also use "_" command for easy use.
#
#    -----
#    gettext "hello"   ;# -> "こんにちは"
#    _ "OK"            ;# -> "了解"
#    -----
#

#####################################################################
# Internal valueable. Don't use it.
set g_language "C"
set g_resource("__dummy__") ""

######################################################################
# Global functions
######################################################################

#
# Select language catarogue file
#
# If catalogue file is found, return language string.
# If not found catalogue file, return "C".
#
proc set_language {{lang ""}} {
    global g_language env

    # If some argument is given, force to set language.
    if {$lang != ""} then {
	set g_language $lang
	return $g_language
    }

    if {![info exists env(LANG)]} {
	set g_language "C"
	return "C"
    }

    foreach f "$env(LANG) [lang_substr1 $env(LANG)] [lang_substr2 $env(LANG)]" {
	if {$f == ""} {continue}
	if {[file exist language.$f.tcl]} {
	    set g_language $f
	}
    }

    return $g_language
}

#
# Read catalogue file named "language.$lang.tcl".
#
proc set_catalogue {lang} {
    if {$lang == "C"} {return}

    if {[file exist language.$lang.tcl]} {
	source language.$lang.tcl
    }
}

#
# Get translated string
#
proc gettext {string} {
    global g_resource

    if {[info exist g_resource($string)]} {
	return $g_resource($string)
    } else {
	return $string
    }
}

#
# Set translation catalogue string.
#
proc settext {from to} {
    global g_resource

    set g_resource($from) $to
}

#
# For easy use
#
proc _ {string} {
    return [gettext $string]
}


########################################################################
# Internal functions. Don't use it.
########################################################################

#
# "ja_JP.eucJP" -> "ja_JP"
# "ja_JP"       -> "ja_JP"
#
proc lang_substr1 {str} {
    if {[regexp -nocase {^([a-z]+_[a-z]+)\.[a-z]+$} $str a b]} {
	return $b
    } elseif {[regexp -nocase {^([a-z]+_[a-z]+)$} $str a b]} {
	return $b
    } else {
	return ""
    }
}

#
# "ja_JP.eucJP" -> "ja"
# "ja_JP"       -> "ja"
# "ja"          -> "ja"
#
proc lang_substr2 {str} {
    if {[regexp -nocase {^([a-z]+)_[a-z]+\.[a-z]+$} $str a b]} {
	return $b
    } elseif {[regexp -nocase {^([a-z]+)_[a-z]+$} $str a b]} {
	return $b
    } elseif {[regexp -nocase {^([a-z]+)$} $str a b]} {
	return $b
    } else {
	return ""
    }
}

# end