/** * Services that are essential for the loading of the localization * functionality. */ public function register() { if (!$this->app->bound('Concrete\\Core\\Localization\\Translator\\TranslatorAdapterFactoryInterface')) { $this->app->bind('Concrete\\Core\\Localization\\Translator\\TranslatorAdapterFactoryInterface', function ($app, $params) { $config = $app->make('config'); $loaders = $config->get('i18n.adapters.zend.loaders', array()); $loaderRepository = new TranslationLoaderRepository(); foreach ($loaders as $key => $class) { $loader = $app->build($class, array($app)); $loaderRepository->registerTranslationLoader($key, $loader); } $zendFactory = new ZendTranslatorAdapterFactory($loaderRepository); $plainFactory = new PlainTranslatorAdapterFactory(); return new CoreTranslatorAdapterFactory($config, $plainFactory, $zendFactory); }); } $this->app->bindShared('Concrete\\Core\\Localization\\Localization', function ($app) { $loc = new Localization(); $translatorAdapterFactory = $app->make('Concrete\\Core\\Localization\\Translator\\TranslatorAdapterFactoryInterface'); $repository = new TranslatorAdapterRepository($translatorAdapterFactory); $loc->setTranslatorAdapterRepository($repository); $loc->setActiveContext('ui'); return $loc; }); }
/** * Translate text (simple form) with a context. * * @param string $context A context, useful for translators to better understand the meaning of the text to be translated. * @param string $text The text to be translated. * @param mixed ... Unlimited optional number of arguments: if specified they'll be used for printf. * * @return string Returns the translated text. * * @example tc('Recipient', 'To %s') will return translation for 'To %s' (example for Italian 'A %s'). * @example tc('End date', 'To %s') will return translation for 'To %s' (example for Italian 'Fino al %s'). * @example tc('Recipient', 'To %s', 'John') will return translation for 'To %s' (example: 'A %s'), using 'John' for printf (so the final result will be 'A John' for Italian). * @example tc('End date', 'To %s', '01/01/2000') will return translation for 'To %s' (example: 'Fino al %s'), using '01/01/2000' for printf (so the final result will be 'Fino al 01/01/2000' for Italian). */ function tc($context, $text) { $loc = Localization::getInstance(); $adapter = $loc->getActiveTranslatorAdapter(); $args = func_get_args(); switch (count($args)) { case 2: return $adapter->translateContext($context, $text); case 3: return $adapter->translateContext($context, $text, $args[2]); case 4: return $adapter->translateContext($context, $text, $args[2], $args[3]); case 5: return $adapter->translateContext($context, $text, $args[2], $args[3], $args[4]); default: return call_user_func_array(array($adapter, 'translateContext'), $args); } }
/** * Translate text (simple form) with a context. * * @param string $context A context, useful for translators to better understand the meaning of the text to be translated. * @param string $text The text to be translated. * @param mixed ... Unlimited optional number of arguments: if specified they'll be used for printf. * @return string Returns the translated text. * @example tc('Recipient', 'To %s') will return translation for 'To %s' (example for Italian 'A %s'). * @example tc('End date', 'To %s') will return translation for 'To %s' (example for Italian 'Fino al %s'). * @example tc('Recipient', 'To %s', 'John') will return translation for 'To %s' (example: 'A %s'), using 'John' for printf (so the final result will be 'A John' for Italian). * @example tc('End date', 'To %s', '01/01/2000') will return translation for 'To %s' (example: 'Fino al %s'), using '01/01/2000' for printf (so the final result will be 'Fino al 01/01/2000' for Italian). */ function tc($context, $text) { $zt = Localization::getTranslate(); if (is_object($zt)) { $msgid = $context . "" . $text; $msgtxt = $zt->translate($msgid); if ($msgtxt != $msgid) { $text = $msgtxt; } } if (func_num_args() == 2) { return $text; } $arg = array(); for ($i = 2; $i < func_num_args(); $i++) { $arg[] = func_get_arg($i); } return vsprintf($text, $arg); }
<?php use Concrete\Core\Localization\Localization; header('Content-type: text/javascript; charset=UTF-8'); $locale = str_replace('_', '-', Localization::activeLocale()); if ($locale === 'en-US') { echo '// No needs to translate ' . $locale; } else { $language = Localization::activeLanguage(); $alternatives = array($locale); if (strcmp($locale, $language) !== 0) { $alternatives[] = $language; } $content = false; foreach ($alternatives as $alternative) { $path = DIR_BASE_CORE . '/' . DIRNAME_JAVASCRIPT . "/i18n/select2_locale_{$alternative}.js"; if (is_file($path) && is_readable($path)) { $content = @file_get_contents($path); if (is_string($content)) { break; } } } if (is_string($content)) { echo $content; } else { echo '// No select2 translations for ' . implode(', ', $alternatives); } }
public function renderViewContents($scopeItems) { extract($scopeItems); if (!$this->outputContent) { ob_start(); include $this->template; $this->outputContent = ob_get_contents(); ob_end_clean(); } // The translatable texts in the block header/footer need to be printed // out in the system language. $loc = Localization::getInstance(); $loc->pushActiveContext('ui'); if ($this->blockViewHeaderFile) { include $this->blockViewHeaderFile; } $this->controller->registerViewAssets($this->outputContent); $this->onBeforeGetContents(); echo $this->outputContent; $this->onAfterGetContents(); if ($this->blockViewFooterFile) { include $this->blockViewFooterFile; } $loc->popActiveContext(); }
/** * Export all the translations associates to every trees. * * @return Translations */ public static function exportTranslations() { $translations = new Translations(); $loc = Localization::getInstance(); $loc->pushActiveContext('system'); try { $app = Application::getFacadeApplication(); $db = $app->make('database')->connection(); $r = $db->executeQuery('select treeID from Trees order by treeID asc'); while ($row = $r->fetch()) { try { $tree = static::getByID($row['treeID']); } catch (Exception $x) { $tree = null; } if (isset($tree)) { /* @var $tree Tree */ $treeName = $tree->getTreeName(); if (is_string($treeName) && $treeName !== '') { $translations->insert('TreeName', $treeName); } $rootNode = $tree->getRootTreeNodeObject(); /* @var $rootNode TreeNode */ if (isset($rootNode)) { $rootNode->exportTranslations($translations); } } } } catch (Exception $x) { $loc->popActiveContext(); throw $x; } $loc->popActiveContext(); return $translations; }
/** * Run startup and localization events on any installed packages. */ public function setupPackages() { $checkAfterStart = false; $config = $this['config']; foreach ($this->packages as $pkg) { // handle updates if ($config->get('concrete.updates.enable_auto_update_packages')) { $dbPkg = \Package::getByHandle($pkg->getPackageHandle()); $pkgInstalledVersion = $dbPkg->getPackageVersion(); $pkgFileVersion = $pkg->getPackageVersion(); if (version_compare($pkgFileVersion, $pkgInstalledVersion, '>')) { $currentLocale = Localization::activeLocale(); if ($currentLocale != 'en_US') { Localization::changeLocale('en_US'); } $dbPkg->upgradeCoreData(); $dbPkg->upgrade(); if ($currentLocale != 'en_US') { Localization::changeLocale($currentLocale); } } } $pkg->setupPackageLocalization(); if (method_exists($pkg, 'on_start')) { $pkg->on_start(); } if (method_exists($pkg, 'on_after_packages_start')) { $checkAfterStart = true; } } $config->set('app.bootstrap.packages_loaded', true); if ($checkAfterStart) { foreach ($this->packages as $pkg) { if (method_exists($pkg, 'on_after_packages_start')) { $pkg->on_after_packages_start(); } } } }
/** * Initialize localization. */ private function setSystemLocale() { $u = new User(); $lan = $u->getUserLanguageToDisplay(); $loc = Localization::getInstance(); $loc->setContextLocale('ui', $lan); }
/** * @return string Returns the CKEditor language configuration */ protected function getLanguageOption() { $langPath = DIR_BASE . '/' . DIRNAME_PACKAGES . '/community_ckeditor/vendor/ckeditor/lang/'; $useLanguage = 'en'; $language = strtolower(str_replace('_', '-', Localization::activeLocale())); if (file_exists($langPath . $language . '.js')) { $useLanguage = $language; } elseif (file_exists($langPath . strtolower(Localization::activeLanguage()) . '.js')) { $useLanguage = strtolower(Localization::activeLanguage()); } return $useLanguage; }
public static function getJQueryUIJavascript($setResponseHeaders = true) { if ($setResponseHeaders) { static::sendJavascriptHeader(); } $env = Environment::get(); /* @var $env \Concrete\Core\Foundation\Environment */ $alternatives = array(Localization::activeLocale()); if (Localization::activeLocale() !== Localization::activeLanguage()) { $alternatives[] = Localization::activeLanguage(); } $found = null; foreach ($alternatives as $alternative) { $r = $env->getRecord(DIRNAME_JAVASCRIPT . '/i18n/ui.datepicker-' . str_replace('_', '-', $alternative) . '.js'); if (is_file($r->file)) { $found = $r->file; break; } } if (isset($found)) { readfile($found); } else { echo '// No jQueryUI translations for ' . Localization::activeLocale(); } }
/** * Run startup and localization events on any installed packages. */ public function setupPackages() { foreach ($this->packages as $pkg) { // handle updates if (Config::get('concrete.updates.enable_auto_update_packages')) { $pkgInstalledVersion = $pkg->getPackageVersion(); $pkgFileVersion = $pkg->getPackageVersion(); if (version_compare($pkgFileVersion, $pkgInstalledVersion, '>')) { $currentLocale = Localization::activeLocale(); if ($currentLocale != 'en_US') { Localization::changeLocale('en_US'); } $pkg->upgradeCoreData(); $pkg->upgrade(); if ($currentLocale != 'en_US') { Localization::changeLocale($currentLocale); } } } $pkg->setupPackageLocalization(); if (method_exists($pkg, 'on_start')) { $pkg->on_start(); } } Config::set('app.bootstrap.packages_loaded', true); }
<?php use Concrete\Core\Localization\Localization; header('Content-type: text/javascript; charset=' . APP_CHARSET); $jh = Core::make('helper/json'); $locale = Localization::activeLocale(); ?> jQuery.Redactor.opts.langs[<?php echo $jh->encode($locale); ?> ] = { html: <?php echo $jh->encode(t('HTML')); ?> , video: <?php echo $jh->encode(t('Insert Video')); ?> , image: <?php echo $jh->encode(t('Insert Image')); ?> , table: <?php echo $jh->encode(t('Table')); ?> , link: <?php echo $jh->encode(t('Link')); ?> ,
/** * Run startup and localization events on any installed packages. */ public function setupPackages() { $pla = \Concrete\Core\Package\PackageList::get(); $pl = $pla->getPackages(); $cl = ClassLoader::getInstance(); /** @var \Package[] $pl */ foreach ($pl as $p) { $p->registerConfigNamespace(); if ($p->isPackageInstalled()) { $pkg = Package::getClass($p->getPackageHandle()); if (is_object($pkg) && !$pkg instanceof \Concrete\Core\Package\BrokenPackage) { $cl->registerPackage($pkg); // handle updates if (Config::get('concrete.updates.enable_auto_update_packages')) { $pkgInstalledVersion = $p->getPackageVersion(); $pkgFileVersion = $pkg->getPackageVersion(); if (version_compare($pkgFileVersion, $pkgInstalledVersion, '>')) { $currentLocale = Localization::activeLocale(); if ($currentLocale != 'en_US') { Localization::changeLocale('en_US'); } $p->upgradeCoreData(); $p->upgrade(); if ($currentLocale != 'en_US') { Localization::changeLocale($currentLocale); } } } $pkg->setupPackageLocalization(); if (method_exists($pkg, 'on_start')) { $pkg->on_start(); } } } } Config::set('app.bootstrap.packages_loaded', true); }
/** * displays the Area in the page * ex: $a = new Area('Main'); $a->display($c);. * * @param Page $c * @param Block[] $alternateBlockArray optional array of blocks to render instead of default behavior * * @return bool */ public function display($c = false, $alternateBlockArray = null) { if (!$c) { $c = Page::getCurrentPage(); } $v = View::getRequestInstance(); if (!is_object($c) || $c->isError()) { return false; } $this->load($c); $ap = new Permissions($this); if (!$ap->canViewArea()) { return false; } $blocksToDisplay = $alternateBlockArray ? $alternateBlockArray : $this->getAreaBlocksArray(); $u = new User(); // The translatable texts in the area header/footer need to be printed // out in the system language. $loc = Localization::getInstance(); // now, we iterate through these block groups (which are actually arrays of block objects), and display them on the page $loc->pushActiveContext('ui'); if ($this->showControls && $c->isEditMode() && $ap->canViewAreaControls()) { View::element('block_area_header', array('a' => $this)); } else { View::element('block_area_header_view', array('a' => $this)); } $loc->popActiveContext(); foreach ($blocksToDisplay as $b) { $bv = new BlockView($b); $bv->setAreaObject($this); $p = new Permissions($b); if ($p->canViewBlock()) { if (!$c->isEditMode()) { echo $this->enclosingStart; } $bv->render('view'); if (!$c->isEditMode()) { echo $this->enclosingEnd; } } } $loc->pushActiveContext('ui'); if ($this->showControls && $c->isEditMode() && $ap->canViewAreaControls()) { View::element('block_area_footer', array('a' => $this)); } else { View::element('block_area_footer_view', array('a' => $this)); } $loc->popActiveContext(); }
/** * Testing. */ public function on_start() { $this->addHeaderItem('<link href="' . ASSETS_URL_CSS . '/views/install.css" rel="stylesheet" type="text/css" media="all" />'); $this->requireAsset('core/app'); $this->requireAsset('javascript', 'backstretch'); $this->requireAsset('javascript', 'bootstrap/collapse'); if (isset($_POST['locale']) && $_POST['locale']) { $loc = Localization::changeLocale($_POST['locale']); $this->set('locale', $_POST['locale']); } Cache::disableAll(); $this->setRequiredItems(); $this->setOptionalItems(); if ($this->app->isInstalled()) { throw new Exception(t('concrete5 is already installed.')); } if (!isset($_COOKIE['CONCRETE5_INSTALL_TEST'])) { setcookie('CONCRETE5_INSTALL_TEST', '1', 0, DIR_REL . '/'); } $this->set('backgroundFade', 0); $this->set('pageTitle', t('Install concrete5')); }
/** * @return \Concrete\Core\Error\Error */ public function configure() { $error = \Core::make('helper/validation/error'); /* @var $error \Concrete\Core\Error\Error */ try { $val = Core::make('helper/validation/form'); $val->setData($this->post()); $val->addRequired("SITE", t("Please specify your site's name")); $val->addRequiredEmail("uEmail", t('Please specify a valid email address')); $val->addRequired("DB_DATABASE", t('You must specify a valid database name')); $val->addRequired("DB_SERVER", t('You must specify a valid database server')); $password = $_POST['uPassword']; $passwordConfirm = $_POST['uPasswordConfirm']; $uh = Core::make('helper/concrete/user'); $uh->validNewPassword($password, $error); if ($password) { if ($password != $passwordConfirm) { $error->add(t('The two passwords provided do not match.')); } } if (is_object($this->fileWriteErrors)) { $error = $this->fileWriteErrors; } $error = $this->validateDatabase($error); $error = $this->validateSampleContent($error); if ($val->test() && !$error->has()) { // write the config file $vh = Core::make('helper/validation/identifier'); $this->fp = @fopen(DIR_CONFIG_SITE . '/site_install.php', 'w+'); $this->fpu = @fopen(DIR_CONFIG_SITE . '/site_install_user.php', 'w+'); if ($this->fp) { $config = isset($_POST['SITE_CONFIG']) ? (array) $_POST['SITE_CONFIG'] : array(); $config['database'] = array('default-connection' => 'concrete', 'connections' => array('concrete' => array('driver' => 'c5_pdo_mysql', 'server' => $_POST['DB_SERVER'], 'database' => $_POST['DB_DATABASE'], 'username' => $_POST['DB_USERNAME'], 'password' => $_POST['DB_PASSWORD'], 'charset' => 'utf8'))); $renderer = new Renderer($config); fwrite($this->fp, $renderer->render()); fclose($this->fp); chmod(DIR_CONFIG_SITE . '/site_install.php', 0700); } else { throw new Exception(t('Unable to open config/app.php for writing.')); } if ($this->fpu) { $hasher = new PasswordHash(Config::get('concrete.user.password.hash_cost_log2'), Config::get('concrete.user.password.hash_portable')); $configuration = "<?php\n"; $configuration .= "define('INSTALL_USER_EMAIL', '" . $_POST['uEmail'] . "');\n"; $configuration .= "define('INSTALL_USER_PASSWORD_HASH', '" . $hasher->HashPassword($_POST['uPassword']) . "');\n"; $configuration .= "define('INSTALL_STARTING_POINT', '" . $this->post('SAMPLE_CONTENT') . "');\n"; $configuration .= "define('SITE', '" . addslashes($_POST['SITE']) . "');\n"; if (Localization::activeLocale() != '' && Localization::activeLocale() != 'en_US') { $configuration .= "define('SITE_INSTALL_LOCALE', '" . Localization::activeLocale() . "');\n"; } $res = fwrite($this->fpu, $configuration); fclose($this->fpu); chmod(DIR_CONFIG_SITE . '/site_install_user.php', 0700); if (PHP_SAPI != 'cli') { $this->redirect('/'); } } else { throw new Exception(t('Unable to open config/site_user.php for writing.')); } } else { if ($error->has()) { $this->set('error', $error); } else { $error = $val->getError(); $this->set('error', $val->getError()); } } } catch (Exception $ex) { $this->reset(); $this->set('error', $ex); $error->add($ex); } return $error; }
public static function getTranslate() { $loc = Localization::getInstance(); return $loc->getActiveTranslateObject(); }
public static function setupSiteInterfaceLocalization(Page $c = null) { if (!$c) { $c = Page::getCurrentPage(); } $app = Facade::getFacadeApplication(); // don't translate dashboard pages $dh = $app->make('helper/concrete/dashboard'); if ($dh->inDashboard($c)) { return; } $ms = Section::getBySectionOfSite($c); if (!is_object($ms)) { $ms = static::getPreferredSection(); } if (!$ms) { return; } $locale = $ms->getLocale(); if ($locale) { $app->make('session')->set('multilingual_default_locale', $locale); $loc = Localization::getInstance(); $loc->setContextLocale('site', $locale); } }
/** * Run startup and localization events on any installed packages. */ public function setupPackages() { $checkAfterStart = false; $config = $this['config']; $loc = Localization::getInstance(); $entityManager = $this['Doctrine\\ORM\\EntityManager']; $configUpdater = new EntityManagerConfigUpdater($entityManager); foreach ($this->packages as $pkg) { if ($config->get('concrete.updates.enable_auto_update_packages')) { $dbPkg = \Package::getByHandle($pkg->getPackageHandle()); $pkgInstalledVersion = $dbPkg->getPackageVersion(); $pkgFileVersion = $pkg->getPackageVersion(); if (version_compare($pkgFileVersion, $pkgInstalledVersion, '>')) { $loc->pushActiveContext('system'); $dbPkg->upgradeCoreData(); $dbPkg->upgrade(); $loc->popActiveContext(); } } $service = $this->make('Concrete\\Core\\Package\\PackageService'); $service->setupLocalization($pkg); if (method_exists($pkg, 'on_start')) { $pkg->on_start(); } $service->bootPackageEntityManager($pkg); if (method_exists($pkg, 'on_after_packages_start')) { $checkAfterStart = true; } } $config->set('app.bootstrap.packages_loaded', true); // After package initialization, the translations adapters need to be // reinitialized when accessed the next time because new translations // are now available. $loc->removeLoadedTranslatorAdapters(); if ($checkAfterStart) { foreach ($this->packages as $pkg) { if (method_exists($pkg, 'on_after_packages_start')) { $pkg->on_after_packages_start(); } } } }
public function setupLocalization(LocalizablePackageInterface $package, $locale = null, $translate = 'current') { if ($translate === 'current') { $translate = \Localization::getTranslate(); } if (is_object($translate)) { if (!isset($locale) || !strlen($locale)) { $locale = Localization::activeLocale(); } $languageFile = $package->getTranslationFile($locale); if (is_file($languageFile)) { $translate->addTranslationFile('gettext', $languageFile); } } }
public function getActiveTranslateObject() { return parent::getActiveTranslateObject(); }