Skip to content

Commit

Permalink
cldr : fetch cldr datas when import new language, fix persmissions error
Browse files Browse the repository at this point in the history
  • Loading branch information
kelu95 committed Aug 27, 2015
1 parent f30d7c7 commit f94dc14
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
10 changes: 9 additions & 1 deletion Core/Business/Cldr/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ class Repository
protected $region;
protected $locale;
protected $contextLanguage;
protected $oldUmask;

public function __construct($contextLanguage = null)
{
$this->contextLanguage = $contextLanguage;

$this->cldrCacheFolder = _PS_TRANSLATIONS_DIR_.'cldr';

$this->oldUmask = umask(0000);

if (!is_dir($this->cldrCacheFolder)) {
try {
mkdir($this->cldrCacheFolder.DIRECTORY_SEPARATOR.'datas', 0777, true);

} catch (\Exception $e) {
throw new \Exception('Cldr cache folder can\'t be created');
}
Expand All @@ -77,6 +80,11 @@ public function __construct($contextLanguage = null)
$this->localeRepository = $this->repository->locales[$this->getCulture()];
}

public function __destruct()
{
umask($this->oldUmask);
}

/*
* get the current culture
*/
Expand Down
30 changes: 21 additions & 9 deletions Core/Business/Cldr/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ class Update extends Repository
{
const ZIP_CORE_URL = 'http://www.unicode.org/Public/cldr/26/json-full.zip';

protected $oldUmask;

public function __construct($psCacheDir)
{
$this->oldUmask = umask(0000);
$this->cldrCacheFolder = $psCacheDir.'cldr';

if (!is_dir($this->cldrCacheFolder)) {
Expand All @@ -46,6 +49,11 @@ public function __construct($psCacheDir)
}
}

public function __destruct()
{
umask($this->oldUmask);
}

/*
* Init CLDR datas and download default language
*/
Expand All @@ -71,7 +79,6 @@ public function init()

//extract ONLY supplemental json files
$archive = new \ZipArchive();

if ($archive->open($file) === true) {
for ($i = 0; $i < $archive->numFiles; $i++) {
$filename = $archive->getNameIndex($i);
Expand Down Expand Up @@ -112,6 +119,7 @@ public function fetchLocale($locale)

for ($i = 0; $i < $archive->numFiles; $i++) {
$filename = $archive->getNameIndex($i);

if (preg_match('%^main\/'.$locale.'\/(.*).json$%', $filename)) {
if (!is_dir($this->cldrCacheFolder.DIRECTORY_SEPARATOR.'datas'.DIRECTORY_SEPARATOR.dirname($filename))) {
mkdir($this->cldrCacheFolder.DIRECTORY_SEPARATOR.'datas'.DIRECTORY_SEPARATOR.dirname($filename), 0777, true);
Expand All @@ -138,10 +146,12 @@ private function generateSupplementalDatas()
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$newFileName = 'supplemental--'.pathinfo($file)['filename'];
copy(
$rootPath.'supplemental'.DIRECTORY_SEPARATOR.$file,
$this->cldrCacheFolder.DIRECTORY_SEPARATOR.$newFileName
);
if (!file_exists($this->cldrCacheFolder.DIRECTORY_SEPARATOR.$newFileName)) {
copy(
$rootPath . 'supplemental' . DIRECTORY_SEPARATOR . $file,
$this->cldrCacheFolder . DIRECTORY_SEPARATOR . $newFileName
);
}
}
}
}
Expand All @@ -159,10 +169,12 @@ private function generateMainDatas($locale)
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$newFileName = 'main--'.$locale.'--'.pathinfo($file)['filename'];
copy(
$rootPath.'main'.DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR.$file,
$this->cldrCacheFolder.DIRECTORY_SEPARATOR.$newFileName
);
if (!file_exists($this->cldrCacheFolder . DIRECTORY_SEPARATOR . $newFileName)) {
copy(
$rootPath . 'main' . DIRECTORY_SEPARATOR . $locale . DIRECTORY_SEPARATOR . $file,
$this->cldrCacheFolder . DIRECTORY_SEPARATOR . $newFileName
);
}
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions classes/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,15 @@ public static function getCurrencies($object = false, $active = true, $group_by
$cldr = new Repository(Context::getContext()->language);

foreach($tab as $k => $c){

if($object){
$tab[$k] = Currency::getCurrencyInstance($c['id_currency']);
}
}else{
$currency = $cldr->getCurrency($c['iso_code']);

$currency = $cldr->getCurrency($c['iso_code']);

$tab[$k]['name'] = ucfirst($currency['name']);
$tab[$k]['iso_code_num'] = $currency['iso_code'];
$tab[$k]['sign'] = $currency['symbol'];
$tab[$k]['name'] = ucfirst($currency['name']);
$tab[$k]['iso_code_num'] = $currency['iso_code'];
$tab[$k]['sign'] = $currency['symbol'];
}
}

return $tab;
Expand Down
14 changes: 14 additions & 0 deletions classes/LocalizationPack.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* International Registered Trademark & Property of PrestaShop SA
*/

use PrestaShop\PrestaShop\Core\Business\Cldr\Update;

class LocalizationPackCore
{
public $name;
Expand Down Expand Up @@ -98,6 +100,18 @@ public function loadLocalisationPack($file, $selection, $install_mode = false, $
}
}

//get/update cldr datas for each language
if ($iso_localization_pack) {
foreach ($xml->languages->language as $lang) {
//use this to get correct language code ex : qc become fr
$languageCode = explode('-', Language::getLanguageCodeByIso($lang['iso_code']));
$isoCode = $languageCode[0].'-'.strtoupper($iso_localization_pack);

$cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_);
$cldrUpdate->fetchLocale($isoCode);
}
}

return $res;
}

Expand Down
15 changes: 15 additions & 0 deletions controllers/admin/AdminTranslationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* International Registered Trademark & Property of PrestaShop SA
*/

use PrestaShop\PrestaShop\Core\Business\Cldr\Update;

class AdminTranslationsControllerCore extends AdminController
{
/** Name of theme by default */
Expand Down Expand Up @@ -721,6 +723,7 @@ public function submitImportLang()
$gz = new Archive_Tar($_FILES['file']['tmp_name'], true);
$filename = $_FILES['file']['name'];
$iso_code = str_replace(array('.tar.gz', '.gzip'), '', $filename);
if (Validate::isLangIsoCode($iso_code)) {
$themes_selected = Tools::getValue('theme', array(self::DEFAULT_THEME_NAME));
$files_list = AdminTranslationsController::filterTranslationFiles($gz->listContent());
Expand Down Expand Up @@ -790,6 +793,13 @@ public function submitImportLang()
}
}
}
//fetch cldr datas for the new imported locale
$languageCode = explode('-', Language::getLanguageCodeByIso($iso_code));
$cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_);
$cldrUpdate->fetchLocale($languageCode[0].'-'.strtoupper($languageCode[1]));
$this->redirect(false, (isset($conf) ? $conf : '15'));
}
}
Expand Down Expand Up @@ -886,6 +896,11 @@ public function submitAddLang()
$this->errors[] = sprintf(Tools::displayError('Cannot delete the archive %s.'), $file);
}
//fetch cldr datas for the new imported locale
$languageCode = explode('-', Language::getLanguageCodeByIso($arr_import_lang[0]));
$cldrUpdate = new Update(_PS_TRANSLATIONS_DIR_);
$cldrUpdate->fetchLocale($languageCode[0].'-'.strtoupper($languageCode[1]));
$this->redirect(false, (isset($conf) ? $conf : '15'));
}
} else {
Expand Down

0 comments on commit f94dc14

Please sign in to comment.