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 * @version $Id: translate.php,v 1.15 2005/03/08 14:24:51 umcesrjones Exp $
11 * @package WebCalendar
15 if ( empty ( $PHP_SELF ) && ! empty ( $_SERVER ) &&
16 ! empty ( $_SERVER['PHP_SELF'] ) ) {
17 $PHP_SELF = $_SERVER['PHP_SELF'];
19 if ( ! empty ( $PHP_SELF ) && preg_match ( "/\/includes\//", $PHP_SELF ) ) {
20 die ( "You can't access this file directly!" );
24 if ( empty ( $LANGUAGE ) ) {
28 // If set to use browser settings, use the user's language preferences
29 // from their browser.
31 if ( $LANGUAGE == "Browser-defined" || $LANGUAGE == "none" ) {
32 $lang = get_browser_language ();
33 if ( $lang == "none" )
36 if ( strlen ( $lang ) == 0 || $lang == 'none' ) {
37 $lang = "English-US"; // Default
40 $lang_file = "translations/" . $lang . ".txt";
42 $translation_loaded = false;
44 $PUBLIC_ACCESS_FULLNAME = "Public Access"; // default
48 * Unloads translations so we can switch languages and translate into a
51 * @param string $new_language New language file to load (just the base
52 * filename, no directory or file suffix. Example:
55 function reset_language ( $new_language ) {
56 global $lang_file, $translations, $basedir, $lang, $translation_loaded;
58 if ( $new_language != $lang || ! $translation_loaded ) {
59 $translations = array ();
60 $lang = $new_language;
61 $lang_file = "translations/" . $lang . ".txt";
62 load_translation_text ();
63 $translation_loaded = true;
70 * Loads all the language translation into an array for quick lookup.
72 * <b>Note:</b> There is no need to call this manually. It will be invoked by
73 * {@link translate()} the first time it is called.
75 function load_translation_text () {
76 global $lang_file, $translations, $basedir, $PUBLIC_ACCESS_FULLNAME, $fullname;
77 $translations = array ();
78 if ( strlen ( $basedir ) ) {
79 $lang_file_2 = "$basedir/$lang_file";
80 if ( file_exists ( $lang_file_2 ) )
81 $lang_file = $lang_file_2;
83 if ( ! file_exists ( $lang_file ) ) {
84 die_miserable_death ( "Cannot find language file: $lang_file" );
86 $fp = fopen ( $lang_file, "r" );
88 die_miserable_death ( "Could not open language file: $lang_file" );
90 while ( ! feof ( $fp ) ) {
91 $buffer = fgets ( $fp, 4096 );
92 $buffer = trim ( $buffer );
93 // stripslashes may cause problems with Japanese translations
94 // if so, we may have to make this configurable.
95 if ( get_magic_quotes_runtime() ) {
96 $buffer = stripslashes ( $buffer );
98 if ( substr ( $buffer, 0, 1 ) == "#" || strlen ( $buffer ) == 0 )
100 $pos = strpos ( $buffer, ":" );
101 $abbrev = substr ( $buffer, 0, $pos );
102 $abbrev = trim ( $abbrev );
103 $trans = substr ( $buffer, $pos + 1 );
104 $trans = trim ( $trans );
105 $translations[$abbrev] = $trans;
106 //echo "Abbrev: $abbrev<br />Trans: $trans<br />\n";
109 $PUBLIC_ACCESS_FULLNAME = translate ("Public Access" );
110 if ( $fullname == "Public Access" ) {
111 $fullname = $PUBLIC_ACCESS_FULLNAME;
118 * Translates a string from the default English usage to some other language.
120 * The first time that this is called, the translation file will be loaded
121 * (with {@link load_translation_text()}).
123 * @param string $str Text to translate
125 * @return string The translated text, if available. If no translation is
126 * avalailable, then the original untranslated text is returned.
128 function translate ( $str ) {
129 global $translations, $translation_loaded;
131 if ( ! $translation_loaded ) {
132 $translation_loaded = true;
133 load_translation_text ();
136 $str = trim ( $str );
137 if ( ! empty ( $translations[$str] ) )
138 return $translations[$str];
140 // To help in translating, use the following to help identify text that
141 // has not been translated
142 // return "<blink>$str</blink>";
150 * Translates text and prints it.
152 * This is just an abbreviation for:
154 * <code>echo translate ( $str )</code>
156 * @param string $str Text to translate and print
160 function etranslate ( $str ) {
161 echo translate ( $str );
165 * Translates and removes HTML from text, and returns it.
167 * This is useful for tooltips, which barf on HTML.
169 * <b>Note:</b> {@link etooltip()} will print the result rather than return the
172 * @param string $str Text to translate
174 * @return string The translated text with all HTML removed
176 function tooltip ( $str ) {
177 $ret = translate ( $str );
178 $ret = eregi_replace ( "<[^>]+>", "", $ret );
179 $ret = eregi_replace ( "\"", "'", $ret );
184 * Translates and removes HTML from text, and prints it.
186 * This is useful for tooltips, which barf on HTML.
188 * <b>Note:</b> {@link tooltip()} will return the result rather than print
191 * @param string $str Text to translate and print
195 function etooltip ( $str ) {
196 echo tooltip ( $str );