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 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://www.mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // "Getter" for language strings
44 // @TODO Rewrite all language constants to this function.
45 function getMessage ($messageId) {
46 // Default is not found
47 $return = '!' . $messageId . '!';
49 // Is the language string found?
50 if (isMessageIdValid($messageId)) {
51 // Language array element found in small_letters
52 $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
54 // Missing language constant
55 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
62 // Getter for message string as a mask
63 function getMaskedMessage ($messageId, $data) {
65 $message = sprintf(getMessage($messageId), $data);
72 function initMessages () {
73 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'getLanguage()=' . getLanguage());
74 $GLOBALS['messages'][getLanguage()] = array();
78 function addMessages ($messages) {
79 // Cache current language
80 $currentLanguage = getCurrentLanguage();
81 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'currentLanguage=' . $currentLanguage);
84 $GLOBALS['messages'][$currentLanguage] = merge_array($GLOBALS['messages'][$currentLanguage], $messages);
86 // Don't count them if we don't want it
87 if (isset($GLOBALS['count'])) {
92 if (isset($GLOBALS['msg_count'][$currentLanguage])) {
93 $GLOBALS['msg_count'][$currentLanguage] += count($messages);
95 $GLOBALS['msg_count'][$currentLanguage] = count($messages);
99 // Checks wether given message id is valid
100 function isMessageIdValid ($messageId) {
101 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
104 // Getter for current language
105 function getCurrentLanguage () {
106 return $GLOBALS['language'];
109 // Setter for current language
110 function setCurrentLanguage ($language) {
111 $GLOBALS['language'] = (string) $language;
114 // "Getter" for language
115 function getLanguage () {
116 // Default is 'de'. DO NOT CHANGE THIS!!!
119 // Set default return value to default language from config
120 if (isConfigEntrySet('DEFAULT_LANG')) $ret = getDefaultLanguage();
122 // Is the variable set
123 if (isGetRequestParameterSet('mx_lang')) {
124 // Accept only first 2 chars
125 $ret = substr(getRequestParameter('mx_lang'), 0, 2);
126 } elseif (isset($GLOBALS['language'])) {
128 $ret = getCurrentLanguage();
129 } elseif (isSessionVariableSet('mx_lang')) {
130 // Return stored value from cookie
131 $ret = getSession('mx_lang');
133 // Fixes a warning before the session has the mx_lang constant
134 if (empty($ret)) $ret = getDefaultLanguage();
138 setCurrentLanguage($ret);
144 // "Setter" for language
145 function setLanguage ($lang) {
146 // Accept only first 2 chars and still secure them
147 $lang = substr(secureString($lang), 0, 2);
150 setSession('mx_lang', $lang);
153 // Checks wether a language file is there for optional extension
154 function isLanguageIncludeReadable ($ext_name = 'none') {
155 // Do we have array element?
156 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
158 if ($ext_name == 'none') {
160 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
162 // Extension's language file
163 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
166 // Look for file if no extension name is provided
167 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
168 //* DEBUG: */ debugOutput(__FUNCTION__.':'.$ext_name.'='.$languageInclude.'='.intval(isIncludeReadable($languageInclude)));
172 return $GLOBALS['lang_inc'][$ext_name];
175 // Load the current language file or fixes it to 'de'
176 // If ext_name is 'none', load general language support, else load extension's
177 // language file. In installation phase load the install language file.
178 function loadLanguageFile ($ext_name = 'none') {
179 // Try to get language from session
180 $currLanguage = getLanguage();
182 // Set default language if it is not (yet) set
183 if (is_null($currLanguage)) {
184 // Get it from config
185 $currLanguage = getDefaultLanguage();
187 // And save it in session
188 setLanguage($currLanguage);
191 // Do we have the language file NOT?
192 if (!isLanguageIncludeReadable($ext_name)) {
193 // Switch to default (DO NOT CHANGE!!!)
196 // And set it temporarily
197 setConfigEntry('DEFAULT_LANG', 'de');
200 // Is the file there?
201 if (isLanguageIncludeReadable($ext_name)) {
202 // Load language file
203 loadLanguageInclude($ext_name);
204 } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
205 // No language file is not so good...
206 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
213 // Check for installation mode
214 if ((isInstallationPhase()) || (!isAdminRegistered())) {
215 // Load language file
216 loadLanguageInclude('install');
220 // Loads the language file
221 function loadLanguageInclude ($ext_name = 'none') {
223 if ($ext_name == 'none') {
225 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
227 // Extension's language file
228 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
231 // Check it before loading
232 if (isLanguageIncludeReadable($ext_name)) {
234 loadIncludeOnce($languageInclude);
237 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
241 // Getter for an array of valid languages with no except by default
242 function getValidLanguages ($except = '') {
243 // @TODO These are all valid languages, again hard-coded
244 $langs = array('de' => 'de', 'en' => 'en');
246 // Should we keep one out?
247 if (!empty($except)) {
249 unset($langs[$except]);
256 // Compares two language files
257 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
259 $GLOBALS['lang_diff'][$target] = array();
260 $GLOBALS['lang_diff_count'][$target] = 0;
261 if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
263 // *Does* match by default
266 // Is one not readable?
267 if (!isIncludeReadable($source)) {
268 // Please report this bug!
269 debug_report_bug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
270 } elseif (!isIncludeReadable($target)) {
271 // Please report this bug!
272 debug_report_bug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
273 } elseif ($targetLanguage == getCurrentLanguage()) {
275 debug_report_bug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
278 // Backup current messages/language
279 $backupLang = getCurrentLanguage();
280 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
281 $GLOBALS['messages'][$backupLang] = array();
283 // Both are readable so include current language file
284 $GLOBALS['count'] = false;
285 loadInclude($source);
286 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
287 unset($GLOBALS['count']);
289 // Set target language
290 setCurrentLanguage($targetLanguage);
292 // Do we have an array?
293 if (!isset($GLOBALS['messages'][$targetLanguage])) {
294 // Then create it to avoid notice
295 $GLOBALS['messages'][$targetLanguage] = array();
296 $GLOBALS['msg_count'][$targetLanguage] = 0;
299 // Load target language file
300 loadInclude($target);
301 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
304 setCurrentLanguage($backupLang);
305 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
306 unset($messages[$backupLang]);
309 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
313 // Check all differences
314 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
316 if (!isset($GLOBALS['msgs'][$target][$key])) {
317 // Then add is as difference
318 $GLOBALS['lang_diff'][$target][$key] = $value;
321 $GLOBALS['lang_diff_count'][$target]++;
322 $GLOBALS['lang_diff_count']['total']++;
331 // Getter for getting difference of target file
332 function getLanguageComparisonDifference ($target) {
333 return $GLOBALS['lang_diff_count'][$target];
336 // Checks wether the given message is masked
337 function isMessageMasked ($messageId) {
338 // Is the message id valid?
339 if (!isMessageIdValid($messageId)) {
340 // No, then abort here
341 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
344 // Now simply check it
345 $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);