2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 08/25/2003 *
4 * =================== Last change: 11/29/2005 *
6 * -------------------------------------------------------------------- *
7 * File : language-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Language functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Sprachfunktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
44 // "Getter" for language strings
45 // @TODO Rewrite all language constants to this function.
46 function getMessage ($messageId) {
47 // Default is not found!
48 $return = '!' . $messageId . '!';
50 // Is the language string found?
51 if (isMessageIdValid($messageId)) {
52 // Language array element found in small_letters
53 $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
55 // Missing language constant
56 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
64 function initMessages () {
65 $GLOBALS['messages'][getLanguage()] = array();
69 function addMessages ($messages) {
71 $GLOBALS['messages'][getCurrentLanguage()] = merge_array($GLOBALS['messages'][getCurrentLanguage()], $messages);
73 // Don't count them if we don't want it
74 if (isset($GLOBALS['count'])) return;
77 if (isset($GLOBALS['msg_count'][getCurrentLanguage()])) {
78 $GLOBALS['msg_count'][getCurrentLanguage()] += count($messages);
80 $GLOBALS['msg_count'][getCurrentLanguage()] = count($messages);
84 // Checks wether given message id is valid
85 function isMessageIdValid ($messageId) {
86 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
89 // Getter for current language
90 function getCurrentLanguage () {
91 return $GLOBALS['language'];
94 // Setter for current language
95 function setCurrentLanguage ($language) {
96 $GLOBALS['language'] = (string) $language;
99 // "Getter" for language
100 function getLanguage () {
101 // Default is 'de'. DO NOT CHANGE THIS!!!
104 // Set default return value to default language from config
105 if (isConfigEntrySet('DEFAULT_LANG')) $ret = getConfig('DEFAULT_LANG');
107 // Is the variable set
108 if (isGetRequestElementSet('mx_lang')) {
109 // Accept only first 2 chars
110 $ret = substr(getRequestElement('mx_lang'), 0, 2);
111 } elseif (isset($GLOBALS['language'])) {
113 $ret = getCurrentLanguage();
114 } elseif (isSessionVariableSet('mx_lang')) {
115 // Return stored value from cookie
116 $ret = getSession('mx_lang');
118 // Fixes a warning before the session has the mx_lang constant
119 if (empty($ret)) $ret = getConfig('DEFAULT_LANG');
123 setCurrentLanguage($ret);
129 // "Setter" for language
130 function setLanguage ($lang) {
131 // Accept only first 2 chars and still secure them
132 $lang = substr(secureString($lang), 0, 2);
135 setSession('mx_lang', $lang);
138 // Checks wether a language file is there for optional extension
139 function isLanguageIncludeReadable ($ext_name = 'none') {
140 // Do we have array element?
141 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
143 if ($ext_name == 'none') {
145 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
147 // Extension's language file
148 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
151 // Look for file if no extension name is provided
152 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
153 //* DEBUG: */ print __FUNCTION__.':'.$ext_name.'='.$languageInclude.'='.intval(isIncludeReadable($languageInclude)).'<br />';
157 return $GLOBALS['lang_inc'][$ext_name];
160 // Load the current language file or fixes it to 'de'
161 // If ext_name is 'none', load general language support, else load extension's
162 // language file. In installation phase load the install language file.
163 function loadLanguageFile ($ext_name = 'none') {
164 // Try to get language from session
165 $currLanguage = getLanguage();
167 // Set default language if it is not (yet) set
168 if (is_null($currLanguage)) {
169 // Get it from config
170 $currLanguage = getConfig('DEFAULT_LANG');
172 // And save it in session
173 setLanguage($currLanguage);
176 // Do we have the language file NOT?
177 if (!isLanguageIncludeReadable($ext_name)) {
178 // Switch to default (DO NOT CHANGE!!!)
181 // And set it temporarily
182 setConfigEntry('DEFAULT_LANG', 'de');
185 // Is the file there?
186 if (isLanguageIncludeReadable($ext_name)) {
187 // Load language file
188 loadLanguageInclude($ext_name);
189 } elseif ((isDebugModeEnabled()) && (getOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
190 // No language file is not so good...
191 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
198 // Check for installation mode
199 if ((isInstallationPhase()) || (!isAdminRegistered())) {
200 // Load language file
201 loadLanguageInclude('install');
205 // Loads the language file
206 function loadLanguageInclude ($ext_name = 'none') {
208 if ($ext_name == 'none') {
210 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
212 // Extension's language file
213 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
216 // Check it before loading
217 if (isLanguageIncludeReadable($ext_name)) {
219 loadIncludeOnce($languageInclude);
222 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
226 // Getter for an array of valid languages with no except by default
227 function getValidLanguages ($except = '') {
228 // @TODO These are all valid languages, again hard-coded
229 $langs = array('de' => 'de', 'en' => 'en');
231 // Should we keep one out?
232 if (!empty($except)) {
234 unset($langs[$except]);
241 // Compares two language files
242 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
244 $GLOBALS['lang_diff'][$target] = array();
245 $GLOBALS['lang_diff_count'][$target] = 0;
246 if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
248 // *Does* match by default
251 // Is one not readable?
252 if (!isIncludeReadable($source)) {
253 // Please report this bug!
254 debug_report_bug(__FUNCTION__ . ': Source file ' . $source . ' is not readable.');
255 } elseif (!isIncludeReadable($target)) {
256 // Please report this bug!
257 debug_report_bug(__FUNCTION__ . ': Target file ' . $target . ' is not readable.');
258 } elseif ($targetLanguage == getCurrentLanguage()) {
260 debug_report_bug(__FUNCTION__ . ': Target language ' . $targetLanguage . ' is same as current.');
263 // Backup current messages/language
264 $backupLang = getCurrentLanguage();
265 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
266 $GLOBALS['messages'][$backupLang] = array();
268 // Both are readable so include current language file
269 $GLOBALS['count'] = false;
270 loadInclude($source);
271 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
272 unset($GLOBALS['count']);
274 // Set target language
275 setCurrentLanguage($targetLanguage);
277 // Do we have an array?
278 if (!isset($GLOBALS['messages'][$targetLanguage])) {
279 // Then create it to avoid notice
280 $GLOBALS['messages'][$targetLanguage] = array();
281 $GLOBALS['msg_count'][$targetLanguage] = 0;
284 // Load target language file
285 loadInclude($target);
286 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
289 setCurrentLanguage($backupLang);
290 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
291 unset($messages[$backupLang]);
294 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
298 // Check all differences
299 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
301 if (!isset($GLOBALS['msgs'][$target][$key])) {
302 // Then add is as difference
303 $GLOBALS['lang_diff'][$target][$key] = $value;
306 $GLOBALS['lang_diff_count'][$target]++;
307 $GLOBALS['lang_diff_count']['total']++;
316 // Getter for getting difference of target file
317 function getLanguageComparisonDifference ($target) {
318 return $GLOBALS['lang_diff_count'][$target];