Open Journal Systems  2.4.4
 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  $key = trim($key);
86  if (empty($key)) {
87  return '';
88  }
89 
90  $cache =& $this->_getCache($this->locale);
91  $message = $cache->get($key);
92  if (!isset($message)) {
93  // Try to force loading the plugin locales.
94  $message = $this->_cacheMiss($cache, $key);
95  }
96 
97  if (isset($message)) {
98  if (!empty($params)) {
99  // Substitute custom parameters
100  foreach ($params as $key => $value) {
101  $message = str_replace("{\$$key}", $value, $message);
102  }
103  }
104 
105  // if client encoding is set to iso-8859-1, transcode string from utf8 since we store all XML files in utf8
106  if (LOCALE_ENCODING == "iso-8859-1") $message = utf8_decode($message);
107 
108  return $message;
109  }
110  return null;
111  }
112 
118  function &load($filename) {
119  $localeData = array();
120 
121  // Reload localization XML file
122  $xmlDao = new XMLDAO();
123  $data = $xmlDao->parseStruct($filename, array('message'));
124 
125  // Build array with ($key => $string)
126  if (isset($data['message'])) {
127  foreach ($data['message'] as $messageData) {
128  $localeData[$messageData['attributes']['key']] = $messageData['value'];
129  }
130  }
131 
132  return $localeData;
133  }
134 
140  function isValid() {
141  return isset($this->locale) && file_exists($this->filename);
142  }
143 
150  function testLocale(&$referenceLocaleFile) {
151  $errors = array(
152  LOCALE_ERROR_MISSING_KEY => array(),
153  LOCALE_ERROR_EXTRA_KEY => array(),
154  LOCALE_ERROR_DIFFERING_PARAMS => array(),
155  LOCALE_ERROR_MISSING_FILE => array()
156  );
157 
158  if ($referenceLocaleFile->isValid()) {
159  if (!$this->isValid()) {
160  $errors[LOCALE_ERROR_MISSING_FILE][] = array(
161  'locale' => $this->locale,
162  'filename' => $this->filename
163  );
164  return $errors;
165  }
166  } else {
167  // If the reference file itself does not exist or is invalid then
168  // there's nothing to be translated here.
169  return $errors;
170  }
171 
172  $localeContents = LocaleFile::load($this->filename);
173  $referenceContents = LocaleFile::load($referenceLocaleFile->filename);
174 
175  foreach ($referenceContents as $key => $referenceValue) {
176  if (!isset($localeContents[$key])) {
177  $errors[LOCALE_ERROR_MISSING_KEY][] = array(
178  'key' => $key,
179  'locale' => $this->locale,
180  'filename' => $this->filename,
181  'reference' => $referenceValue
182  );
183  continue;
184  }
185  $value = $localeContents[$key];
186 
187  $referenceParams = AppLocale::getParameterNames($referenceValue);
188  $params = AppLocale::getParameterNames($value);
189  if (count(array_diff($referenceParams, $params)) > 0) {
190  $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array(
191  'key' => $key,
192  'locale' => $this->locale,
193  'mismatch' => array_diff($referenceParams, $params),
194  'filename' => $this->filename,
195  'reference' => $referenceValue,
196  'value' => $value
197  );
198  }
199  // After processing a key, remove it from the list;
200  // this way, the remainder at the end of the loop
201  // will be extra unnecessary keys.
202  unset($localeContents[$key]);
203  }
204 
205  // Leftover keys are extraneous.
206  foreach ($localeContents as $key => $value) {
207  $errors[LOCALE_ERROR_EXTRA_KEY][] = array(
208  'key' => $key,
209  'locale' => $this->locale,
210  'filename' => $this->filename
211  );
212  }
213 
214  return $errors;
215  }
216 }
217 
218 ?>
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)