Open Journal Systems  3.3.0
LocaleFile.inc.php
1 <?php
2 
17 class LocaleFile {
19  var $cache;
20 
22  var $locale;
23 
25  var $filename;
26 
32  function __construct($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  static function &load($filename) {
121  $localeData = array();
122  // This compatibility code for XML file fallback will eventually be removed.
123  // See https://github.com/pkp/pkp-lib/issues/5090.
124  if (file_exists($filename) && substr($filename, -3) == '.po') {
125  // Prefer a PO file, if one exists.
126  $translations = Gettext\Translations::fromPoFile($filename);
127  foreach ($translations as $translation) {
128  $localeData[$translation->getOriginal()] = $translation->getTranslation();
129  }
130  } else {
131  // Try a fallback to an old-style XML locale file.
132  $xmlFilename = preg_replace('/\.po$/', '.xml', $filename);
133  if ($xmlFilename && file_exists($xmlFilename)) {
134  $xmlDao = new XMLDAO();
135  $data = $xmlDao->parseStruct($filename, array('message'));
136 
137  // Build array with ($key => $string)
138  if (isset($data['message'])) {
139  foreach ($data['message'] as $messageData) {
140  $localeData[$messageData['attributes']['key']] = $messageData['value'];
141  }
142  }
143  }
144  }
145  return $localeData;
146  }
147 
153  function isValid() {
154  return isset($this->locale) && file_exists($this->filename);
155  }
156 
163  function testLocale(&$referenceLocaleFile) {
164  $errors = array(
165  LOCALE_ERROR_MISSING_KEY => array(),
166  LOCALE_ERROR_EXTRA_KEY => array(),
167  LOCALE_ERROR_DIFFERING_PARAMS => array(),
168  LOCALE_ERROR_MISSING_FILE => array()
169  );
170 
171  if ($referenceLocaleFile->isValid()) {
172  if (!$this->isValid()) {
173  $errors[LOCALE_ERROR_MISSING_FILE][] = array(
174  'locale' => $this->locale,
175  'filename' => $this->filename
176  );
177  return $errors;
178  }
179  } else {
180  // If the reference file itself does not exist or is invalid then
181  // there's nothing to be translated here.
182  return $errors;
183  }
184 
185  $localeContents = LocaleFile::load($this->filename);
186  $referenceContents = LocaleFile::load($referenceLocaleFile->filename);
187 
188  foreach ($referenceContents as $key => $referenceValue) {
189  if (!isset($localeContents[$key])) {
190  $errors[LOCALE_ERROR_MISSING_KEY][] = array(
191  'key' => $key,
192  'locale' => $this->locale,
193  'filename' => $this->filename,
194  'reference' => $referenceValue
195  );
196  continue;
197  }
198  $value = $localeContents[$key];
199 
200  $referenceParams = AppLocale::getParameterNames($referenceValue);
201  $params = AppLocale::getParameterNames($value);
202  if (count(array_diff($referenceParams, $params)) > 0) {
203  $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array(
204  'key' => $key,
205  'locale' => $this->locale,
206  'mismatch' => array_diff($referenceParams, $params),
207  'filename' => $this->filename,
208  'reference' => $referenceValue,
209  'value' => $value
210  );
211  }
212  // After processing a key, remove it from the list;
213  // this way, the remainder at the end of the loop
214  // will be extra unnecessary keys.
215  unset($localeContents[$key]);
216  }
217 
218  // Leftover keys are extraneous.
219  foreach ($localeContents as $key => $value) {
220  $errors[LOCALE_ERROR_EXTRA_KEY][] = array(
221  'key' => $key,
222  'locale' => $this->locale,
223  'filename' => $this->filename
224  );
225  }
226 
227  return $errors;
228  }
229 }
230 
231 
XMLDAO
Operations for retrieving and modifying objects from an XML data source.
Definition: XMLDAO.inc.php:19
LocaleFile
Abstraction of a locale file.
Definition: LocaleFile.inc.php:17
LocaleFile\isValid
isValid()
Definition: LocaleFile.inc.php:162
LocaleFile\__construct
__construct($locale, $filename)
Definition: LocaleFile.inc.php:41
LocaleFile\$filename
$filename
Definition: LocaleFile.inc.php:34
LocaleFile\testLocale
testLocale(&$referenceLocaleFile)
Definition: LocaleFile.inc.php:172
LocaleFile\$cache
$cache
Definition: LocaleFile.inc.php:22
LocaleFile\_cacheMiss
_cacheMiss($cache, $id)
Definition: LocaleFile.inc.php:73
PKPLocale\getParameterNames
static getParameterNames($source)
Definition: PKPLocale.inc.php:599
CacheManager\getManager
static getManager()
Definition: CacheManager.inc.php:27
Seboettg\Collection\count
count()
Definition: ArrayListTrait.php:253
LocaleFile\translate
translate($key, $params=array(), $locale=null)
Definition: LocaleFile.inc.php:93
LocaleFile\$locale
$locale
Definition: LocaleFile.inc.php:28
LocaleFile\_getCache
_getCache($locale)
Definition: LocaleFile.inc.php:49
LocaleFile\getFilename
getFilename()
Definition: LocaleFile.inc.php:80
LocaleFile\load
static & load($filename)
Definition: LocaleFile.inc.php:129