wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.Message.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.Message.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 // require file only if run in wcmf enviroment
20 if (file_exists(BASE."wcmf/lib/util/class.InifileParser.php"))
21  require_once(BASE."wcmf/lib/util/class.InifileParser.php");
22 
23 /**
24  * @class Message
25  * @ingroup Util
26  * @brief Use the Message class to output messages.
27  * You need not instantiate a Message object
28  * because the methods may be called like static
29  * class methods e.g.
30  * $translated = Message::get('text to translate')
31  *
32  * @author ingo herwig <ingo@wemove.com>
33  */
34 class Message
35 {
36  /**
37  * The get() method is used to get a localized string.
38  * This method uses the GNU gettext (see PHP manual),
39  * the localization directory must be given in the global variable $MESSAGE_LOCALE_DIR (configuration value 'localeDir' in section 'cms')
40  * @note The language is determined in one of 3 ways (in this order):
41  * -# use the value of the global variable $MESSAGE_LANGUAGE (configuration value 'language' in section 'cms')
42  * -# use the value of the global variable $_SERVER['HTTP_ACCEPT_LANGUAGE']
43  * -# use the value of the given lang parameter
44  * @param message The message to translate (\%0%, \%1%, ... will be replaced by given parameters).
45  * @param parameters An array of values for parameter substitution in the message.
46  * @param domain The domain to get the text from, optional, default: 'main'.
47  * @param lang The language, optional, default: ''.
48  * @return The localized string
49  * @note It is not recommended to use this method with concatenated strings because this
50  * restricts the positions of words in translations. E.g. 'She was born in %1% on %2%'
51  * translates to the german sentance 'Sie wurde am \%2% in \%1% geboren' with the variables
52  * flipped.
53  * @note since gettext sometimes is not reliable (caching problem), it is possible to
54  * use custom php arrays created from .po files. use wcmf/tools/po2array.php to create
55  * the appropriate messages_$lang.php files in the locale directories. the usage of these arrays
56  * is configurable by the ini file option 'usegettext' in section 'cms' if this option is set
57  * to 0 the method tries to search for the appropriate array definition.
58  */
59  function get ($message, $parameters=null, $domain='', $lang='')
60  {
61  if (!file_exists(BASE."wcmf/lib/util/class.InifileParser.php"))
62  return $message;
63 
64  global $MESSAGE_LANGUAGE;
65  global $MESSAGE_LOCALE_DIR;
66 
67  // select language
68  if ($lang == '')
69  {
70  if ($MESSAGE_LANGUAGE != '')
71  $lang = $MESSAGE_LANGUAGE;
72  else if ($lang == '')
73  $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
74  }
75  if ($domain == '')
76  $domain = 'main';
77 
78  // convert lang to language_COUNTRY format if not already done
79  $lang = preg_replace("/\-/", "_", $lang);
80  $lang = preg_replace("/(\w+)_(\w+)/e", "'\\1_'.strtoupper('\\2')", $lang);
81  // if _COUNTRY is missing, use language as country
82  if (strpos($lang, '_') === false)
83  $lang = $lang.'_'.strtoupper($lang);
84 
85  $parser = &InifileParser::getInstance();
86  if (($useGetText = $parser->getValue('usegettext', 'cms')) === false)
87  $useGetText = 1;
88 
89  if ($useGetText)
90  {
91  // see if gettext is installed
92  if (function_exists(bindtextdomain) && function_exists(textdomain) && function_exists(gettext))
93  {
94  // get localized message
95  putenv("LANGUAGE=".$lang);
96  putenv("LANG=".$lang);
97  putenv("LC_ALL=".$lang);
98  setlocale(LC_ALL, $lang);
99  bindtextdomain($domain, $MESSAGE_LOCALE_DIR);
100  textdomain($domain);
101  $localizedMessage = gettext($message);
102  }
103  else
104  $localizedMessage = $message;
105  }
106  else
107  {
108  // try to use custom array definitions as dictionary
109  $messageFile = $MESSAGE_LOCALE_DIR.$lang."/LC_MESSAGES/messages_".$lang.".php";
110  if (file_exists($messageFile))
111  {
112  require($messageFile); // require_once does not work here !!!
113  if (${"messages_$lang"}[$message] != "")
114  $localizedMessage = ${"messages_$lang"}[$message];
115  else
116  $localizedMessage = $message;
117  }
118  else
119  $localizedMessage = $message;
120  }
121 
122  // replace parameters
123  preg_match_all("/%([0-9]+)%/", $localizedMessage, $matches);
124  $matches = $matches[1];
125  for ($i=0; $i<sizeof($matches);$i++)
126  $matches[$i] = '/\%'.$matches[$i].'\%/';
127  sort($matches);
128  if (sizeof($matches) > 0 && is_array($parameters))
129  $localizedMessage = preg_replace($matches, $parameters, $localizedMessage);
130 
131  return $localizedMessage;
132  }
133  /**
134  * The getAll() method is used to get a localized list of all defined strings.
135  * See Message::get() for more information.
136  * This function only returns results if the ini file option 'usegettext' in section 'cms' is set
137  * to 0.
138  * @param lang The language, optional, default: ''.
139  * @return An array of localized string
140  */
141  function getAll ($lang='')
142  {
143  if (!file_exists(BASE."wcmf/lib/util/class.InifileParser.php"))
144  return array();
145 
146  global $MESSAGE_LANGUAGE;
147  global $MESSAGE_LOCALE_DIR;
148 
149  // select language
150  if ($lang == '')
151  {
152  if ($MESSAGE_LANGUAGE != '')
153  $lang = $MESSAGE_LANGUAGE;
154  else if ($lang == '')
155  $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
156  }
157 
158  // convert lang to language_COUNTRY format if not already done
159  $lang = preg_replace("/\-/", "_", $lang);
160  $lang = preg_replace("/(\w+)_(\w+)/e", "'\\1_'.strtoupper('\\2')", $lang);
161  // if _COUNTRY is missing, use language as country
162  if (strpos($lang, '_') === false)
163  $lang = $lang.'_'.strtoupper($lang);
164 
165  $parser = &InifileParser::getInstance();
166  // try to use custom array definitions as dictionary
167  $messageFile = $MESSAGE_LOCALE_DIR.$lang."/LC_MESSAGES/messages_".$lang.".php";
168  if (file_exists($messageFile))
169  {
170  require($messageFile); // require_once does not work here !!!
171  return ${"messages_$lang"};
172  }
173  return array();
174  }
175 }
176 ?>
Use the Message class to output messages. You need not instantiate a Message object because the metho...
$lang
Definition: message.js.php:12
getAll($lang='')