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) {
64 return sprintf(getMessage($messageId), $data);
68 function initMessages () {
69 $GLOBALS['messages'][getLanguage()] = array();
73 function addMessages ($messages) {
75 $GLOBALS['messages'][getCurrentLanguage()] = merge_array($GLOBALS['messages'][getCurrentLanguage()], $messages);
77 // Don't count them if we don't want it
78 if (isset($GLOBALS['count'])) return;
81 if (isset($GLOBALS['msg_count'][getCurrentLanguage()])) {
82 $GLOBALS['msg_count'][getCurrentLanguage()] += count($messages);
84 $GLOBALS['msg_count'][getCurrentLanguage()] = count($messages);
88 // Checks wether given message id is valid
89 function isMessageIdValid ($messageId) {
90 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
93 // Getter for current language
94 function getCurrentLanguage () {
95 return $GLOBALS['language'];
98 // Setter for current language
99 function setCurrentLanguage ($language) {
100 $GLOBALS['language'] = (string) $language;
103 // "Getter" for language
104 function getLanguage () {
105 // Default is 'de'. DO NOT CHANGE THIS!!!
108 // Set default return value to default language from config
109 if (isConfigEntrySet('DEFAULT_LANG')) $ret = getDefaultLanguage();
111 // Is the variable set
112 if (isGetRequestParameterSet('mx_lang')) {
113 // Accept only first 2 chars
114 $ret = substr(getRequestParameter('mx_lang'), 0, 2);
115 } elseif (isset($GLOBALS['language'])) {
117 $ret = getCurrentLanguage();
118 } elseif (isSessionVariableSet('mx_lang')) {
119 // Return stored value from cookie
120 $ret = getSession('mx_lang');
122 // Fixes a warning before the session has the mx_lang constant
123 if (empty($ret)) $ret = getDefaultLanguage();
127 setCurrentLanguage($ret);
133 // "Setter" for language
134 function setLanguage ($lang) {
135 // Accept only first 2 chars and still secure them
136 $lang = substr(secureString($lang), 0, 2);
139 setSession('mx_lang', $lang);
142 // Checks wether a language file is there for optional extension
143 function isLanguageIncludeReadable ($ext_name = 'none') {
144 // Do we have array element?
145 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
147 if ($ext_name == 'none') {
149 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
151 // Extension's language file
152 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
155 // Look for file if no extension name is provided
156 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
157 //* DEBUG: */ debugOutput(__FUNCTION__.':'.$ext_name.'='.$languageInclude.'='.intval(isIncludeReadable($languageInclude)));
161 return $GLOBALS['lang_inc'][$ext_name];
164 // Load the current language file or fixes it to 'de'
165 // If ext_name is 'none', load general language support, else load extension's
166 // language file. In installation phase load the install language file.
167 function loadLanguageFile ($ext_name = 'none') {
168 // Try to get language from session
169 $currLanguage = getLanguage();
171 // Set default language if it is not (yet) set
172 if (is_null($currLanguage)) {
173 // Get it from config
174 $currLanguage = getDefaultLanguage();
176 // And save it in session
177 setLanguage($currLanguage);
180 // Do we have the language file NOT?
181 if (!isLanguageIncludeReadable($ext_name)) {
182 // Switch to default (DO NOT CHANGE!!!)
185 // And set it temporarily
186 setConfigEntry('DEFAULT_LANG', 'de');
189 // Is the file there?
190 if (isLanguageIncludeReadable($ext_name)) {
191 // Load language file
192 loadLanguageInclude($ext_name);
193 } elseif ((isDebugModeEnabled()) && (isHtmlOutputMode()) && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
194 // No language file is not so good...
195 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
202 // Check for installation mode
203 if ((isInstallationPhase()) || (!isAdminRegistered())) {
204 // Load language file
205 loadLanguageInclude('install');
209 // Loads the language file
210 function loadLanguageInclude ($ext_name = 'none') {
212 if ($ext_name == 'none') {
214 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
216 // Extension's language file
217 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
220 // Check it before loading
221 if (isLanguageIncludeReadable($ext_name)) {
223 loadIncludeOnce($languageInclude);
226 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
230 // Getter for an array of valid languages with no except by default
231 function getValidLanguages ($except = '') {
232 // @TODO These are all valid languages, again hard-coded
233 $langs = array('de' => 'de', 'en' => 'en');
235 // Should we keep one out?
236 if (!empty($except)) {
238 unset($langs[$except]);
245 // Compares two language files
246 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
248 $GLOBALS['lang_diff'][$target] = array();
249 $GLOBALS['lang_diff_count'][$target] = 0;
250 if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
252 // *Does* match by default
255 // Is one not readable?
256 if (!isIncludeReadable($source)) {
257 // Please report this bug!
258 debug_report_bug(__FUNCTION__, __LINE__, 'Source file ' . $source . ' is not readable.');
259 } elseif (!isIncludeReadable($target)) {
260 // Please report this bug!
261 debug_report_bug(__FUNCTION__, __LINE__, 'Target file ' . $target . ' is not readable.');
262 } elseif ($targetLanguage == getCurrentLanguage()) {
264 debug_report_bug(__FUNCTION__, __LINE__, 'Target language ' . $targetLanguage . ' is same as current.');
267 // Backup current messages/language
268 $backupLang = getCurrentLanguage();
269 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
270 $GLOBALS['messages'][$backupLang] = array();
272 // Both are readable so include current language file
273 $GLOBALS['count'] = false;
274 loadInclude($source);
275 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
276 unset($GLOBALS['count']);
278 // Set target language
279 setCurrentLanguage($targetLanguage);
281 // Do we have an array?
282 if (!isset($GLOBALS['messages'][$targetLanguage])) {
283 // Then create it to avoid notice
284 $GLOBALS['messages'][$targetLanguage] = array();
285 $GLOBALS['msg_count'][$targetLanguage] = 0;
288 // Load target language file
289 loadInclude($target);
290 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
293 setCurrentLanguage($backupLang);
294 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
295 unset($messages[$backupLang]);
298 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
302 // Check all differences
303 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
305 if (!isset($GLOBALS['msgs'][$target][$key])) {
306 // Then add is as difference
307 $GLOBALS['lang_diff'][$target][$key] = $value;
310 $GLOBALS['lang_diff_count'][$target]++;
311 $GLOBALS['lang_diff_count']['total']++;
320 // Getter for getting difference of target file
321 function getLanguageComparisonDifference ($target) {
322 return $GLOBALS['lang_diff_count'][$target];
325 // Checks wether the given message is masked
326 function isMessageMasked ($messageId) {
327 // Is the message id valid?
328 if (!isMessageIdValid($messageId)) {
329 // No, then abort here
330 debug_report_bug(__FUNCTION__, __LINE__, 'Invalid message id ' . $messageId . ' detected.');
333 // Now simply check it
334 $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);