ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

lang.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
26 class lang_1_0_0{
27
38 var $LangList = false;
39
50 var $StringSet = array();
51
62 var $Language = false;
63
74 var $AutoTranslationsWereLoaded = false;
75
86 var $BlockSettings = false;
87 var $Cache = false;
88 var $CachedMultyFS = false;
89 var $PageJS = false;
90 var $Security = false;
91 var $String = false;
92
107 function __construct()
108 {
109 try
110 {
111 $this->BlockSettings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
112 $this->Cache = get_package( 'cache' , 'last' , __FILE__ );
113 $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
114 $this->PageJS = get_package( 'page::page_js' , 'last' , __FILE__ );
115 $this->Security = get_package( 'security' , 'last' , __FILE__ );
116 $this->String = get_package( 'string' , 'last' , __FILE__ );
117
118 $this->get_locale();
119 }
120 catch( Exception $e )
121 {
122 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
123 }
124 }
125
144 function get_client_language()
145 {
146 $Splits = array();
147
148 foreach( explode( ',' , @$_SERVER[ 'HTTP_ACCEPT_LANGUAGE' ] ) as $Lang )
149 {
150 $Pattern = '/^(?P<primarytag>[a-zA-Z]{2,8})'.
151 '(?:-(?P<subtag>[a-zA-Z]{2,8}))?(?:(?:;q=)'.
152 '(?P<quantifier>\d\.\d))?$/';
153
154 if( preg_match( $Pattern , $Lang , $Splits ) )
155 {
156 return( $Splits[ 'primarytag' ] );
157 }
158 }
159
160 return( false );
161 }
162
181 function pre_generation( $Options )
182 {
183 try
184 {
185 $Path = _get_package_relative_path_ex( 'lang' , '1.0.0' );
186 $Path = $Path.'/include/js/lang.core.'.$this->Language.'.js';
187
188 if( $this->CachedMultyFS->file_exists( $Path ) === false )
189 {
190 $this->PageJS->add_javascript( '{http_host}/'.$Path );
191 $Content = $this->CachedMultyFS->get_template( __FILE__ , 'lang.core.js.tpl' );
192 $Content = str_replace( '{locale}' , $this->Language , $Content );
193 $this->CachedMultyFS->file_put_contents( $Path , $Content );
194 }
195
196 $this->PageJS->add_javascript( '{http_host}/'.$Path );
197
198 $this->include_strings_js( 'lang' );
199 }
200 catch( Exception $e )
201 {
202 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
203 }
204 }
205
228 function dispatch_string_data( $RawData )
229 {
230 try
231 {
232 $tmp1 = explode( '=' , $RawData );
233
234 return( array( $tmp1[ 0 ] , 'default' , $tmp1[ 1 ] ) );
235 }
236 catch( Exception $e )
237 {
238 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
239 }
240 }
241
264 function include_strings( $PackageName , $PackageVersion = 'last' )
265 {
266 try
267 {
268 $Path = _get_package_relative_path_ex( $PackageName , $PackageVersion );
269 $TopPackageName = _get_top_package_name( $PackageName );
270 $LanguageFilePath = $Path.'/res/lang/'.$TopPackageName.'.'.$this->Language;
271
272 if( file_exists( $LanguageFilePath ) )
273 {
274 $RawData = $this->CachedMultyFS->file_get_contents( $LanguageFilePath , 'cleaned' );
275 $this->load_data( $RawData );
276 }
277 }
278 catch( Exception $e )
279 {
280 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
281 }
282 }
283
314 private function compile_script_line( $StringAlias , $Condition , $Translation )
315 {
316 try
317 {
318 return(
319 "if( !ultimix.lang.Strings[ ultimix.lang.Locale ][ '$StringAlias' ] ) ".
320 "ultimix.lang.Strings[ ultimix.lang.Locale ][ '$StringAlias' ] = {};\r\n".
321 "ultimix.lang.Strings[ ultimix.lang.Locale ]".
322 "[ '$StringAlias' ][ '$Condition' ] = '".trim( $Translation , '/' )."';\r\n"
323 );
324 }
325 catch( Exception $e )
326 {
327 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
328 }
329 }
330
353 private function compile_lang_javascript( $LangFilePath , $ScriptPath )
354 {
355 try
356 {
357 if( $this->CachedMultyFS->file_exists( $ScriptPath ) === false ||
358 $this->Cache->get_data( $ScriptPath ) === false )
359 {
360 $Strings = $this->CachedMultyFS->file_get_contents( $LangFilePath , 'exploded' );
361
362 $Script = '';
363 foreach( $Strings as $k => $v )
364 {
365 list( $StringAlias , $Condition , $Translation ) = $this->dispatch_string_data( $v );
366 $Translation = str_replace( "'" , "\\'" , $Translation );
367 $Script .= $this->compile_script_line( $StringAlias , $Condition , $Translation );
368 }
369
370 $this->CachedMultyFS->file_put_contents( $ScriptPath , $Script );
371 }
372 }
373 catch( Exception $e )
374 {
375 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
376 }
377 }
378
401 function include_strings_js( $PackageName , $PackageVersion = 'last' )
402 {
403 try
404 {
405 $Path = _get_package_relative_path_ex( $PackageName , $PackageVersion );
406 $TopPackageName = _get_top_package_name( $PackageName );
407 $LangFilePath = $Path.'/res/lang/'.$TopPackageName.'.'.$this->Language;
408
409 if( $this->CachedMultyFS->file_exists( $LangFilePath ) )
410 {
411 $ScriptPath = $Path.'/include/js/'.$TopPackageName.'.'.$this->Language.'.js';
412
413 mkdir_ex( $Path.'/include/' );
414 mkdir_ex( $Path.'/include/js/' );
415
416 $this->compile_lang_javascript( $LangFilePath , $ScriptPath );
417
418 $this->PageJS->add_javascript( '{http_host}/'.$ScriptPath );
419 }
420 }
421 catch( Exception $e )
422 {
423 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
424 }
425 }
426
445 function get_list_of_languages()
446 {
447 try
448 {
449 $Languages = file_get_contents( dirname( __FILE__ ).'/conf/cf_lang_list' );
450 $Languages = str_replace( "\r" , "\n" , $Languages );
451 $Languages = str_replace( "\n\n" , "\n" , $Languages );
452 $Languages = explode( "\n" , $Languages );
453
454 return( $Languages );
455 }
456 catch( Exception $e )
457 {
458 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
459 }
460 }
461
480 function get_locale()
481 {
482 try
483 {
484 if( $this->Language === false )
485 {
486 if( $this->LangList === false )
487 {
488 $this->LangList = $this->get_list_of_languages();
489 }
490 if( $this->Security->get_c( 'client_lang' ) )
491 {
492 $this->Language = $this->Security->get_c( 'client_lang' , 'command' );
493 }
494 elseif( ( $Key = array_search( $this->get_client_language() , $this->LangList ) ) !== false )
495 {
496 $this->Language = $this->LangList[ $Key ];
497 }
498 else
499 {
500 $this->Language = $this->CachedMultyFS->get_config( __FILE__ , 'cf_locale_conf' );
501 }
502 }
503
504 return( $this->Language );
505 }
506 catch( Exception $e )
507 {
508 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
509 }
510 }
511
530 function load_data( $RawData )
531 {
532 try
533 {
534 if( strlen( $RawData ) )
535 {
536 $RawData = explode( "\n" , $RawData );
537
538 foreach( $RawData as $rd )
539 {
540 list( $StringAlias , $Condition , $Translation ) = $this->dispatch_string_data( $rd );
541
542 if( isset( $this->StringSet[ $StringAlias ] ) === false )
543 {
544 $this->StringSet[ $StringAlias ] = array();
545 }
546
547 $this->StringSet[ $StringAlias ][ $Condition ] = $Translation;
548 }
549 }
550 }
551 catch( Exception $e )
552 {
553 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
554 }
555 }
556
575 function load_translations_from_file( $LanguageFilePath )
576 {
577 try
578 {
579 if( file_exists( $LanguageFilePath ) )
580 {
581 $RawData = $this->CachedMultyFS->file_get_contents( $LanguageFilePath , 'cleaned' );
582 $this->load_data( $RawData );
583 }
584 }
585 catch( Exception $e )
586 {
587 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
588 }
589 }
590
613 function load_translations_from_package( $PackageName , $PackageVersion )
614 {
615 try
616 {
617 $PackagePath = _get_package_relative_path_ex( $PackageName , $PackageVersion );
618 $TopPackageName = _get_top_package_name( $PackageName );
619
620 $this->load_translations_from_file( "$PackageName/res/lang/$TopPackageName.$this->Language" );
621 }
622 catch( Exception $e )
623 {
624 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
625 }
626 }
627
642 private function force_load_translations()
643 {
644 try
645 {
646 $Paths = _get_loaded_packages_paths();
647
648 $Paths [] = array( 'directory' => '.' , 'package_name' => 'lang' );
649 $Key = implode_ex( '' , $Paths , 'directory' );
650 if( $this->Cache->data_exists( $Key ) === true )
651 {
652 $this->StringSet = unserialize( $this->Cache->get_data( $Key ) );
653 }
654 else
655 {
656 foreach( $Paths as $p )
657 {
658 $Name = _get_top_package_name( $p[ 'package_name' ] );
659 $Path = $p[ 'directory' ]."/res/lang/$Name.$this->Language";
660
661 $this->load_translations_from_file( $Path );
662 }
663 $this->Cache->add_data( $Key , serialize( $this->StringSet ) );
664 }
665 }
666 catch( Exception $e )
667 {
668 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
669 }
670 }
671
686 function load_translations()
687 {
688 try
689 {
690 if( $this->AutoTranslationsWereLoaded === false )
691 {
692 $this->get_locale();
693
694 $this->force_load_translations();
695
696 $this->AutoTranslationsWereLoaded = true;
697 }
698 }
699 catch( Exception $e )
700 {
701 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
702 }
703 }
704
731 function get_string( $StringAlias , $Value = 'default' )
732 {
733 try
734 {
735 if( isset( $this->StringSet[ $StringAlias ] ) === false ||
736 ( $Value == 'default' && isset( $this->StringSet[ $StringAlias ][ $Value ] ) === false ) )
737 {
738 return( $StringAlias );
739 }
740
741 if( $Value == 'default' && isset( $this->StringSet[ $StringAlias ][ 'default' ] ) !== false )
742 {
743 return( $this->StringSet[ $StringAlias ][ 'default' ] );
744 }
745
746 return( $StringAlias );
747 }
748 catch( Exception $e )
749 {
750 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
751 }
752 }
753 }
754
755 ?>