Open Journal Systems  2.4.8
 All Classes Namespaces Functions Variables Groups Pages
LocaleFile.inc.php
1 <?php
2 
17 class LocaleFile {
19  var $cache;
20 
22  var $locale;
23 
25  var $filename;
26 
32  function LocaleFile($locale, $filename) {
33  $this->locale = $locale;
34  $this->filename = $filename;
35  }
36 
40  function &_getCache($locale) {
41  if (!isset($this->cache)) {
42  $cacheManager =& CacheManager::getManager();
43  $this->cache = $cacheManager->getFileCache(
44  'locale', md5($this->filename),
45  array(&$this, '_cacheMiss')
46  );
47 
48  // Check to see if the cache is outdated.
49  // Only some kinds of caches track cache dates;
50  // if there's no date available (ie cachedate is
51  // null), we have to assume it's up to date.
52  $cacheTime = $this->cache->getCacheTime();
53  if ($cacheTime === null || $cacheTime < filemtime($this->filename)) {
54  // This cache is out of date; flush it.
55  $this->cache->setEntireCache(LocaleFile::load($this->filename));
56  }
57  }
58  return $this->cache;
59  }
60 
64  function _cacheMiss(&$cache, $id) {
65  return null; // It's not in this locale file.
66  }
67 
71  function getFilename() {
72  return $this->filename;
73  }
74 
84  function translate($key, $params = array(), $locale = null) {
85  if ($this->isValid()) {
86  $key = trim($key);
87  if (empty($key)) {
88  return '';
89  }
90 
91  $cache =& $this->_getCache($this->locale);
92  $message = $cache->get($key);
93  if (!isset($message)) {
94  // Try to force loading the plugin locales.
95  $message = $this->_cacheMiss($cache, $key);
96  }
97 
98  if (isset($message)) {
99  if (!empty($params)) {
100  // Substitute custom parameters
101  foreach ($params as $key => $value) {
102  $message = str_replace("{\$$key}", $value, $message);
103  }
104  }
105 
106  // if client encoding is set to iso-8859-1, transcode string from utf8 since we store all XML files in utf8
107  if (LOCALE_ENCODING == "iso-8859-1") $message = utf8_decode($message);
108 
109  return $message;
110  }
111  }
112  return null;
113  }
114 
120  function &load($filename) {
121  $localeData = array();
122 
123  // Reload localization XML file
124  $xmlDao = new XMLDAO();
125  $data = $xmlDao->parseStruct($filename, array('message'));
126 
127  // Build array with ($key => $string)
128  if (isset($data['message'])) {
129  foreach ($data['message'] as $messageData) {
130  $localeData[$messageData['attributes']['key']] = $messageData['value'];
131  }
132  }
133 
134  return $localeData;
135  }
136 
142  function isValid() {
143  return isset($this->locale) && file_exists($this->filename);
144  }
145 
152  function testLocale(&$referenceLocaleFile) {
153  $errors = array(
154  LOCALE_ERROR_MISSING_KEY => array(),
155  LOCALE_ERROR_EXTRA_KEY => array(),
156  LOCALE_ERROR_DIFFERING_PARAMS => array(),
157  LOCALE_ERROR_MISSING_FILE => array()
158  );
159 
160  if ($referenceLocaleFile->isValid()) {
161  if (!$this->isValid()) {
162  $errors[LOCALE_ERROR_MISSING_FILE][] = array(
163  'locale' => $this->locale,
164  'filename' => $this->filename
165  );
166  return $errors;
167  }
168  } else {
169  // If the reference file itself does not exist or is invalid then
170  // there's nothing to be translated here.
171  return $errors;
172  }
173 
174  $localeContents = LocaleFile::load($this->filename);
175  $referenceContents = LocaleFile::load($referenceLocaleFile->filename);
176 
177  foreach ($referenceContents as $key => $referenceValue) {
178  if (!isset($localeContents[$key])) {
179  $errors[LOCALE_ERROR_MISSING_KEY][] = array(
180  'key' => $key,
181  'locale' => $this->locale,
182  'filename' => $this->filename,
183  'reference' => $referenceValue
184  );
185  continue;
186  }
187  $value = $localeContents[$key];
188 
189  $referenceParams = AppLocale::getParameterNames($referenceValue);
190  $params = AppLocale::getParameterNames($value);
191  if (count(array_diff($referenceParams, $params)) > 0) {
192  $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array(
193  'key' => $key,
194  'locale' => $this->locale,
195  'mismatch' => array_diff($referenceParams, $params),
196  'filename' => $this->filename,
197  'reference' => $referenceValue,
198  'value' => $value
199  );
200  }
201  // After processing a key, remove it from the list;
202  // this way, the remainder at the end of the loop
203  // will be extra unnecessary keys.
204  unset($localeContents[$key]);
205  }
206 
207  // Leftover keys are extraneous.
208  foreach ($localeContents as $key => $value) {
209  $errors[LOCALE_ERROR_EXTRA_KEY][] = array(
210  'key' => $key,
211  'locale' => $this->locale,
212  'filename' => $this->filename
213  );
214  }
215 
216  return $errors;
217  }
218 }
219 
220 ?>
testLocale(&$referenceLocaleFile)
translate($key, $params=array(), $locale=null)
LocaleFile($locale, $filename)
Abstraction of a locale file.
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
& _getCache($locale)
& load($filename)
_cacheMiss(&$cache, $id)
getParameterNames($source)