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 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
44 // "Getter" for language strings
45 // @TODO Rewrite all language constants to this function.
46 function getMessage ($messageId) {
47 // Default is not found!
48 $return = '!' . $messageId . '!';
50 // Is the language string found?
51 if (isMessageIdValid($messageId)) {
52 // Language array element found in small_letters
53 $return = $GLOBALS['messages'][getCurrentLanguage()][$messageId];
55 // Missing language constant
56 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Missing message string %s detected.", $messageId));
63 // Getter for message string as a mask
64 function getMaskedMessage ($messageId, $data) {
65 return sprintf(getMessage($messageId), $data);
69 function initMessages () {
70 $GLOBALS['messages'][getLanguage()] = array();
74 function addMessages ($messages) {
76 $GLOBALS['messages'][getCurrentLanguage()] = merge_array($GLOBALS['messages'][getCurrentLanguage()], $messages);
78 // Don't count them if we don't want it
79 if (isset($GLOBALS['count'])) return;
82 if (isset($GLOBALS['msg_count'][getCurrentLanguage()])) {
83 $GLOBALS['msg_count'][getCurrentLanguage()] += count($messages);
85 $GLOBALS['msg_count'][getCurrentLanguage()] = count($messages);
89 // Checks wether given message id is valid
90 function isMessageIdValid ($messageId) {
91 return (isset($GLOBALS['messages'][getCurrentLanguage()][$messageId]));
94 // Getter for current language
95 function getCurrentLanguage () {
96 return $GLOBALS['language'];
99 // Setter for current language
100 function setCurrentLanguage ($language) {
101 $GLOBALS['language'] = (string) $language;
104 // "Getter" for language
105 function getLanguage () {
106 // Default is 'de'. DO NOT CHANGE THIS!!!
109 // Set default return value to default language from config
110 if (isConfigEntrySet('DEFAULT_LANG')) $ret = getConfig('DEFAULT_LANG');
112 // Is the variable set
113 if (isGetRequestElementSet('mx_lang')) {
114 // Accept only first 2 chars
115 $ret = substr(getRequestElement('mx_lang'), 0, 2);
116 } elseif (isset($GLOBALS['language'])) {
118 $ret = getCurrentLanguage();
119 } elseif (isSessionVariableSet('mx_lang')) {
120 // Return stored value from cookie
121 $ret = getSession('mx_lang');
123 // Fixes a warning before the session has the mx_lang constant
124 if (empty($ret)) $ret = getConfig('DEFAULT_LANG');
128 setCurrentLanguage($ret);
134 // "Setter" for language
135 function setLanguage ($lang) {
136 // Accept only first 2 chars and still secure them
137 $lang = substr(secureString($lang), 0, 2);
140 setSession('mx_lang', $lang);
143 // Checks wether a language file is there for optional extension
144 function isLanguageIncludeReadable ($ext_name = 'none') {
145 // Do we have array element?
146 if (!isset($GLOBALS['lang_inc'][$ext_name])) {
148 if ($ext_name == 'none') {
150 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
152 // Extension's language file
153 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
156 // Look for file if no extension name is provided
157 $GLOBALS['lang_inc'][$ext_name] = isIncludeReadable($languageInclude);
158 //* DEBUG: */ print __FUNCTION__.':'.$ext_name.'='.$languageInclude.'='.intval(isIncludeReadable($languageInclude)).'<br />';
162 return $GLOBALS['lang_inc'][$ext_name];
165 // Load the current language file or fixes it to 'de'
166 // If ext_name is 'none', load general language support, else load extension's
167 // language file. In installation phase load the install language file.
168 function loadLanguageFile ($ext_name = 'none') {
169 // Try to get language from session
170 $currLanguage = getLanguage();
172 // Set default language if it is not (yet) set
173 if (is_null($currLanguage)) {
174 // Get it from config
175 $currLanguage = getConfig('DEFAULT_LANG');
177 // And save it in session
178 setLanguage($currLanguage);
181 // Do we have the language file NOT?
182 if (!isLanguageIncludeReadable($ext_name)) {
183 // Switch to default (DO NOT CHANGE!!!)
186 // And set it temporarily
187 setConfigEntry('DEFAULT_LANG', 'de');
190 // Is the file there?
191 if (isLanguageIncludeReadable($ext_name)) {
192 // Load language file
193 loadLanguageInclude($ext_name);
194 } elseif ((isDebugModeEnabled()) && (getOutputMode() == '0') && ($ext_name != 'sql_patches') && (substr($ext_name, 0, 10) != 'admintheme')) {
195 // No language file is not so good...
196 logDebugMessage(__FUNCTION__, __LINE__, sprintf("NOTICE: Extension %s has no language file or we cannot read from it. lang=%s, mode=%s",
203 // Check for installation mode
204 if ((isInstallationPhase()) || (!isAdminRegistered())) {
205 // Load language file
206 loadLanguageInclude('install');
210 // Loads the language file
211 function loadLanguageInclude ($ext_name = 'none') {
213 if ($ext_name == 'none') {
215 $languageInclude = sprintf("inc/language/%s.php", getLanguage());
217 // Extension's language file
218 $languageInclude = sprintf("inc/language/%s_%s.php", $ext_name, getLanguage());
221 // Check it before loading
222 if (isLanguageIncludeReadable($ext_name)) {
224 loadIncludeOnce($languageInclude);
227 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Language file %s not found or readable.", $languageInclude));
231 // Getter for an array of valid languages with no except by default
232 function getValidLanguages ($except = '') {
233 // @TODO These are all valid languages, again hard-coded
234 $langs = array('de' => 'de', 'en' => 'en');
236 // Should we keep one out?
237 if (!empty($except)) {
239 unset($langs[$except]);
246 // Compares two language files
247 function ifLanguageFilesCompares ($source, $target, $targetLanguage) {
249 $GLOBALS['lang_diff'][$target] = array();
250 $GLOBALS['lang_diff_count'][$target] = 0;
251 if (!isset($GLOBALS['lang_diff_count']['total'])) $GLOBALS['lang_diff_count']['total'] = 0;
253 // *Does* match by default
256 // Is one not readable?
257 if (!isIncludeReadable($source)) {
258 // Please report this bug!
259 debug_report_bug(__FUNCTION__ . ': Source file ' . $source . ' is not readable.');
260 } elseif (!isIncludeReadable($target)) {
261 // Please report this bug!
262 debug_report_bug(__FUNCTION__ . ': Target file ' . $target . ' is not readable.');
263 } elseif ($targetLanguage == getCurrentLanguage()) {
265 debug_report_bug(__FUNCTION__ . ': Target language ' . $targetLanguage . ' is same as current.');
268 // Backup current messages/language
269 $backupLang = getCurrentLanguage();
270 $messages[$backupLang] = $GLOBALS['messages'][$backupLang];
271 $GLOBALS['messages'][$backupLang] = array();
273 // Both are readable so include current language file
274 $GLOBALS['count'] = false;
275 loadInclude($source);
276 $GLOBALS['msgs'][$source] = $GLOBALS['messages'][$backupLang];
277 unset($GLOBALS['count']);
279 // Set target language
280 setCurrentLanguage($targetLanguage);
282 // Do we have an array?
283 if (!isset($GLOBALS['messages'][$targetLanguage])) {
284 // Then create it to avoid notice
285 $GLOBALS['messages'][$targetLanguage] = array();
286 $GLOBALS['msg_count'][$targetLanguage] = 0;
289 // Load target language file
290 loadInclude($target);
291 $GLOBALS['msgs'][$target] = $GLOBALS['messages'][$targetLanguage];
294 setCurrentLanguage($backupLang);
295 $GLOBALS['messages'][$backupLang] = $messages[$backupLang];
296 unset($messages[$backupLang]);
299 if ((count($GLOBALS['msgs'][$source])) != (count($GLOBALS['msgs'][$target]))) {
303 // Check all differences
304 foreach ($GLOBALS['msgs'][$source] as $key => $value) {
306 if (!isset($GLOBALS['msgs'][$target][$key])) {
307 // Then add is as difference
308 $GLOBALS['lang_diff'][$target][$key] = $value;
311 $GLOBALS['lang_diff_count'][$target]++;
312 $GLOBALS['lang_diff_count']['total']++;
321 // Getter for getting difference of target file
322 function getLanguageComparisonDifference ($target) {
323 return $GLOBALS['lang_diff_count'][$target];