1 /******************************************************************************
2 *
3 * Copyright (C) 2007, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * ID: prj 18.07.07 0.0.A.
11 *
12 * Author: Alexey Krivonogov ( gentee )
13 *
14 ******************************************************************************/
15
16 include
17 {
18 $"..\gt\gt.g"
19 $"..\macro\macro.g"
20 }
21
22 define
23 {
24 LANGEXT = "lng"
25 }
26
27 type langdata
28 {
29 str filename
30 str name
31 ustr native
32 str custom
33 // uint utf8
34 }
35
36 /*-----------------------------------------------------------------------------
37 *
38 * ID: language 12.10.06 1.1.A.
39 *
40 * Summary: Language type
41 *
42 -----------------------------------------------------------------------------*/
43
44 type language <inherit = gt>// index = str>
45 {
46 str path // Path to language files
47 str deflang // Default language
48 str curlang // Current language
49 str defin // Input string of default language
50 arr lnglist of langdata
51 macro macros
52 }
53 /*
54 global
55 {
56 str langempty
57 }
58 */
59 method uint language.getinfo( str filename )
60 {
61 str data datacut
62 gt gtdata
63 uint gti
64 uint newlng
65
66 data.read( filename )
67
68 datacut.substr( data, 0, data.findch( '/' ) + 2 )
69 gtdata.utf8 = 1
70 gtdata += datacut
71 gti as gtdata.find("data")
72 if !>i : return 0
73 newlng as .lnglist[ .lnglist.expand( 1 ) ]
74 // .utf8 = /*( gti.find( "utf8" ) ||*/ data.isprefutf8() )
75 filename.fgetparts( 0->str, newlng.filename, 0->str )
76 gti.get( "name", newlng.name )
77 gti.get( "native", newlng.native )
78 if .deflang %== newlng.filename : .defin = data
79 return 1
80 }
81
82 /*-----------------------------------------------------------------------------
83 *
84 -----------------------------------------------------------------------------*/
85
86 method uint language.getid( str langname )
87 {
88 uint i
89
90 fornum i, *.lnglist
91 {
92 if langname %== .lnglist[i].name : break
93 }
94 return i
95 }
96
97 /*-----------------------------------------------------------------------------
98 *
99 * ID: language_change 12.10.06 1.1.A.
100 *
101 * Summary:
102 *
103 -----------------------------------------------------------------------------*/
104
105 method uint language.change( str langname )
106 {
107 subfunc loadcustom( str custlang )
108 {
109 uint id = .getid( custlang )
110 if id < *.lnglist : .root().load( .lnglist[ id ].custom )
111 }
112
113 .clear()
114 .root().load( .defin )
115 loadcustom( .deflang )
116 if langname %!= .deflang
117 {
118 .read( "\(.path)\\\( langname ).\($LANGEXT)")
119 loadcustom( langname )
120 .curlang = langname
121 }
122 else : .curlang = .deflang
123 return 1
124 }
125
126 /*-----------------------------------------------------------------------------
127 *
128 * ID: language_change 12.10.06 1.1.A.
129 *
130 * Summary:
131 *
132 -----------------------------------------------------------------------------*/
133
134 method uint language.change( uint id )
135 {
136 if id >= *.lnglist : return 0
137 return .change( .lnglist[ id ].filename )
138 }
139
140 /*-----------------------------------------------------------------------------
141 *
142 * ID: language_get 12.10.06 1.1.A.
143 *
144 * Summary:
145 *
146 -----------------------------------------------------------------------------*/
147 /*
148 method ustr language.getlang( ustr name ret )
149 {
150 ret.clear()
151 .get( "default/\( str( name ))", ret )
152 // print("Len=\( *ret )\n")
153 return ret
154 }
155 */
156 /*-----------------------------------------------------------------------------
157 *
158 * ID: language_get 12.10.06 1.1.A.
159 *
160 * Summary:
161 *
162 -----------------------------------------------------------------------------*/
163
164 method ustr language.getlang( str name, ustr ret )
165 {
166 if name[0] == '^' && name[ *name - 1 ] == '^'
167 {
168 str in out
169 arrstr items
170 ustr space
171
172 space = ustr(" ")
173 in.substr( name, 1, *name - 2 )
174 in.split( items, ' ', $SPLIT_EMPTY )
175 foreach cur, items
176 {
177 if *ret : ret += space
178 if *cur && .find("default/\( cur )")
179 {
180 ustr stemp
181 .get( "default/\( cur )", stemp )
182 ret += stemp
183 }
184 else : ret += ustr( cur )
185 }
186 }
187 else : .get( "default/\( name )", ret )
188 .macros.replace( ret )
189 return ret
190 }
191
192 method ustr language.getlang( ustr name ret )
193 {
194 return .getlang( str( name ), ret )
195 }
196
197 /*-----------------------------------------------------------------------------
198 *
199 * ID: language_load 12.10.06 1.1.A.
200 *
201 * Summary:
202 *
203 -----------------------------------------------------------------------------*/
204
205 method uint language.load( str path deflang curlang )
206 {
207 ffind fd
208 uint count
209
210 .path = path
211 .deflang = deflang
212 .utf8 = 1
213
214 fd.init( "\(path)\\*.\($LANGEXT)", $FIND_FILE )
215 foreach cur, fd
216 {
217 if .getinfo( cur.fullname ) : count++
218 }
219 .change( deflang )
220 if *curlang : .change( curlang )
221
222 return count
223 }
224
225 method language.setmacro( str macroname, ustr value )
226 {
227 .macros[ macroname ] = value
228 }
229
230 method language.setmacro( str macroname, str value )
231 {
232 .macros[ macroname ] = ustr( value )
233 }
234
235 method ustr language.getlist<result>
236 {
237 uint i
238
239 fornum i, *.lnglist
240 {
241 result += ustr( "\(.lnglist[i].name) (" ) + .lnglist[i].native +
242 ustr( ")=\(.lnglist[i].filename)\l" )
243 }
244 result.trim( 0xa, $TRIM_SYS | $TRIM_RIGHT )
245 }
246
247 method uint language.load( str lang data )
248 {
249 uint id
250
251 if ( id = .getid( lang )) < *.lnglist
252 {
253 .lnglist[ id ].custom = data
254 if lang %== .curlang : .root().load( data )
255
256 return 1
257 }
258 return 0
259 }
260 /*
261 func mainlang<main>
262 {
263 language lng
264 ustr list
265
266 lng.load( $"k:\gentee\open source\gentee\lib\language\language", "english", "english")
267 lng.load( "english", "<default>
268 <test4 = \"Custom English\" />
269 </> ")
270 // lng.load( "russian", "п»ї<default>
271 // <test2 = \"СЌР»РµРјРµРЅС Ooops\" />
272 //</> ")
273 // lng.change( "english" )
274 print( "QQQ = \( str( lng.getlang( "^test1 + test2 + test3^", ustr("")))) \n" )
275 print( "Test1 = \( str( lng.getlang( "test1", ustr("")))) \n" )
276 print( "Test2 = \( str( lng.getlang( ustr("test2"), ustr(""))))\n")
277 print( "Test3 = \( str( lng.getlang( ustr("test3"), ustr(""))))\n")
278 lng.change( "russian" )
279 print( "Test1 = \( str( lng.getlang( "test1", ustr("")))) \n" )
280 print( "Test2 = \( str( lng.getlang( ustr("test2"), ustr(""))))\n")
281 print( "Test3 = \( str( lng.getlang( ustr("test4"), ustr(""))))\n")
282 list = lng.getlist()
283 print( "\(str(list))\ncurlang=\(lng.curlang) \( lng.getid( lng.curlang ))" )
284 getch()
285 }*/