Przeglądaj źródła

[ZF-9500] Zend_Translate:

- added better handling for "ignore" option

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@21655 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas 16 lat temu
rodzic
commit
594d794033

+ 100 - 1
documentation/manual/en/module_specs/Zend_Translate-Additional.xml

@@ -102,7 +102,8 @@ $translate->addTranslation('/path/to/new.csv', 'fr', $options);
                             files will be ignored. Setting this value to <code>'tmp'</code> would
                             mean that directories and files like <code>'tmpImages'</code> and
                             <code>'tmpFiles'</code> would be ignored as well as all subsequent
-                            directories
+                            directories. This option also accepts an array which can be used when
+                            you want to ignore more than one prefix.
                         </entry>
 
                         <entry><emphasis>.</emphasis></entry>
@@ -623,6 +624,104 @@ $translate = new Zend_Translate(
 ]]></programlisting>
             </sect4>
         </sect3>
+
+        <sect3 id="zend.translate.additional.detection.ignore">
+            <title>Ignoring special files and directories</title>
+
+            <para>
+                Sometimes it is useful to exclude files or even directories from being added
+                automatically. Therefor you can use the <property>ignore</property> option which
+                accepts 3 possible usages.
+            </para>
+
+            <sect4 id="zend.translate.additional.detection.ignore.string">
+                <title>Ignore a special directory or file</title>
+
+                <para>
+                    Per default <classname>Zend_Translate</classname> is set to ignore all
+                    files and directories beginning with <emphasis>'/.'</emphasis>. This means that
+                    all <acronym>SVN</acronym> files will be ignored.
+                </para>
+
+                <para>
+                    You can set your own syntax by giving a string for the
+                    <property>ignore</property> option. The directory separator will be attached
+                    automatically and has to be omitted.
+                </para>
+
+                <programlisting><![CDATA[
+$options   = array('ignore' => 'test');
+$translate = new Zend_Translate($adapter, $content, $locale, $options);
+]]></programlisting>
+
+                <para>
+                    The above example will ignore all files and directories beginning with
+                    <emphasis>test</emphasis>. This means for example
+                    <filename>/test/en.mo</filename>, <filename>/testing/en.mo</filename> and
+                    <filename>/dir/test_en.mo</filename>. But it would still add
+                    <filename>/mytest/en.mo</filename> or <filename>/dir/atest.mo</filename>.
+                </para>
+
+                <note>
+                    <title>Prevent SVN files from being searched</title>
+
+                    <para>
+                        When you set this option, then the default <emphasis>'/.'</emphasis> will
+                        be erased. This means that <classname>Zend_Translate</classname> will then
+                        add all files from the hidden <acronym>SVN</acronym> directories. When you
+                        are working with <acronym>SVN</acronym>, then you should use the array
+                        syntax described in the next section.
+                    </para>
+                </note>
+            </sect4>
+
+            <sect4 id="zend.translate.additional.detection.ignore.array">
+                <title>Ignore several directories or files</title>
+
+                <para>
+                    You can also ignore several files and directories. Instead of a string,
+                    you can simply give an array with all wished names which will be ignored.
+                </para>
+
+                <programlisting><![CDATA[
+$options = array('ignore' => array('.', 'test', 'old'));
+$translate = new Zend_Translate($adapter, $content, $locale, $options);
+]]></programlisting>
+
+                <para>
+                    In the above case all 3 syntax will be ignored. But still they have to
+                    begin with the syntax to be detected and ignored.
+                </para>
+            </sect4>
+
+            <sect4 id="zend.translate.additional.detection.ignore.array">
+                <title>Ignore specific names</title>
+
+                <para>
+                    To ignore files and directories which are not beginning with a defined syntax
+                    but have a special syntax anywhere within their name you can use a regular
+                    expression.
+                </para>
+
+                <para>
+                    To use a regular expression the array key of the <property>ignore</property>
+                    option has to begin with <emphasis>regex</emphasis>.
+                </para>
+
+                <programlisting><![CDATA[
+$options = array('ignore' => array('regex' => '/test/u', 'regex_2' => '/deleted$/u'));
+$translate = new Zend_Translate($adapter, $content, $locale, $options);
+]]></programlisting>
+
+                <para>
+                    In the above case we defined 2 regular expressions. The files and directories
+                    will always being searched with all given regular expressions. In our example
+                    this means that any files which contains <emphasis>test</emphasis> anywhere in
+                    their name will be ignored. Additionally all files and directories which end
+                    with <emphasis>deleted</emphasis> will not be added as translation.
+                </para>
+            </sect4>
+        </sect3>
     </sect2>
 
     <sect2 id="zend.translate.additional.combination">

+ 23 - 7
library/Zend/Translate/Adapter.php

@@ -67,6 +67,7 @@ abstract class Zend_Translate_Adapter {
     /**
      * Array with all options, each adapter can have own additional options
      *   'clear'           => when true, clears already loaded data when adding new files
+     *   'content'         => content to translate or file or directory with content
      *   'disableNotices'  => when true, omits notices from being displayed
      *   'ignore'          => a prefix for files and directories which are not being added
      *   'locale'          => the actual set locale to use
@@ -80,6 +81,7 @@ abstract class Zend_Translate_Adapter {
      */
     protected $_options = array(
         'clear'           => false,
+        'content'         => null,
         'disableNotices'  => false,
         'ignore'          => '.',
         'locale'          => 'auto',
@@ -99,14 +101,14 @@ abstract class Zend_Translate_Adapter {
     /**
      * Generates the adapter
      *
-     * @param  string|array       $data    OPTIONAL Translation data or filename for this adapter
+     * @param  string|array       $content OPTIONAL Translation content or filename for this adapter
      * @param  string|Zend_Locale $locale  OPTIONAL Locale/Language to set, identical with Locale
      *                                     identifiers see Zend_Locale for more information
      * @param  array              $options OPTIONAL Options for the adaptor
      * @throws Zend_Translate_Exception
      * @return void
      */
-    public function __construct($data = null, $locale = null, array $options = array())
+    public function __construct($content = null, $locale = null, array $options = array())
     {
         if (isset(self::$_cache)) {
             $id = 'Zend_Translate_' . $this->toString() . '_Options';
@@ -123,8 +125,8 @@ abstract class Zend_Translate_Adapter {
         }
 
         $this->setOptions($options);
-        if ($data !== null) {
-            $this->addTranslation($data, $locale, $options);
+        if ($content !== null) {
+            $this->addTranslation($content, $locale, $options);
         }
 
         if ($this->getLocale() !== (string) $locale) {
@@ -178,9 +180,23 @@ abstract class Zend_Translate_Adapter {
                      new RecursiveDirectoryIterator($data, RecursiveDirectoryIterator::KEY_AS_PATHNAME),
                      RecursiveIteratorIterator::SELF_FIRST) as $directory => $info) {
                 $file = $info->getFilename();
-                if (strpos($directory, DIRECTORY_SEPARATOR . $options['ignore']) !== false) {
-                    // ignore files matching first characters from option 'ignore' and all files below
-                    continue;
+                if (is_array($options['ignore'])) {
+                    foreach ($options['ignore'] as $key => $ignore) {
+                        if (strpos($key, 'regex') !== false) {
+                            if (preg_match($ignore, $directory)) {
+                                // ignore files matching the given regex from option 'ignore' and all files below
+                                continue 2;
+                            }
+                        } else if (strpos($directory, DIRECTORY_SEPARATOR . $ignore) !== false) {
+                            // ignore files matching first characters from option 'ignore' and all files below
+                            continue 2;
+                        }
+                    }
+                } else {
+                    if (strpos($directory, DIRECTORY_SEPARATOR . $options['ignore']) !== false) {
+                        // ignore files matching first characters from option 'ignore' and all files below
+                        continue;
+                    }
                 }
 
                 if ($info->isDir()) {

+ 36 - 2
tests/Zend/TranslateTest.php

@@ -665,13 +665,11 @@ class Zend_TranslateTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 0));
     }
 
-
     /**
      * @group ZF-9489
      */
     public function testAddingAdapterToSourcealsUsingOwnRule()
     {
-print "\n=============================================";
         $translate = new Zend_Translate(
             Zend_Translate::AN_ARRAY,
             array('singular' =>
@@ -698,6 +696,42 @@ print "\n=============================================";
     }
 
     /**
+     * @group ZF-9500
+     */
+    public function testIgnoreMultipleDirectories()
+    {
+        $translate = new Zend_Translate(
+            Zend_Translate::AN_ARRAY,
+            dirname(__FILE__) . '/Translate/Adapter/_files/testArray/',
+            'auto',
+            array(
+                'scan' => Zend_Translate::LOCALE_FILENAME,
+                'ignore' => array('.', 'ignoreme', 'LC_TEST')
+            )
+        );
+
+        $langs = $translate->getList();
+        $this->assertFalse(array_key_exists('de_DE', $langs));
+        $this->assertTrue(array_key_exists('ja', $langs));
+        $this->assertTrue(array_key_exists('en_US', $langs));
+
+        $translate2 = new Zend_Translate(
+            Zend_Translate::AN_ARRAY,
+            dirname(__FILE__) . '/Translate/Adapter/_files/testArray/',
+            'auto',
+            array(
+                'scan' => Zend_Translate::LOCALE_FILENAME,
+                'ignore' => array('.', 'regex_1' => '/de_DE/', 'regex' => '/ja/')
+            )
+        );
+
+        $langs = $translate2->getList();
+        $this->assertFalse(array_key_exists('de_DE', $langs));
+        $this->assertFalse(array_key_exists('ja', $langs));
+        $this->assertTrue(array_key_exists('en_US', $langs));
+    }
+
+    /**
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      *
      * @param  integer $errno