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 () {
117 if (!isset($GLOBALS['language'])) {
118 // Default is 'de'. DO NOT CHANGE THIS!!!
121 // Set default return value to default language from config
122 if (isConfigEntrySet('DEFAULT_LANG')) {
123 $ret = getDefaultLanguage();
126 // Is the variable set
127 if (isGetRequestParameterSet('mx_lang')) {
128 // Accept only first 2 chars
129 $ret = substr(getRequestParameter('mx_lang'), 0, 2);
130 } elseif (isset($GLOBALS['language'])) {
132 $ret = getCurrentLanguage();
133 } elseif (isSessionVariableSet('mx_lang')) {
134 // Return stored value from cookie
135 $ret = getSession('mx_lang');
137 // Fixes a warning before the session has the mx_lang constant
139 $ret = getDefaultLanguage();
144 setCurrentLanguage($ret);
147 // Return cached value
148 return getCurrentLanguage();
151 // "Setter" for language
152 function setLanguage ($lang) {
153 // Accept only first 2 chars and still secure them
154 $lang = substr(secureString($lang), 0, 2);
157 setSession('mx_lang', $lang);
160 // Checks wether a language file is there for optional extension
161 function isLanguageIncludeReadable ($ext_name = 'none') {
162 // Do we have array element?
163 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
165 if ($ext_name == 'none') {
167 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
169 // Extension's language file
170 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
173 // Look for file if no extension name is provided
174 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
175 //* DEBUG: */ debugOutput(__FUNCTION__ . ':' . $ext_name . '=' . $languageInclude . '=' . intval(isIncludeReadable($languageInclude)));
179 return $GLOBALS['lang_inc'][$ext_name];
182 // Load the current language file or fixes it to 'de'
183 // If ext_name is 'none', load general language support, else load extension's
184 // language file. In installation phase load the install language file.
185 function loadLanguageFile ($ext_name = 'none') {
186 // Try to get language from session
187 $currLanguage = getLanguage();
189 // Set default language if it is not (yet) set
190 if (is_null($currLanguage)) {
191 // Get it from config
192 $currLanguage = getDefaultLanguage();
194 // And save it in session
195 setLanguage($currLanguage);
198 // Do we have the language file NOT?
199 if (!isLanguageIncludeReadable($ext_name)) {
200 // Switch to default (DO NOT CHANGE!!!)
203 // And set it temporarily
204 setConfigEntry('DEFAULT_LANG', 'de');
207 // Is the file there?
208 if (isLanguageIncludeReadable($ext_name)) {
209 // Load language file
210 loadLanguageInclude($ext_name);
211 } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
212 // No language file is not so good...
213 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
220 // Check for installation mode
221 if ((isInstallationPhase()) || (!isAdminRegistered())) {
222 // Load language file
223 loadLanguageInclude('install');
227 // Loads the language file
228 function loadLanguageInclude ($ext_name = 'none') {
230 if ($ext_name == 'none') {
232 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
234 // Extension's language file
235 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
238 // Check it before loading
239 if (isLanguageIncludeReadable($ext_name)) {
241 loadIncludeOnce($languageInclude);
244 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
248 // Getter for an array of valid languages with no except by default
249 function getValidLanguages ($except = '') {
250 // @TODO These are all valid languages, again hard-coded
251 $langs = array('de' => 'de', 'en' => 'en');
253 // Should we keep one out?
254 if (!empty($except)) {
256 unset($langs[$except]);
263 // Compares two language files
264 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
266 $GLOBALS['lang_diff'][$target] = array();
267 $GLOBALS['lang_diff_count'][$target] = 0;
268 if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
270 // *Does* match by default
273 // Is one not readable?
274 if (!isIncludeReadable($source)) {
275 // Please report this bug!
276 debug_report_bug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
277 } elseif (!isIncludeReadable($target)) {
278 // Please report this bug!
279 debug_report_bug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
280 } elseif ($targetLanguage == getCurrentLanguage()) {
282 debug_report_bug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
285 // Backup current messages/language
286 $backupLang = getCurrentLanguage();
287 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
288 $GLOBALS['messages'][$backupLang] = array();
290 // Both are readable so include current language file
291 $GLOBALS['count'] = false;
292 loadInclude($source);
293 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
294 unset($GLOBALS['count']);
296 // Set target language
297 setCurrentLanguage($targetLanguage);
299 // Do we have an array?
300 if (!isset($GLOBALS['messages'][$targetLanguage])) {
301 // Then create it to avoid notice
302 $GLOBALS['messages'][$targetLanguage] = array();
303 $GLOBALS['msg_count'][$targetLanguage] = 0;
306 // Load target language file
307 loadInclude($target);
308 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
311 setCurrentLanguage($backupLang);
312 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
313 unset($messages[$backupLang]);
316 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
320 // Check all differences
321 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
323 if (!isset($GLOBALS['msgs'][$target][$key])) {
324 // Then add is as difference
325 $GLOBALS['lang_diff'][$target][$key] = $value;
328 $GLOBALS['lang_diff_count'][$target]++;
329 $GLOBALS['lang_diff_count']['total']++;
338 // Getter for getting difference of target file
339 function getLanguageComparisonDifference ($target) {
340 return $GLOBALS['lang_diff_count'][$target];
343 // Checks wether the given message is masked
344 function isMessageMasked ($messageId) {
345 // Is the message id valid?
346 if (!isMessageIdValid($messageId)) {
347 // No, then abort here
348 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
351 // Now simply check it
352 $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);