3 * Language translation functions.
5 * The idea is very much stolen from the GNU translate C library.
7 * Although there is a PHP gettext() function, I prefer to use this home-grown
8 * translate function since it is simpler to work with.
10 * @package WebCalendar
14 if ( empty ( $PHP_SELF ) && ! empty ( $_SERVER ) &&
15 ! empty ( $_SERVER['PHP_SELF'] ) ) {
16 $PHP_SELF = $_SERVER['PHP_SELF'];
18 if ( ! empty ( $PHP_SELF ) && preg_match ( "/\/includes\//", $PHP_SELF ) ) {
19 die ( "You can't access this file directly!" );
23 if ( empty ( $LANGUAGE ) ) {
27 // If set to use browser settings, use the user's language preferences
28 // from their browser.
30 if ( $LANGUAGE == "Browser-defined" || $LANGUAGE == "none" ) {
31 $lang = get_browser_language ();
32 if ( $lang == "none" )
35 if ( strlen ( $lang ) == 0 || $lang == 'none' ) {
36 $lang = "English-US"; // Default
39 $lang_file = "translations/" . $lang . ".txt";
41 $translation_loaded = false;
43 $PUBLIC_ACCESS_FULLNAME = "Public Access"; // default
47 * Unloads translations so we can switch languages and translate into a
50 * @param string $new_language New language file to load (just the base
51 * filename, no directory or file suffix. Example:
54 function reset_language ( $new_language ) {
55 global $lang_file, $translations, $basedir, $lang, $translation_loaded;
57 if ( $new_language != $lang || ! $translation_loaded ) {
58 $translations = array ();
59 $lang = $new_language;
60 $lang_file = "translations/" . $lang . ".txt";
61 load_translation_text ();
62 $translation_loaded = true;
69 * Loads all the language translation into an array for quick lookup.
71 * <b>Note:</b> There is no need to call this manually. It will be invoked by
72 * {@link translate()} the first time it is called.
74 function load_translation_text () {
75 global $lang_file, $translations, $basedir, $PUBLIC_ACCESS_FULLNAME, $fullname;
76 $translations = array ();
77 if ( strlen ( $basedir ) ) {
78 $lang_file_2 = "$basedir/$lang_file";
79 if ( file_exists ( $lang_file_2 ) )
80 $lang_file = $lang_file_2;
82 if ( ! file_exists ( $lang_file ) ) {
83 die_miserable_death ( "Cannot find language file: $lang_file" );
85 $fp = fopen ( $lang_file, "r" );
87 die_miserable_death ( "Could not open language file: $lang_file" );
89 while ( ! feof ( $fp ) ) {
90 $buffer = fgets ( $fp, 4096 );
91 $buffer = trim ( $buffer );
92 // stripslashes may cause problems with Japanese translations
93 // if so, we may have to make this configurable.
94 if ( get_magic_quotes_runtime() ) {
95 $buffer = stripslashes ( $buffer );
97 if ( substr ( $buffer, 0, 1 ) == "#" || strlen ( $buffer ) == 0 )
99 $pos = strpos ( $buffer, ":" );
100 $abbrev = substr ( $buffer, 0, $pos );
101 $abbrev = trim ( $abbrev );
102 $trans = substr ( $buffer, $pos + 1 );
103 $trans = trim ( $trans );
104 $translations[$abbrev] = $trans;
105 //echo "Abbrev: $abbrev<br />Trans: $trans<br />\n";
108 $PUBLIC_ACCESS_FULLNAME = translate ("Public Access" );
109 if ( $fullname == "Public Access" ) {
110 $fullname = $PUBLIC_ACCESS_FULLNAME;
117 * Translates a string from the default English usage to some other language.
119 * The first time that this is called, the translation file will be loaded
120 * (with {@link load_translation_text()}).
122 * @param string $str Text to translate
124 * @return string The translated text, if available. If no translation is
125 * avalailable, then the original untranslated text is returned.
127 function translate ( $str ) {
128 global $translations, $translation_loaded;
130 if ( ! $translation_loaded ) {
131 $translation_loaded = true;
132 load_translation_text ();
135 $str = trim ( $str );
136 if ( ! empty ( $translations[$str] ) )
137 return $translations[$str];
139 // To help in translating, use the following to help identify text that
140 // has not been translated
141 // return "<blink>$str</blink>";
149 * Translates text and prints it.
151 * This is just an abbreviation for:
153 * <code>echo translate ( $str )</code>
155 * @param string $str Text to translate and print
159 function etranslate ( $str ) {
160 echo translate ( $str );
164 * Translates and removes HTML from text, and returns it.
166 * This is useful for tooltips, which barf on HTML.
168 * <b>Note:</b> {@link etooltip()} will print the result rather than return the
171 * @param string $str Text to translate
173 * @return string The translated text with all HTML removed
175 function tooltip ( $str ) {
176 $ret = translate ( $str );
177 $ret = eregi_replace ( "<[^>]+>", "", $ret );
178 $ret = eregi_replace ( "\"", "'", $ret );
183 * Translates and removes HTML from text, and prints it.
185 * This is useful for tooltips, which barf on HTML.
187 * <b>Note:</b> {@link tooltip()} will return the result rather than print
190 * @param string $str Text to translate and print
194 function etooltip ( $str ) {
195 echo tooltip ( $str );