本文整理汇总了PHP中Gettext\Translations类的典型用法代码示例。如果您正苦于以下问题:PHP Translations类的具体用法?PHP Translations怎么用?PHP Translations使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Translations类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->writeSection($output, 'Po to Csv converter');
$this->cwd = getcwd() . DIRECTORY_SEPARATOR;
$output->writeln('<info>Using CWD</info> ' . $this->cwd);
$poFiles = $this->getInputPoFiles($input, $output);
$outputFolder = $this->getOutputFolder($input, $output);
$useDefaults = $input->getOption('use-defaults');
if ($useDefaults) {
$output->writeln('<info>Po files</info>:');
$this->writeList($output, $poFiles);
$output->writeln(['', '<info>Output folder</info>: ' . $outputFolder]);
}
$poHandles = [];
foreach ($poFiles as $poFile) {
$key = basename($poFile, '.po');
$output->writeln('<info>loading ' . $key . '</info>...');
$poHandles[$key] = Translations::fromPoFile($poFile);
}
$output->writeln('<info>merging po files</info>...');
$csvArray = [];
foreach ($poHandles as $language => $poHandle) {
foreach ($poHandle as $translation) {
$original = trim($translation->original);
$translation = trim($translation->translation);
if (!isset($csvArray[$original])) {
$csvArray[$original] = [$language => $translation];
} elseif (!isset($csvArray[$original][$language])) {
$csvArray[$original][$language] = $translation;
} elseif ($csvArray[$original][$language] != $translation) {
$csvArray[$original][$language] = $this->handleConflict($input, $output, $original, $csvArray[$original][$language], $translation);
}
}
}
$output->writeln('<info>writing csv</info>...');
$writer = Writer::createFromFileObject(new SplTempFileObject());
$writer->setDelimiter(';');
$header = ['original'];
$header = array_merge($header, array_keys($poHandles));
$writer->insertOne($header);
foreach ($csvArray as $original => $item) {
$row = [];
foreach ($header as $column) {
if ($column === 'original') {
$row[] = $original;
} else {
$row[] = isset($item[$column]) ? $item[$column] : null;
}
}
$writer->insertOne($row);
}
$outputFile = $outputFolder . DIRECTORY_SEPARATOR . 'translations.csv';
file_put_contents($outputFile, $writer->__toString());
$output->writeln('<info>done. output file is</info> ' . $outputFile);
}
示例2: process
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);
}
}
}
示例3: testAllKnownTranslationFunctionsAreCovered
/**
* All known translation functions are covered.
*/
public function testAllKnownTranslationFunctionsAreCovered()
{
$extractor = new WordPressExtractor();
$translations = new Translations();
$translations->setDomain('test');
$translations = $extractor->fromDirectory($this->getResourcesPath() . 'commonSources', $translations);
// file_put_contents($this->getResourcesPath() . 'commonSources.php', var_export($arrayCopy, true));
$poContent = $translations->toPoString();
// strip base path for better comparison
$poContent = str_replace($this->getResourcesPath(), '', $poContent);
$this->assertContains('"translate"', $poContent);
$this->assertContains('"translate_with_gettext_context"', $poContent);
$this->assertContains('"__"', $poContent);
$this->assertContains('"_x"', $poContent);
$this->assertContains('"_e"', $poContent);
$this->assertContains('"_ex"', $poContent);
$this->assertContains('"esc_attr__"', $poContent);
$this->assertContains('"esc_attr_e"', $poContent);
$this->assertContains('"esc_attr_x"', $poContent);
$this->assertContains('"esc_html__"', $poContent);
$this->assertContains('"esc_html_e"', $poContent);
$this->assertContains('"esc_html_x"', $poContent);
$this->assertContains('"_n-single"', $poContent);
$this->assertContains('"_n-plural"', $poContent);
$this->assertContains('"_nx-context"', $poContent);
$this->assertContains('"_nx-single"', $poContent);
$this->assertContains('"_nx-plural"', $poContent);
$this->assertContains('"_n_noop-singular"', $poContent);
$this->assertContains('"_n_noop-plural"', $poContent);
$this->assertContains('"_nx_noop-context"', $poContent);
$this->assertContains('"_nx_noop-singular"', $poContent);
$this->assertContains('"_nx_noop-plural"', $poContent);
}
示例4: main
/**
* 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);
}
示例5: exportTranslations
/**
* @deprecated
*/
public function exportTranslations()
{
$translations = new Translations();
foreach ($this->getList() as $type) {
$translations->insert('AttributeTypeName', $type->getAttributeTypeName());
}
return $translations;
}
示例6: mergeTranslationsWithSectionFile
public function mergeTranslationsWithSectionFile(Section $section, Translations $translations)
{
$file = DIR_LANGUAGES_SITE_INTERFACE . '/' . $section->getLocale() . '.po';
if (is_file($file)) {
$sectionTranslations = PoExtractor::fromFile($file);
$translations->mergeWith($sectionTranslations);
}
}
示例7: generateHeaders
/**
* Returns the headers as a string.
*
* @param Translations $translations
*
* @return string
*/
private static function generateHeaders(Translations $translations)
{
$headers = '';
foreach ($translations->getHeaders() as $name => $value) {
$headers .= sprintf("%s: %s\n", $name, $value);
}
return $headers;
}
示例8: exportTranslations
/**
* @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;
}
示例9: exportTranslations
/**
* @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;
}
示例10: exportTranslations
/**
* @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;
}
示例11: parseDirectoryDo
/**
* {@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]);
}
}
}
示例12: insertTranslation
/**
* 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);
}
示例13: fromString
/**
* {@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;
}
示例14: exportTranslations
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;
}
示例15: saveGettextFunctions
/**
* 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);
}
}
}
}
}