/** * main * */ public function main() { $schemaPo = APP . 'Locale' . DS . 'schema.pot'; $conn = ConnectionManager::get('default'); $collection = $conn->schemaCollection(); $translations = new Translations(); $tables = $collection->listTables(); foreach ($tables as $table) { $translations->insert($table, Inflector::humanize(Inflector::underscore($table))); $translations->insert($table, Inflector::humanize(Inflector::underscore(Inflector::singularize($table)))); $columns = $collection->describe($table)->columns(); foreach ($columns as $column) { $c = $collection->describe($table)->column($column); $comment = $c['comment']; $t = new Translation($table . '.' . $column, Inflector::humanize(Inflector::underscore($column))); $translations[] = $t; $t->setTranslation($comment); $t = new Translation($table . '.' . $column, Inflector::humanize(Inflector::underscore(Inflector::singularize($table))) . ' ' . Inflector::humanize(Inflector::underscore($column))); $translations[] = $t; $t->setTranslation($comment); } } $poString = $translations->toPoString(); $caked = preg_replace('/msgctxt "([^"]+)"/i', '#: \\1', $poString); $this->createFile($schemaPo, $caked); }
public static function process(\Twig_Node $node, Translations $translations, $file) { $fileReference = str_replace(realpath(self::$rootDir . '/../') . '/', "", $file); if ($node instanceof TransNode) { //Process nodes that {% trans %} blocks $body = new \Twig_Node_Expression_Constant($node->getNode('body')->getAttribute('data'), $node->getLine()); $compiledTranslation = eval('return ' . self::$twig->compile($body) . ';'); $translations->insert('', $compiledTranslation)->addReference($fileReference, $node->getLine()); } if ($node instanceof \Twig_Node_Expression_Function) { //Process nodes that are function expressions if ($node->getAttribute('name') == '__') { //Check the function name for __() foreach ($node->getNode('arguments') as $argument) { //Grab the argument $key = eval('return ' . self::$twig->compile($argument) . ';'); $translations->insert('', $key)->addReference($fileReference, $node->getLine()); break; //I only needed the first argument in my implementation } } } //Recursively loop through the AST foreach ($node as $child) { if ($child instanceof \Twig_Node) { self::process($child, $translations, $file); } } }
/** * Search for specific functions and create translations. * * @param array $functions The gettext functions to search * @param Translations $translations The translations instance where save the values * @param string $file The filename used to the reference */ public function saveGettextFunctions(array $functions, Translations $translations, $file = '') { foreach ($this->getFunctions() as $function) { list($name, $line, $args) = $function; if (!isset($functions[$name])) { continue; } $translation = null; switch ($functions[$name]) { case '__': if (!isset($args[0])) { continue 2; } $original = $args[0]; if ($original !== '') { $translation = $translations->insert('', $original); } break; case 'n__': if (!isset($args[1])) { continue 2; } $original = $args[0]; $plural = $args[1]; if ($original !== '') { $translation = $translations->insert('', $original, $plural); } break; case 'p__': if (!isset($args[1])) { continue 2; } $context = $args[0]; $original = $args[1]; if ($original !== '') { $translation = $translations->insert($context, $original); } break; default: throw new Exception('Not valid functions'); } if (isset($translation)) { $translation->addReference($file, $line); if (isset($function[3])) { foreach ($function[3] as $extractedComment) { $translation->addExtractedComment($extractedComment); } } } } }
/** * return \GetText\Translations $translations; */ public function extractTranslatableSiteStrings() { $translations = new Translations(); $translations->insert('SiteName', Config::get('concrete.site')); $phpParser = new \C5TL\Parser\Php(); $blockTemplatesParser = new \C5TL\Parser\BlockTemplates(); $themesPresetsParser = new \C5TL\Parser\ThemePresets(); $processApplication = array(DIRNAME_BLOCKS => array($phpParser, $blockTemplatesParser), DIRNAME_ELEMENTS => array($phpParser), DIRNAME_CONTROLLERS => array($phpParser), DIRNAME_MAIL_TEMPLATES => array($phpParser), DIRNAME_PAGE_TYPES => array($phpParser), DIRNAME_PAGES => array($phpParser), DIRNAME_THEMES => array($phpParser, $themesPresetsParser, $blockTemplatesParser), DIRNAME_VIEWS => array($phpParser)); foreach ($processApplication as $dirname => $parsers) { if (is_dir(DIR_APPLICATION . '/' . $dirname)) { foreach ($parsers as $parser) { /* @var $parser \C5TL\Parser */ $fullDirname = DIR_APPLICATION . '/' . $dirname; if (is_dir($fullDirname)) { $parser->parseDirectory($fullDirname, DIRNAME_APPLICATION . '/' . $dirname, $translations); } } } } if (is_dir(DIR_PACKAGES)) { $packages = Package::getInstalledList(); foreach ($packages as $package) { $fullDirname = DIR_PACKAGES . '/' . $package->getPackageHandle(); $phpParser->parseDirectory($fullDirname, DIRNAME_PACKAGES . '/' . $dirname, $translations); } } // Now, we grab dynamic content that's part of our site that we translate dynamically $dynamicTranslations = $this->getDynamicTranslations(); $translations->mergeWith($dynamicTranslations); return $translations; }
/** * @deprecated */ public function exportTranslations() { $translations = new Translations(); foreach ($this->getList() as $type) { $translations->insert('AttributeTypeName', $type->getAttributeTypeName()); } return $translations; }
/** * @deprecated */ public function exportTranslations() { $translations = new Translations(); $sets = $this->entityManager->getRepository('\\Concrete\\Core\\Entity\\Attribute\\Set')->findAll(); foreach ($sets as $set) { $translations->insert('AttributeSet', $set->getAttributeSetName()); } return $translations; }
/** * @deprecated */ public function exportTranslations() { $translations = new Translations(); $keys = $this->entityManager->getRepository('\\Concrete\\Core\\Entity\\Attribute\\Key\\Key')->findAll(); foreach ($keys as $key) { $translations->insert('AttributeKeyName', $key->getAttributeKeyName()); } return $translations; }
/** * @deprecated */ public function exportTranslations() { $translations = new Translations(); $list = $this->getList(); $akcNameMap = array('collection' => 'Page attributes', 'user' => 'User attributes', 'file' => 'File attributes'); foreach ($list as $category) { $akcHandle = $category->getAttributeKeyCategoryHandle(); $translations->insert('AttributeKeyCategory', isset($akcNameMap[$akcHandle]) ? $akcNameMap[$akcHandle] : ucwords(str_replace(array('_', '-', '/'), ' ', $akcHandle))); } return $translations; }
/** * {@inheritdoc} * * @see \C5TL\Parser::parseDirectoryDo() */ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDirectory, $relativePath, $subParsersFilter, $exclude3rdParty) { $themesPresets = array(); $prefix = $relativePath === '' ? '' : "{$relativePath}/"; $matches = null; foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) { $presetsAbsDirectory = $child === '' ? $rootDirectory : "{$rootDirectory}/{$child}"; if (preg_match('%(?:^|/)themes/\\w+/css/presets$%', $presetsAbsDirectory, $matches)) { $dirList = @scandir($presetsAbsDirectory); if ($dirList === false) { throw new \Exception("Unable to parse directory {$presetsAbsDirectory}"); } $shownChild = $child === '' ? rtrim($prefix, '/') : $prefix . $child; foreach ($dirList as $file) { if ($file[0] !== '.' && preg_match('/[^.].*\\.less$/i', $file)) { $fileAbs = "{$presetsAbsDirectory}/{$file}"; if (is_file($fileAbs)) { $content = @file_get_contents($fileAbs); if ($content === false) { throw new \Exception("Error reading file '{$fileAbs}'"); } $content = str_replace("\r", "\n", str_replace("\r\n", "\n", $content)); // Strip multiline comments $content = preg_replace_callback('|/\\*.*?\\*/|s', function ($matches) { return str_repeat("\n", substr_count($matches[0], "\n")); }, $content); foreach (array("'", '"') as $quote) { if (preg_match('%(?:^|\\n|;)[ \\t]*@preset-name:\\s*' . $quote . '([^' . $quote . ']*)' . $quote . '\\s*(?:;|$)%s', $content, $matches)) { $presetName = $matches[1]; $presetLine = null; $p = strpos($content, $matches[0]); if ($p !== false) { $presetLine = substr_count(substr($content, 0, $p), "\n") + 1; } if (!isset($themesPresets[$presetName])) { $themesPresets[$presetName] = array(); } $themesPresets[$presetName][] = array($shownChild . "/{$file}", $presetLine); break; } } } } } } } foreach ($themesPresets as $themesPreset => $references) { $translation = $translations->insert('PresetName', ucwords(str_replace(array('_', '-', '/'), ' ', $themesPreset))); foreach ($references as $reference) { $translation->addReference($reference[0], $reference[1]); } } }
/** * {@inheritDoc} */ public static function fromString($string, Translations $translations = null, $file = '') { if ($translations === null) { $translations = new Translations(); } if ($entries = json_decode($string, true)) { foreach ($entries as $original => $translation) { $translations->insert(null, $original)->setTranslation($translation); } } return $translations; }
/** * Extract and insert a new translation. * * @param Translations $translations * @param string $key * @param string $message */ protected static function insertTranslation(Translations $translations, $key, $message) { $context_glue = '\\u0004'; $key = explode($context_glue, $key); $context = isset($key[1]) ? array_shift($key) : ''; $original = array_shift($key); $translation = array_shift($message); $plural_translation = array_shift($message); $entry = $translations->insert($context, $original); $entry->setTranslation($translation); $entry->setPluralTranslation($plural_translation); }
public static function exportTranslations() { $translations = new Translations(); $em = \Database::connection()->getEntityManager(); $options = $em->getRepository(SelectValueOption::class)->findAll(); /** * @var $option SelectValueOption */ foreach ($options as $option) { $translations->insert('SelectAttributeValue', $option->getSelectAttributeOptionValue()); } return $translations; }
/** * Parse the file type names. * * @param \Gettext\Translations $translations * @param string $rootDirectory * @param string $prefix * @param string[] $directoryAlternatives */ private function parseFileTypes(\Gettext\Translations $translations, $rootDirectory, $prefix, $directoryAlternatives) { foreach ($directoryAlternatives as $subDir) { $rel = $subDir === '' ? 'app.php' : "{$subDir}/app.php"; $fileAbs = $rootDirectory . '/' . $rel; if (!is_file($fileAbs)) { continue; } $fileRel = $prefix . $rel; $configFile = new \C5TL\Util\ConfigFile($fileAbs); $config = $configFile->getArray(); if (isset($config['file_types']) && is_array($config['file_types'])) { $fileTypes = $config['file_types']; foreach (array_keys($fileTypes) as $fileType) { $translation = $translations->insert('', $fileType); $translation->addReference($fileRel); } } } }
/** * Handle an array of translations and append to the Translations instance * * @param array $content * @param Translations $translations */ public static function handleArray(array $content, Translations $translations) { $content = $content['messages']; $translations_info = isset($content['']) ? $content[''] : null; unset($content['']); if (isset($translations_info['domain'])) { $translations->setDomain($translations_info['domain']); } $context_glue = '\\u0004'; foreach ($content as $key => $message) { $key = explode($context_glue, $key); $context = isset($key[1]) ? array_shift($key) : ''; $original = array_shift($key); $plural = array_shift($message); $translation = array_shift($message); $plural_translation = array_shift($message); $entry = $translations->insert($context, $original, $plural); $entry->setTranslation($translation); $entry->setPluralTranslation($plural_translation); } }
/** * Handle an array of translations and append to the Translations instance. * * @param array $content * @param Translations $translations */ public static function extract(array $content, Translations $translations) { $messages = current($content); $headers = isset($messages['']) ? $messages[''] : null; unset($messages['']); if (!empty($headers['domain'])) { $translations->setDomain($headers['domain']); } if (!empty($headers['lang'])) { $translations->setLanguage($headers['lang']); } if (!empty($headers['plural-forms'])) { $translations->setHeader(Translations::HEADER_PLURAL, $headers['plural-forms']); } $context_glue = '\\u0004'; foreach ($messages as $key => $translation) { $key = explode($context_glue, $key); $context = isset($key[1]) ? array_shift($key) : ''; $translations->insert($context, array_shift($key))->setTranslation(array_shift($translation))->setPluralTranslations($translation); } }
/** * {@inheritdoc} * * @see \C5TL\Parser::parseDirectoryDo() */ protected function parseDirectoryDo(\Gettext\Translations $translations, $rootDirectory, $relativePath, $subParsersFilter, $exclude3rdParty) { $templateHandles = array(); $prefix = $relativePath === '' ? '' : "{$relativePath}/"; $matches = null; foreach (array_merge(array(''), $this->getDirectoryStructure($rootDirectory, $exclude3rdParty)) as $child) { $shownChild = $child === '' ? rtrim($prefix, '/') : $prefix . $child; $fullpath = $child === '' ? $rootDirectory : "{$rootDirectory}/{$child}"; if (preg_match('%(?:^|/)blocks/\\w+/(?:templates|composer)/(\\w+)$%', $fullpath, $matches)) { if (!isset($templateHandles[$matches[1]])) { $templateHandles[$matches[1]] = array(); } $templateHandles[$matches[1]][] = $shownChild; } elseif (preg_match('%(^|/)blocks/\\w+/(?:templates|composer)$%', $fullpath)) { $contents = @scandir($fullpath); if ($contents === false) { throw new \Exception("Unable to parse directory {$fullpath}"); } foreach ($contents as $file) { if ($file[0] !== '.') { if (preg_match('/^(.*)\\.php$/', $file, $matches) && is_file("{$fullpath}/{$file}")) { if (!isset($templateHandles[$matches[1]])) { $templateHandles[$matches[1]] = array(); } $templateHandles[$matches[1]][] = $shownChild . "/{$file}"; } } } } } foreach ($templateHandles as $templateHandle => $references) { $translation = $translations->insert('TemplateFileName', ucwords(str_replace(array('_', '-', '/'), ' ', $templateHandle))); foreach ($references as $reference) { $translation->addReference($reference); } } }
/** * return \GetText\Translations $translations;. */ public function extractTranslatableSiteStrings() { $app = Application::getFacadeApplication(); $translations = new Translations(); $translations->insert('SiteName', $app->make('site')->getSite()->getSiteName()); $fh = $app->make('helper/file'); C5TLOptions::setTemporaryDirectory($fh->getTemporaryDirectory()); $phpParser = new C5TLParserPhp(); $blockTemplatesParser = new C5TLParserBlockTemplates(); $themesPresetsParser = new C5TLParserThemePresets(); $configFilesParser = new C5TLParserConfigFiles(); $configFilesParser->parseDirectory(DIR_BASE, ''); $processApplication = [DIRNAME_BLOCKS => [$phpParser, $blockTemplatesParser], DIRNAME_ELEMENTS => [$phpParser], DIRNAME_CONTROLLERS => [$phpParser], DIRNAME_MAIL_TEMPLATES => [$phpParser], DIRNAME_PAGE_TYPES => [$phpParser], DIRNAME_PAGES => [$phpParser], DIRNAME_THEMES => [$phpParser, $themesPresetsParser, $blockTemplatesParser], DIRNAME_VIEWS => [$phpParser]]; foreach ($processApplication as $dirname => $parsers) { if (is_dir(DIR_APPLICATION . '/' . $dirname)) { foreach ($parsers as $parser) { /* @var $parser \C5TL\Parser */ $fullDirname = DIR_APPLICATION . '/' . $dirname; if (is_dir($fullDirname)) { $parser->parseDirectory($fullDirname, DIRNAME_APPLICATION . '/' . $dirname, $translations); } } } } if (is_dir(DIR_PACKAGES)) { $packages = Package::getInstalledList(); foreach ($packages as $package) { $fullDirname = DIR_PACKAGES . '/' . $package->getPackageHandle(); $phpParser->parseDirectory($fullDirname, DIRNAME_PACKAGES . '/' . $dirname, $translations); } } // Now, we grab dynamic content that's part of our site that we translate dynamically $dynamicTranslations = $this->getDynamicTranslations(); $translations->mergeWith($dynamicTranslations); return $translations; }
/** * 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; }
public static function exportTranslations() { $translations = new Translations(); $db = \Database::get(); $r = $db->Execute('select ID from atSelectOptions order by ID asc'); while ($row = $r->FetchRow()) { $opt = static::getByID($row['ID']); $translations->insert('SelectAttributeValue', $opt->getSelectAttributeOptionValue()); } return $translations; }
public static function exportTranslations() { $translations = new Translations(); $db = \Database::get(); $r = $db->Execute('select akID from AttributeKeys order by akID asc'); while ($row = $r->FetchRow()) { $key = static::getInstanceByID($row['akID']); $translations->insert('AttributeKeyName', $key->getAttributeKeyName()); } return $translations; }
public static function exportTranslations() { $translations = new Translations(); $sets = static::getList(); foreach ($sets as $set) { $translations->insert('GroupSetName', $set->getGroupSetName()); } return $translations; }
public static function exportTranslations() { $translations = new Translations(); $attribs = static::getList(); foreach ($attribs as $type) { $translations->insert('PermissionAccessEntityTypeName', $type->getAccessEntityTypeName()); } return $translations; }
/** * Adds a translation to the \Gettext\Translations object. * * @param \Gettext\Translations $translations * @param string $string * @param string $context */ protected final function addTranslation(\Gettext\Translations $translations, $string, $context = '') { if (is_string($string) && $string !== '') { $translations->insert($context, $string); } }
public static function exportTranslations() { $translations = new Translations(); $gl = new GroupList(); $gl->includeAllGroups(); $results = $gl->getResults(); foreach ($results as $group) { $translations->insert('GroupName', $group->getGroupName()); if ($group->getGroupDescription()) { $translations->insert('GroupDescription', $group->getGroupDescription()); } } return $translations; }
/** * Parse a node value and create a POEntry item if it has a value. * * @param \Gettext\Translations $translations Will be populated with found entries. * @param string $filenameRel The relative file name of the xml file being read. * @param \DOMNode $node The current node. * @param string $context='' The translation context. */ private static function parseXmlNodeValue(\Gettext\Translations $translations, $filenameRel, \DOMNode $node, $context = '') { $value = (string) $node->nodeValue; if ($value !== '') { $translation = $translations->insert($context, $value); $translation->addReference($filenameRel, $node->getLineNo()); } }
/** * Attempts to filter custom Wordpress text function from the * different class and template files. * @param array $functions * @param Translations $translations * @param string $file * @return null */ public function saveWPGettextFunctions(array $functions, Translations $translations, $file = '') { foreach ($this->getFunctions() as $function) { list($name, $line, $args) = $function; if (!isset($functions[$name])) { continue; } $translation = null; switch ($functions[$name]) { case '__': if (!isset($args[0])) { continue 2; } $original = $args[0]; if ($original !== '') { $translation = $translations->insert('', $original); } break; case 'wp_n__x_n': if (!isset($args[1])) { continue 2; } $original = $args[0]; $plural = $args[1]; $context = $args[3]; if ($original !== '') { $translation = $translations->insert($context, $original, $plural); } break; case 'wp_n__x': if (!isset($args[1])) { continue 2; } $original = $args[0]; $plural = $args[1]; $context = $args[2]; if ($original !== '') { $translation = $translations->insert($context, $original, $plural); } break; // Works the same as p__ but the arguments // are reversed. // Works the same as p__ but the arguments // are reversed. case 'wp_p__': if (!isset($args[1])) { continue 2; } $original = $args[0]; $context = $args[1]; if ($original !== '') { $translation = $translations->insert($context, $original); } break; default: throw new Exception('Not valid functions ' . $functions[$name]); } if (isset($translation)) { $translation->addReference($file, $line); } } }
/** * @param Translations $translations * * @internal */ public function exportTranslations(Translations $translations) { $name = $this->getTreeNodeDisplayName('text'); if (is_string($name) && $name !== '') { $context = method_exists($this, 'getTreeNodeTranslationContext') ? $this->getTreeNodeTranslationContext() : ''; $translations->insert($context, $name); } $this->populateDirectChildrenOnly(); foreach ($this->getChildNodes() as $childnode) { $childnode->exportTranslations($translations); } }
/** * {@inheritDoc} */ public static function fromString($string, Translations $translations = null, $file = '') { if ($translations === null) { $translations = new Translations(); } $stream = new StringReader($string); $magic = self::readInt($stream, 'V'); if ($magic === self::MAGIC1 || $magic === self::MAGIC3) { //to make sure it works for 64-bit platforms $byteOrder = 'V'; //low endian } elseif ($magic === (self::MAGIC2 & 0xffffffff)) { $byteOrder = 'N'; //big endian } else { throw new \Exception('Not MO file'); } self::readInt($stream, $byteOrder); $total = self::readInt($stream, $byteOrder); //total string count $originals = self::readInt($stream, $byteOrder); //offset of original table $tran = self::readInt($stream, $byteOrder); //offset of translation table $stream->seekto($originals); $table_originals = self::readIntArray($stream, $byteOrder, $total * 2); $stream->seekto($tran); $table_translations = self::readIntArray($stream, $byteOrder, $total * 2); for ($i = 0; $i < $total; $i++) { $stream->seekto($table_originals[$i * 2 + 2]); $original = $stream->read($table_originals[$i * 2 + 1]); $stream->seekto($table_translations[$i * 2 + 2]); $translated = $stream->read($table_translations[$i * 2 + 1]); if ($original === '') { // Headers foreach (explode("\n", $translated) as $headerLine) { if ($headerLine !== '') { $headerChunks = preg_split('/:\\s*/', $headerLine, 2); $translations->setHeader($headerChunks[0], isset($headerChunks[1]) ? $headerChunks[1] : ''); } } } else { $chunks = explode("", $original, 2); if (isset($chunks[1])) { $context = $chunks[0]; $original = $chunks[1]; } else { $context = ''; } $chunks = explode("", $original, 2); if (isset($chunks[1])) { $original = $chunks[0]; $plural = $chunks[1]; } else { $plural = ''; } $translation = $translations->insert($context, $original, $plural); if ($translated !== '') { if ($plural === '') { $translation->setTranslation($translated); } else { foreach (explode("", $translated) as $pluralIndex => $pluralValue) { if ($pluralIndex === 0) { $translation->setTranslation($pluralValue); } else { $translation->setPluralTranslation($pluralValue, $pluralIndex - 1); } } } } } } return $translations; }
public static function exportTranslations() { $translations = new Translations(); $db = \Database::get(); $r = $db->Execute('select asID from AttributeSets order by asID asc'); while ($row = $r->FetchRow()) { $set = static::getByID($row['asID']); $translations->insert('AttributeSet', $set->getAttributeSetName()); } return $translations; }
/** * Export all the translations associates to every trees. * * @return Translations */ public static function exportTranslations() { $translations = new Translations(); $currentLocale = Localization::activeLocale(); if ($currentLocale !== 'en_US') { Localization::changeLocale('en_US'); } try { $db = \Database::get(); $r = $db->Execute('select treeID from Trees order by treeID asc'); while ($row = $r->FetchRow()) { 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 \Concrete\Core\Tree\Node\Node */ if (isset($rootNode)) { $rootNode->exportTranslations($translations); } } } } catch (\Exception $x) { if ($currentLocale !== 'en_US') { Localization::changeLocale($currentLocale); } throw $x; } if ($currentLocale !== 'en_US') { Localization::changeLocale($currentLocale); } return $translations; }