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 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // "Getter" for language strings
46 // @TODO Rewrite all language constants to this function.
47 function getMessage ($messageId) {
48 // Default is not found!
49 $return = '!' . $messageId . '!';
51 // Is the language string found?
52 if (isMessageIdValid($messageId)) {
53 // Language array element found in small_letters
54 $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
56 // Missing language constant
57 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
64 // Getter for message string as a mask
65 function getMaskedMessage ($messageId, $data) {
66 return sprintf(getMessage($messageId), $data);
70 function initMessages () {
71 $GLOBALS['messages'][getLanguage()] = array();
75 function addMessages ($messages) {
77 $GLOBALS['messages'][getCurrentLanguage()] = merge_array($GLOBALS['messages'][getCurrentLanguage()], $messages);
79 // Don't count them if we don't want it
80 if (isset($GLOBALS['count'])) return;
83 if (isset($GLOBALS['msg_count'][getCurrentLanguage()])) {
84 $GLOBALS['msg_count'][getCurrentLanguage()] += count($messages);
86 $GLOBALS['msg_count'][getCurrentLanguage()] = count($messages);
90 // Checks wether given message id is valid
91 function isMessageIdValid ($messageId) {
92 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
95 // Getter for current language
96 function getCurrentLanguage () {
97 return $GLOBALS['language'];
100 // Setter for current language
101 function setCurrentLanguage ($language) {
102 $GLOBALS['language'] = (string) $language;
105 // "Getter" for language
106 function getLanguage () {
107 // Default is 'de'. DO NOT CHANGE THIS!!!
110 // Set default return value to default language from config
111 if (isConfigEntrySet('DEFAULT_LANG')) $ret = getConfig('DEFAULT_LANG');
113 // Is the variable set
114 if (isGetRequestParameterSet('mx_lang')) {
115 // Accept only first 2 chars
116 $ret = substr(getRequestParameter('mx_lang'), 0, 2);
117 } elseif (isset($GLOBALS['language'])) {
119 $ret = getCurrentLanguage();
120 } elseif (isSessionVariableSet('mx_lang')) {
121 // Return stored value from cookie
122 $ret = getSession('mx_lang');
124 // Fixes a warning before the session has the mx_lang constant
125 if (empty($ret)) $ret = getConfig('DEFAULT_LANG');
129 setCurrentLanguage($ret);
135 // "Setter" for language
136 function setLanguage ($lang) {
137 // Accept only first 2 chars and still secure them
138 $lang = substr(secureString($lang), 0, 2);
141 setSession('mx_lang', $lang);
144 // Checks wether a language file is there for optional extension
145 function isLanguageIncludeReadable ($ext_name = 'none') {
146 // Do we have array element?
147 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
149 if ($ext_name == 'none') {
151 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
153 // Extension's language file
154 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
157 // Look for file if no extension name is provided
158 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
159 //* DEBUG: */ print __FUNCTION__.':'.$ext_name.'='.$languageInclude.'='.intval(isIncludeReadable($languageInclude)).'<br />';
163 return $GLOBALS['lang_inc'][$ext_name];
166 // Load the current language file or fixes it to 'de'
167 // If ext_name is 'none', load general language support, else load extension's
168 // language file. In installation phase load the install language file.
169 function loadLanguageFile ($ext_name = 'none') {
170 // Try to get language from session
171 $currLanguage = getLanguage();
173 // Set default language if it is not (yet) set
174 if (is_null($currLanguage)) {
175 // Get it from config
176 $currLanguage = getConfig('DEFAULT_LANG');
178 // And save it in session
179 setLanguage($currLanguage);
182 // Do we have the language file NOT?
183 if (!isLanguageIncludeReadable($ext_name)) {
184 // Switch to default (DO NOT CHANGE!!!)
187 // And set it temporarily
188 setConfigEntry('DEFAULT_LANG', 'de');
191 // Is the file there?
192 if (isLanguageIncludeReadable($ext_name)) {
193 // Load language file
194 loadLanguageInclude($ext_name);
195 } elseif ((isDebugModeEnabled()) && (getOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
196 // No language file is not so good...
197 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
204 // Check for installation mode
205 if ((isInstallationPhase()) || (!isAdminRegistered())) {
206 // Load language file
207 loadLanguageInclude('install');
211 // Loads the language file
212 function loadLanguageInclude ($ext_name = 'none') {
214 if ($ext_name == 'none') {
216 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
218 // Extension's language file
219 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
222 // Check it before loading
223 if (isLanguageIncludeReadable($ext_name)) {
225 loadIncludeOnce($languageInclude);
228 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
232 // Getter for an array of valid languages with no except by default
233 function getValidLanguages ($except = '') {
234 // @TODO These are all valid languages, again hard-coded
235 $langs = array('de' => 'de', 'en' => 'en');
237 // Should we keep one out?
238 if (!empty($except)) {
240 unset($langs[$except]);
247 // Compares two language files
248 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
250 $GLOBALS['lang_diff'][$target] = array();
251 $GLOBALS['lang_diff_count'][$target] = 0;
252 if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
254 // *Does* match by default
257 // Is one not readable?
258 if (!isIncludeReadable($source)) {
259 // Please report this bug!
260 debug_report_bug(__FUNCTION__ . ': Source file ' . $source . ' is not readable.');
261 } elseif (!isIncludeReadable($target)) {
262 // Please report this bug!
263 debug_report_bug(__FUNCTION__ . ': Target file ' . $target . ' is not readable.');
264 } elseif ($targetLanguage == getCurrentLanguage()) {
266 debug_report_bug(__FUNCTION__ . ': Target language ' . $targetLanguage . ' is same as current.');
269 // Backup current messages/language
270 $backupLang = getCurrentLanguage();
271 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
272 $GLOBALS['messages'][$backupLang] = array();
274 // Both are readable so include current language file
275 $GLOBALS['count'] = false;
276 loadInclude($source);
277 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
278 unset($GLOBALS['count']);
280 // Set target language
281 setCurrentLanguage($targetLanguage);
283 // Do we have an array?
284 if (!isset($GLOBALS['messages'][$targetLanguage])) {
285 // Then create it to avoid notice
286 $GLOBALS['messages'][$targetLanguage] = array();
287 $GLOBALS['msg_count'][$targetLanguage] = 0;
290 // Load target language file
291 loadInclude($target);
292 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
295 setCurrentLanguage($backupLang);
296 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
297 unset($messages[$backupLang]);
300 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
304 // Check all differences
305 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
307 if (!isset($GLOBALS['msgs'][$target][$key])) {
308 // Then add is as difference
309 $GLOBALS['lang_diff'][$target][$key] = $value;
312 $GLOBALS['lang_diff_count'][$target]++;
313 $GLOBALS['lang_diff_count']['total']++;
322 // Getter for getting difference of target file
323 function getLanguageComparisonDifference ($target) {
324 return $GLOBALS['lang_diff_count'][$target];
327 // Checks wether the given message is masked
328 function isMessageMasked ($messageId) {
329 // Is the message id valid?
330 if (!isMessageIdValid($messageId)) {
331 // No, then abort here
332 debug_report_bug(__FUNCTION__ . ': Invalid message id ' . $messageId . ' detected.');
335 // Now simply check it
336 $masked = (strpos($GLOBALS['messages'][getCurrentLanguage()][$messageId], '%') !== false);