Skip to content

Loading…

Feature ini adapter translate #2706

Closed
wants to merge 11 commits into from

4 participants

@blanchonvincent

Feature ini adapter translate

@weierophinney weierophinney commented on an outdated diff
library/Zend/I18n/Translator/Loader/Ini.php
((61 lines not shown))
+ } else {
+ $messages[array_shift($message)] = array_shift($message);
+ }
+ }
+
+ if (!is_array($messages)) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Expected an array, but received %s',
+ gettype($messages)
+ ));
+ }
+
+ $textDomain = new TextDomain($messages);
+
+ if (array_key_exists('plural', $messagesNamespaced)) {
+ if (isset($messagesNamespaced['plural']['plural_forms'])) {
@weierophinney Zend Framework member

Combine these into a single conditional using '&&'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney commented on an outdated diff
library/Zend/I18n/Translator/Loader/Ini.php
((48 lines not shown))
+
+ $list = $messagesNamespaced;
+ if(isset($messagesNamespaced['translation'])) {
+ $list = $messagesNamespaced['translation'];
+ }
+ foreach($list as $message) {
+ if(!is_array($message) || count($message) < 2) {
+ throw new Exception\InvalidArgumentException(
+ 'Each INI row must be an array with message and translation'
+ );
+ }
+ if(isset($message['message']) && isset($message['translation'])) {
+ $messages[$message['message']] = $message['translation'];
+ } else {
+ $messages[array_shift($message)] = array_shift($message);
+ }
@weierophinney Zend Framework member

Rewrite the above as:

if (isset($message['message'] && isset($message['translation'])) {
    $messages[$message['message'] = $message['translation'];
    continue;
}
$messages[array_shift($message)] = array_shift($message);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@DASPRiD DASPRiD was assigned
@weierophinney
Zend Framework member

Looks clean enough -- have you had any feedback on the mailing list or @DASPRiD ?

@blanchonvincent

Code change is done. Thx.
No, i had no feedback from the mailing list.

@ThomasCantonnet ThomasCantonnet commented on an outdated diff
...8n/Translator/_files/translation_en_simple_syntax.ini
((11 lines not shown))
+idenfitier3[] = "Message 3"
+idenfitier3[] = "Message 3 (en)"
+
+idenfitier4[] = "Message 4"
+idenfitier4[] = "Message 4 (en)"
+
+idenfitier5.message = "Message 5"
+idenfitier5.translation.0 = "Message 5 (en) Plural 0"
+idenfitier5.translation.1 = "Message 5 (en) Plural 1"
+idenfitier5.translation.2 = "Message 5 (en) Plural 2"
+
+idenfitier6[] = "Cooking furniture"
+idenfitier6[] = "Küchen Möbel (en)"
+
+idenfitier7[] = "Küchen Möbel"
+idenfitier7[] = "Cooking furniture (en)"

Dunno if you wanna correct everything, but you wrote identifier wrong everywhere (idenfitier) :p

Oops, i will change this :)

It's fixed, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney added a commit that referenced this pull request
@weierophinney weierophinney [#2706] CS cleanup
- whitespace around conditionals
- remove commented code
- EOF markers
c6a496c
@weierophinney
Zend Framework member

@blanchonvincent If you haven't yet, please submit some docs on using the new adapter. :)

@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney [#2706] CS cleanup
- whitespace around conditionals
- remove commented code
- EOF markers
15ca53e
@ghost Unknown pushed a commit that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/i18n-translator-ini' into develop
Close #2706
f6d25c0
@weierophinney weierophinney added a commit to zendframework/zend-i18n that referenced this pull request
@weierophinney weierophinney [zendframework/zf2#2706] CS cleanup
- whitespace around conditionals
- remove commented code
- EOF markers
fd6f1d5
@weierophinney weierophinney added a commit to zendframework/zend-i18n that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/i18n-translator-ini' into develop 9e59ae6
@weierophinney weierophinney added a commit to zendframework/zend-i18n-resources that referenced this pull request
@weierophinney weierophinney Merge branch 'feature/i18n-translator-ini' into develop beb876e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 8, 2012
  1. @blanchonvincent

    Fix french translation

    blanchonvincent committed
    Fix french translation
  2. @blanchonvincent

    Other fix french translation

    blanchonvincent committed
    Other fix french translation
  3. @blanchonvincent

    Add INI loader for translator

    blanchonvincent committed
    Add INI loader for translator
  4. @blanchonvincent

    Add tests for INI loader translator

    blanchonvincent committed
    Add tests for INI loader translator
  5. @blanchonvincent

    Add new syntax more user friendly

    blanchonvincent committed
    Add new syntax more user friendly
  6. @blanchonvincent

    Add tests for new syntax

    blanchonvincent committed
    Add tests for new syntax
  7. @blanchonvincent

    Fix CS PSR2

    blanchonvincent committed
    Fix CS PSR2
  8. @blanchonvincent

    revert zend messages, bad manipulation

    blanchonvincent committed
    revert zend messages, bad manipulation
Commits on Oct 10, 2012
  1. @blanchonvincent

    Code refactory, more readable

    blanchonvincent committed
    Code refactory, more readable
  2. @blanchonvincent

    Fix code condition

    blanchonvincent committed
    Fix code condition
Commits on Nov 14, 2012
  1. @blanchonvincent

    Fix word "identifier"

    blanchonvincent committed
This page is out of date. Refresh to see the latest.
View
84 library/Zend/I18n/Translator/Loader/Ini.php
@@ -0,0 +1,84 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_I18n
+ */
+
+namespace Zend\I18n\Translator\Loader;
+
+use Zend\Config\Reader\Ini as IniReader;
+use Zend\I18n\Exception;
+use Zend\I18n\Translator\Plural\Rule as PluralRule;
+use Zend\I18n\Translator\TextDomain;
+
+/**
+ * PHP INI format loader.
+ *
+ * @category Zend
+ * @package Zend_I18n
+ * @subpackage Translator
+ */
+class Ini implements FileLoaderInterface
+{
+ /**
+ * load(): defined by FileLoaderInterface.
+ *
+ * @see FileLoaderInterface::load()
+ * @param string $locale
+ * @param string $filename
+ * @return TextDomain|null
+ * @throws Exception\InvalidArgumentException
+ */
+ public function load($locale, $filename)
+ {
+ if (!is_file($filename) || !is_readable($filename)) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Could not open file %s for reading',
+ $filename
+ ));
+ }
+
+ $messages = array();
+ $iniReader = new IniReader();
+ $messagesNamespaced = $iniReader->fromFile($filename);
+
+ $list = $messagesNamespaced;
+ if(isset($messagesNamespaced['translation'])) {
+ $list = $messagesNamespaced['translation'];
+ }
+ foreach($list as $message) {
+ if(!is_array($message) || count($message) < 2) {
+ throw new Exception\InvalidArgumentException(
+ 'Each INI row must be an array with message and translation'
+ );
+ }
+ if(isset($message['message']) && isset($message['translation'])) {
+ $messages[$message['message']] = $message['translation'];
+ continue;
+ }
+ $messages[array_shift($message)] = array_shift($message);
+ }
+
+ if (!is_array($messages)) {
+ throw new Exception\InvalidArgumentException(sprintf(
+ 'Expected an array, but received %s',
+ gettype($messages)
+ ));
+ }
+
+ $textDomain = new TextDomain($messages);
+
+ if (array_key_exists('plural', $messagesNamespaced) &&
+ isset($messagesNamespaced['plural']['plural_forms'])) {
+ $textDomain->setPluralRule(
+ PluralRule::fromString($messagesNamespaced['plural']['plural_forms'])
+ );
+ }
+
+ return $textDomain;
+ }
+}
View
1 library/Zend/I18n/Translator/LoaderPluginManager.php
@@ -34,6 +34,7 @@ class LoaderPluginManager extends AbstractPluginManager
protected $invokableClasses = array(
'phparray' => 'Zend\I18n\Translator\Loader\PhpArray',
'gettext' => 'Zend\I18n\Translator\Loader\Gettext',
+ 'ini' => 'Zend\I18n\Translator\Loader\Ini',
);
/**
View
103 tests/ZendTest/I18n/Translator/Loader/IniTest.php
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_I18n
+ */
+
+namespace ZendTest\I18n\Translator\Loader;
+
+use PHPUnit_Framework_TestCase as TestCase;
+use Locale;
+use Zend\I18n\Translator\Loader\Ini as IniLoader;
+
+class IniTest extends TestCase
+{
+ protected $testFilesDir;
+ protected $originalLocale;
+
+ public function setUp()
+ {
+ //$this->originalLocale = Locale::getDefault();
+ //Locale::setDefault('en_EN');
+
+ $this->testFilesDir = realpath(__DIR__ . '/../_files');
+ }
+
+ public function tearDown()
+ {
+ //Locale::setDefault($this->originalLocale);
+ }
+
+ public function testLoaderFailsToLoadMissingFile()
+ {
+ $loader = new IniLoader();
+ $this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException', 'Could not open file');
+ $loader->load('en_EN', 'missing');
+ }
+
+ public function testLoaderLoadsEmptyFile()
+ {
+ $loader = new IniLoader();
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_empty.ini');
+ $this->assertInstanceOf('Zend\I18n\Translator\TextDomain', $textDomain);
+ }
+
+ public function testLoaderFailsToLoadNonArray()
+ {
+ $loader = new IniLoader();
+ $this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException',
+ 'Each INI row must be an array with message and translation');
+ $loader->load('en_EN', $this->testFilesDir . '/failed.ini');
+ }
+
+ public function testLoaderFailsToLoadBadSyntax()
+ {
+ $loader = new IniLoader();
+ $this->setExpectedException('Zend\I18n\Exception\InvalidArgumentException',
+ 'Each INI row must be an array with message and translation');
+ $loader->load('en_EN', $this->testFilesDir . '/failed_syntax.ini');
+ }
+
+
+ public function testLoaderReturnsValidTextDomain()
+ {
+ $loader = new IniLoader();
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en.ini');
+
+ $this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
+ $this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
+ }
+
+ public function testLoaderReturnsValidTextDomainWithFileWithoutPlural()
+ {
+ $loader = new IniLoader();
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en_without_plural.ini');
+
+ $this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
+ $this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
+ }
+
+ public function testLoaderReturnsValidTextDomainWithSimpleSyntax()
+ {
+ $loader = new IniLoader();
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en_simple_syntax.ini');
+
+ $this->assertEquals('Message 1 (en)', $textDomain['Message 1']);
+ $this->assertEquals('Message 4 (en)', $textDomain['Message 4']);
+ }
+
+ public function testLoaderLoadsPluralRules()
+ {
+ $loader = new IniLoader();
+ $textDomain = $loader->load('en_EN', $this->testFilesDir . '/translation_en.ini');
+
+ $this->assertEquals(2, $textDomain->getPluralRule()->evaluate(0));
+ $this->assertEquals(0, $textDomain->getPluralRule()->evaluate(1));
+ $this->assertEquals(1, $textDomain->getPluralRule()->evaluate(2));
+ $this->assertEquals(2, $textDomain->getPluralRule()->evaluate(10));
+ }
+}
View
1 tests/ZendTest/I18n/Translator/_files/failed.ini
@@ -0,0 +1 @@
+identifier1 = "Message 1"
View
1 tests/ZendTest/I18n/Translator/_files/failed_syntax.ini
@@ -0,0 +1 @@
+identifier1[] = "Message 1"
View
0 tests/ZendTest/I18n/Translator/_files/translation_empty.ini
No changes.
View
26 tests/ZendTest/I18n/Translator/_files/translation_en.ini
@@ -0,0 +1,26 @@
+[plural]
+plural_forms = 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);'
+
+[translation]
+identifier1.message = "Message 1"
+identifier1.translation = "Message 1 (en)"
+
+identifier2.message = "Message 2"
+identifier2.translation = "Message 2 (en)"
+
+identifier3.message = "Message 3"
+identifier3.translation = "Message 3 (en)"
+
+identifier4.message = "Message 4"
+identifier4.translation = "Message 4 (en)"
+
+identifier5.message = "Message 5"
+identifier5.translation.0 = "Message 5 (en) Plural 0"
+identifier5.translation.1 = "Message 5 (en) Plural 1"
+identifier5.translation.2 = "Message 5 (en) Plural 2"
+
+identifier6.message = "Cooking furniture"
+identifier6.translation = "Küchen Möbel (en)"
+
+identifier7.message = "Küchen Möbel"
+identifier7.translation = "Cooking furniture (en)"
View
26 tests/ZendTest/I18n/Translator/_files/translation_en_simple_syntax.ini
@@ -0,0 +1,26 @@
+[plural]
+plural_forms = 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);'
+
+[translation]
+identifier1[] = "Message 1"
+identifier1[] = "Message 1 (en)"
+
+identifier2[] = "Message 2"
+identifier2[] = "Message 2 (en)"
+
+identifier3[] = "Message 3"
+identifier3[] = "Message 3 (en)"
+
+identifier4[] = "Message 4"
+identifier4[] = "Message 4 (en)"
+
+identifier5.message = "Message 5"
+identifier5.translation.0 = "Message 5 (en) Plural 0"
+identifier5.translation.1 = "Message 5 (en) Plural 1"
+identifier5.translation.2 = "Message 5 (en) Plural 2"
+
+identifier6[] = "Cooking furniture"
+identifier6[] = "Küchen Möbel (en)"
+
+identifier7[] = "Küchen Möbel"
+identifier7[] = "Cooking furniture (en)"
View
22 tests/ZendTest/I18n/Translator/_files/translation_en_without_plural.ini
@@ -0,0 +1,22 @@
+identifier1.message = "Message 1"
+identifier1.translation = "Message 1 (en)"
+
+identifier2.message = "Message 2"
+identifier2.translation = "Message 2 (en)"
+
+identifier3.message = "Message 3"
+identifier3.translation = "Message 3 (en)"
+
+identifier4.message = "Message 4"
+identifier4.translation = "Message 4 (en)"
+
+identifier5.message = "Message 5"
+identifier5.translation.0 = "Message 5 (en) Plural 0"
+identifier5.translation.1 = "Message 5 (en) Plural 1"
+identifier5.translation.2 = "Message 5 (en) Plural 2"
+
+identifier6.message = "Cooking furniture"
+identifier6.translation = "Küchen Möbel (en)"
+
+identifier7.message = "Küchen Möbel"
+identifier7.translation = "Cooking furniture (en)"
Something went wrong with that request. Please try again.