/** * Returns name of the field * * @param string $locale - locale * @return string */ public function getName($locale = null, $escape = false) { if (!$locale) { $locale = waSystem::getInstance()->getLocale(); } $name = ''; if (isset($this->name[$locale])) { $name = $this->name[$locale]; } else { if (isset($this->name['en_US'])) { if ($locale == waSystem::getInstance()->getLocale() && wa()->getEnv() == 'backend') { $name = _ws($this->name['en_US']); } else { $name = waLocale::translate('webasyst', $locale, $this->name['en_US']); } } else { $name = reset($this->name); // reset() returns the first value } } return $escape ? htmlspecialchars($name, ENT_QUOTES) : $name; }
/** * If $locale exists, make sure DB is up to date and return true. * If no files found for $locale, return FALSE. * * @param string $locale * @return bool */ protected function updateLocaleDb($locale) { if ($locale == 'en_US') { return TRUE; } if (isset(self::$localeDbUpdated[$locale])) { return self::$localeDbUpdated[$locale]; } $localeFile = waSystem::getInstance('webasyst')->getConfig()->getAppPath("locale/{$locale}/LC_MESSAGES/webasyst.po"); if (!file_exists($localeFile)) { //throw new waException('No such file: '.$localeFile); self::$localeDbUpdated[$locale] = FALSE; return self::$localeDbUpdated[$locale]; } $lastLocateUpdate = filemtime($localeFile); // lock $cache = waSystem::getInstance('webasyst')->getAppCachePath('config/countries_locale_updates.txt'); if (!file_exists($cache)) { touch($cache); } $fd = fopen($cache, 'r+b'); if (!flock($fd, LOCK_EX)) { fclose($fd); return FALSE; } $lastDbUpdate = array(); $file = fread($fd, 99999); // file_get_contents does not wirk with flock(LOCK_EX) on windows; 100Kb is reasonable enough foreach (explode("\n", $file) as $line) { $line = trim($line); if (!$line || FALSE === strpos($line, ':')) { continue; } list($loc, $time) = explode(':', $line, 2); $lastDbUpdate[$loc] = $time; } if (!isset($lastDbUpdate[$locale])) { $lastDbUpdate[$locale] = 0; } if ($lastDbUpdate[$locale] < $lastLocateUpdate) { // load default (en_US) locale, translate names to $locale with gettext and save them back to DB. $sql = "SELECT * FROM wa_country WHERE locale='en_US'"; $values = ''; $loc = $this->escape($locale); foreach ($this->query($sql) as $row) { // waLocale::translate is SLOW if current locale != $locale, but this update doesn't run often $name = $this->escape(waLocale::translate('webasyst', $locale, $row['name'])); $iso3letter = $this->escape($row['iso3letter']); $iso2letter = $this->escape($row['iso2letter']); $isonumeric = $this->escape($row['isonumeric']); $values .= ($values ? ',' : '') . "('{$name}', '{$iso3letter}', '{$iso2letter}', '{$isonumeric}', '{$loc}')"; } if ($values) { $sql = "REPLACE INTO wa_country (name, iso3letter, iso2letter, isonumeric, locale) VALUES " . $values; $this->exec($sql); } // save lastDbUpdate time ftruncate($fd, 0); fseek($fd, 0); $lastDbUpdate[$locale] = time(); foreach ($lastDbUpdate as $loc => $time) { fwrite($fd, "{$loc}:{$time}\n"); } } flock($fd, LOCK_UN); fclose($fd); self::$localeDbUpdated[$locale] = TRUE; return self::$localeDbUpdated[$locale]; }
/** * Returns name of the field * * @param string $locale - locale * @return string */ public function getName($locale = null) { if (!$locale) { $locale = waSystem::getInstance()->getLocale(); } if (isset($this->name[$locale])) { return $this->name[$locale]; } else { if (isset($this->name['en_US'])) { if ($locale = waSystem::getInstance()->getLocale()) { return _ws($this->name['en_US']); } else { return waLocale::translate('webasyst', $locale, $this->name['en_US']); } } else { return reset($this->name); // reset() returns the first value } } }
/** * @param string $locale * @param mixed $iterable anything foreach'able (e.g. an array or db result) containing wa_country rows * @return array iso3letter => wa_country row, sorted by row[name] */ protected function translate($locale, $iterable) { $current_locale = wa()->getLocale(); $result = array(); foreach ($iterable as $row) { if ($locale != 'en_US') { if ($current_locale === $locale) { $row['name'] = _ws($row['name']); } else { // !!! waLocale::translate is SLOW if $current_locale != $locale $row['name'] = waLocale::translate('webasyst', $locale, $row['name']); } } $result[$row['iso3letter']] = $row; } if ($locale != 'en_US') { uasort($result, array($this, 'sortHelper')); } return $result; }