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, 2010 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 $GLOBALS['messages'][getLanguage()] = array();
77 function addMessages ($messages) {
78 // Cache current language
79 $currentLanguage = getCurrentLanguage();
82 $GLOBALS['messages'][$currentLanguage] = merge_array($GLOBALS['messages'][$currentLanguage], $messages);
84 // Don't count them if we don't want it
85 if (isset($GLOBALS['count'])) {
90 if (isset($GLOBALS['msg_count'][$currentLanguage])) {
91 $GLOBALS['msg_count'][$currentLanguage] += count($messages);
93 $GLOBALS['msg_count'][$currentLanguage] = count($messages);
97 // Checks wether given message id is valid
98 function isMessageIdValid ($messageId) {
99 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
102 // Getter for current language
103 function getCurrentLanguage () {
104 return $GLOBALS['language'];
107 // Setter for current language
108 function setCurrentLanguage ($language) {
109 $GLOBALS['language'] = (string) $language;
112 // "Getter" for language
113 function getLanguage () {
114 // Default is 'de'. DO NOT CHANGE THIS!!!
117 // Set default return value to default language from config
118 if (isConfigEntrySet('DEFAULT_LANG')) $ret = getDefaultLanguage();
120 // Is the variable set
121 if (isGetRequestParameterSet('mx_lang')) {
122 // Accept only first 2 chars
123 $ret = substr(getRequestParameter('mx_lang'), 0, 2);
124 } elseif (isset($GLOBALS['language'])) {
126 $ret = getCurrentLanguage();
127 } elseif (isSessionVariableSet('mx_lang')) {
128 // Return stored value from cookie
129 $ret = getSession('mx_lang');
131 // Fixes a warning before the session has the mx_lang constant
132 if (empty($ret)) $ret = getDefaultLanguage();
136 setCurrentLanguage($ret);
142 // "Setter" for language
143 function setLanguage ($lang) {
144 // Accept only first 2 chars and still secure them
145 $lang = substr(secureString($lang), 0, 2);
148 setSession('mx_lang', $lang);
151 // Checks wether a language file is there for optional extension
152 function isLanguageIncludeReadable ($ext_name = 'none') {
153 // Do we have array element?
154 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
156 if ($ext_name == 'none') {
158 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
160 // Extension's language file
161 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
164 // Look for file if no extension name is provided
165 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
166 //* DEBUG: */ debugOutput(__FUNCTION__.':'.$ext_name.'='.$languageInclude.'='.intval(isIncludeReadable($languageInclude)));
170 return $GLOBALS['lang_inc'][$ext_name];
173 // Load the current language file or fixes it to 'de'
174 // If ext_name is 'none', load general language support, else load extension's
175 // language file. In installation phase load the install language file.
176 function loadLanguageFile ($ext_name = 'none') {
177 // Try to get language from session
178 $currLanguage = getLanguage();
180 // Set default language if it is not (yet) set
181 if (is_null($currLanguage)) {
182 // Get it from config
183 $currLanguage = getDefaultLanguage();
185 // And save it in session
186 setLanguage($currLanguage);
189 // Do we have the language file NOT?
190 if (!isLanguageIncludeReadable($ext_name)) {
191 // Switch to default (DO NOT CHANGE!!!)
194 // And set it temporarily
195 setConfigEntry('DEFAULT_LANG', 'de');
198 // Is the file there?
199 if (isLanguageIncludeReadable($ext_name)) {
200 // Load language file
201 loadLanguageInclude($ext_name);
202 } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
203 // No language file is not so good...
204 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
211 // Check for installation mode
212 if ((isInstallationPhase()) || (!isAdminRegistered())) {
213 // Load language file
214 loadLanguageInclude('install');
218 // Loads the language file
219 function loadLanguageInclude ($ext_name = 'none') {
221 if ($ext_name == 'none') {
223 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
225 // Extension's language file
226 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
229 // Check it before loading
230 if (isLanguageIncludeReadable($ext_name)) {
232 loadIncludeOnce($languageInclude);
235 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
239 // Getter for an array of valid languages with no except by default
240 function getValidLanguages ($except = '') {
241 // @TODO These are all valid languages, again hard-coded
242 $langs = array('de' => 'de', 'en' => 'en');
244 // Should we keep one out?
245 if (!empty($except)) {
247 unset($langs[$except]);
254 // Compares two language files
255 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
257 $GLOBALS['lang_diff'][$target] = array();
258 $GLOBALS['lang_diff_count'][$target] = 0;
259 if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
261 // *Does* match by default
264 // Is one not readable?
265 if (!isIncludeReadable($source)) {
266 // Please report this bug!
267 debug_report_bug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
268 } elseif (!isIncludeReadable($target)) {
269 // Please report this bug!
270 debug_report_bug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
271 } elseif ($targetLanguage == getCurrentLanguage()) {
273 debug_report_bug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
276 // Backup current messages/language
277 $backupLang = getCurrentLanguage();
278 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
279 $GLOBALS['messages'][$backupLang] = array();
281 // Both are readable so include current language file
282 $GLOBALS['count'] = false;
283 loadInclude($source);
284 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
285 unset($GLOBALS['count']);
287 // Set target language
288 setCurrentLanguage($targetLanguage);
290 // Do we have an array?
291 if (!isset($GLOBALS['messages'][$targetLanguage])) {
292 // Then create it to avoid notice
293 $GLOBALS['messages'][$targetLanguage] = array();
294 $GLOBALS['msg_count'][$targetLanguage] = 0;
297 // Load target language file
298 loadInclude($target);
299 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
302 setCurrentLanguage($backupLang);
303 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
304 unset($messages[$backupLang]);
307 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
311 // Check all differences
312 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
314 if (!isset($GLOBALS['msgs'][$target][$key])) {
315 // Then add is as difference
316 $GLOBALS['lang_diff'][$target][$key] = $value;
319 $GLOBALS['lang_diff_count'][$target]++;
320 $GLOBALS['lang_diff_count']['total']++;
329 // Getter for getting difference of target file
330 function getLanguageComparisonDifference ($target) {
331 return $GLOBALS['lang_diff_count'][$target];
334 // Checks wether the given message is masked
335 function isMessageMasked ($messageId) {
336 // Is the message id valid?
337 if (!isMessageIdValid($messageId)) {
338 // No, then abort here
339 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
342 // Now simply check it
343 $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);