Author: edng
Date: 2010-09-10 03:16:56 +0200 (Fri, 10 Sep 2010)
New Revision: 30867
Added:
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/LICENSE
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/README
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/helper/
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/helper/I18NPluralHelper.php
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfI18NGettextPlural.class.php
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageFormatPlural.php
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageSource_gettextPlural.class.php
Log:
Tagging the 1.0.0 release
Copied:
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/LICENSE
(from rev 30865, plugins/sfI18NGettextPluralPlugin/trunk/LICENSE)
===================================================================
---
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/LICENSE
(rev 0)
+++
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/LICENSE
2010-09-10 01:16:56 UTC (rev 30867)
@@ -0,0 +1,7 @@
+Copyright (c) 2010 Edwood Ng
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copied:
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/README
(from rev 30865, plugins/sfI18NGettextPluralPlugin/trunk/README)
===================================================================
---
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/README
(rev 0)
+++
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/README
2010-09-10 01:16:56 UTC (rev 30867)
@@ -0,0 +1,90 @@
+sfI18NGettextPluralPlugin
+=========================
+
+The `sfI18NGettextPluralPlugin` extends sfI18N to provide extended support on
Gettext PO/MO file's plural forms format.
+
+This plugin will interpret the plural form formula, that's usually located in
PO/MO file's meta data section, to remove the need to specify the formula when
you use format_number_choice. This is especially important for application
that supports languages with more complex plural forms such as Russian. This
plugin also fixes an issue in current sfI18N implementation where it doesn't
break down the plural forms into individual translated text. When a
translation has plural form, the singular and plural form's text would be
merged.
+
+Content
+-------
+ * `sfI18NGettextPlural.class.php` class which extends sfI18N
+ * `sfMessageFormatPlural.class.php` class which extends sfMessageFormat
+ * `sfMessageSource_gettextPlural.class.php` class which extends
sfMessageSource_gettext
+ * `I18NPluralHelper.php` adds a new function called __plural($text, $plural,
$args, $catalogue)
+
+Installation
+------------
+
+ * Install the plugin
+
+ $ symfony plugin:install sfI18NGettextPluralPlugin
+
+ * Change i18n class in `factories.yml` from `sfI18N` to
`sfI18NGettextPlural` and add/modify params `source` to `gettextPlural`
+
+ all:
+ i18n:
+ class: sfI18NGettextPlural
+ param:
+ source: gettextPlural
+
+ * Create PO and MO files and place it in i18n directory of your app. (e.g.
/apps/frontend/i18n/<language>) MO files are binary format of the PO and MO
files will be read by this plugin. You can convert PO to MO with the `msgfmt`
tool.
+
+ * Enable `i18n` in `settings.yml`
+
+ all:
+ .settings:
+ i18n: true
+
+ * Add I18N and I18NPlural helpers
+
+ all:
+ .settings:
+ standard_helpers: [Partial, Cache, I18N, I18NPlural]
+
+ * Clear the cache
+
+ $ symfony cache:clear
+
+How plugin works
+----------------
+
+The plugin will first look for `Plural-Forms` in the PO/MO file's meta data
section and store it as `%PLURAL-FORMS%` in the same translation associative
array. The plural forms is stored as an array under a formulated key
`<singular form>|<plural form>`. For example, Russian has 1 singular and 2
plural forms. In the PO/MO's header section, you will need to specify the
formula as follow:
+
+ "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));"
+
+In the translation, you will have something like this:
+
+ msgid "1 student"
+ msgid_plural "@count students"
+ msgstr[0] "@count человек"
+ msgstr[1] "@count человека"
+ msgstr[2] "@count человек"
+
+After we parse the MO file, the associative array will have following (this is
for illustrative purpose only, the actual array is slightly different):
+
+ '%PLURAL-FORMS' =>
'((((n%10)==1)&&((n%100)!=11))?(0):(((((n%10)>=2)&&((n%10)<=4))&&(((n%100)<10)||((n%100)>=20)))?(1):2));\n'
+ '1 student' => '1 человек'
+ '1 student|@count students' => Array( [0] => '@count человек'
+ [1] => '@count человека'
+ [2] => '@count человек' )
+
+A new function is added for producing plural forms translation.
+
+ __plural($text, $plural, $args, $catalogue)
+
+Example:
+
+ __plural('1 student','@count students', array('@count' => '2'));
+
+You can continue to use the usual __($text, $args, $catalogue) function for
singular form translation.
+
+* Note that the first parameter is used for plural form calculation. If you
have other parameters you like to insert before the numeric value, you will
have to break up the translation to ensure the numeric value always appears as
the first parameter.
+
+* Also, keep in mind that there is a chance of naming collision with
`%PLURAL-FORMS%` and the generated plural form key `<singular form>|<plural
form>`. Please make sure you don't use any of such form as your normal
translation key.
+
+Changelog
+---------
+
+### 2010-09-09 | 1.0.0 Stable
+
+ * edng: Initial release (symfony 1.4)
Copied:
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/helper/I18NPluralHelper.php
(from rev 30865,
plugins/sfI18NGettextPluralPlugin/trunk/lib/helper/I18NPluralHelper.php)
===================================================================
---
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/helper/I18NPluralHelper.php
(rev 0)
+++
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/helper/I18NPluralHelper.php
2010-09-10 01:16:56 UTC (rev 30867)
@@ -0,0 +1,48 @@
+<?
+/*
+ * This file is part of the sfI18NGettextPlural package.
+ * (c) Edwood Ng
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this plugin.
+ */
+
+/**
+ * This file contains a new helper function for displaying transltion in
plural form.
+ *
+ * @package sfI18NGettextPlural
+ * @author Edwood Ng <[email protected]>
+ * @version SVN: $Id$
+ */
+
+/**
+ * Call the appropriate __plural function in sfI18NGettextPlural class for
plural form
+ * translation.
+ *
+ * @see I18NPluralHelper
+ */
+function __plural($text, $plural, $args = array(), $catalogue = 'messages')
+{
+ if (sfConfig::get('sf_i18n'))
+ {
+ return sfContext::getInstance()->getI18N()->__plural($text, $plural,
$args, $catalogue);
+ }
+ else
+ {
+ if (empty($args))
+ {
+ $args = array();
+ }
+
+ // replace object with strings
+ foreach ($args as $key => $value)
+ {
+ if (is_object($value) && method_exists($value, '__toString'))
+ {
+ $args[$key] = $value->__toString();
+ }
+ }
+
+ return strtr($text, $args);
+ }
+}
Copied:
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfI18NGettextPlural.class.php
(from rev 30865,
plugins/sfI18NGettextPluralPlugin/trunk/lib/i18n/sfI18NGettextPlural.class.php)
===================================================================
---
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfI18NGettextPlural.class.php
(rev 0)
+++
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfI18NGettextPlural.class.php
2010-09-10 01:16:56 UTC (rev 30867)
@@ -0,0 +1,50 @@
+<?php
+/*
+ * This file is part of the sfI18NGettextPlural package.
+ * (c) Edwood Ng
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this plugin.
+ */
+
+/**
+ * sfI18NGettextPlural class provides a new function __plural($string,
$plural, $args, $catalogue) for
+ * producing plural form translation.
+ *
+ *
+ * @package sfI18NGettextPlural
+ * @author Edwood Ng <[email protected]>
+ * @version SVN: $Id$
+ */
+class sfI18NGettextPlural extends sfI18N
+{
+ /**
+ * Call sfMessageFormatPlural->formatPlural to produce plural translation
+ *
+ * @see sfI18N
+ */
+ public function __plural($string, $plural, $args = array(), $catalogue =
'messages')
+ {
+ return $this->getMessageFormat()->formatPlural($string, $plural, $args,
$catalogue);
+ }
+
+ /**
+ * Returns sfMessageFormatPlural
+ *
+ * @see sfI18N
+ */
+ public function getMessageFormat()
+ {
+ if (!isset($this->messageFormat))
+ {
+ $this->messageFormat = new
sfMessageFormatPlural($this->getMessageSource(), sfConfig::get('sf_charset'));
+
+ if ($this->options['debug'])
+ {
+
$this->messageFormat->setUntranslatedPS(array($this->options['untranslated_prefix'],
$this->options['untranslated_suffix']));
+ }
+ }
+
+ return $this->messageFormat;
+ }
+}
Copied:
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageFormatPlural.php
(from rev 30865,
plugins/sfI18NGettextPluralPlugin/trunk/lib/i18n/sfMessageFormatPlural.php)
===================================================================
---
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageFormatPlural.php
(rev 0)
+++
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageFormatPlural.php
2010-09-10 01:16:56 UTC (rev 30867)
@@ -0,0 +1,119 @@
+<?php
+/*
+ * This file is part of the sfI18NGettextPlural package.
+ * (c) Edwood Ng
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this plugin.
+ */
+
+/**
+ * sfMessageFormatPlural class provides formatting functions for proper plural
translation
+ *
+ *
+ * @package sfI18NGettextPlural
+ * @author Edwood Ng <[email protected]>
+ * @version SVN: $Id$
+ */
+class sfMessageFormatPlural extends sfMessageFormat
+{
+ /**
+ * Call formatStringPlural for plural translation formatting. Equivalent to
sfMessageFormat->format function.
+ *
+ * @see sfMessageFormat
+ */
+ public function formatPlural($string, $plural, $args = array(), $catalogue =
null, $charset = null)
+ {
+ // make sure that objects with __toString() are converted to strings
+ $string = (string) $string;
+ if (empty($charset))
+ {
+ $charset = $this->getCharset();
+ }
+
+ $s = $this->formatStringPlural(sfToolkit::I18N_toUTF8($string, $charset),
$plural, $args, $catalogue);
+
+ return sfToolkit::I18N_toEncoding($s, $charset);
+ }
+
+ /**
+ * Calculate plural forms index based on plural formula and will return
proper plural form translation.
+ * Equivalent to sfMessageFormat->formatString function.
+ *
+ * @see sfMessageFormat
+ */
+ protected function formatStringPlural($string, $plural, $args = array(),
$catalogue = null)
+ {
+ if (empty($args))
+ {
+ $args = array();
+ }
+
+ if (empty($catalogue))
+ {
+ $catalogue = empty($this->catalogue) ? 'messages' : $this->catalogue;
+ }
+
+ $this->loadCatalogue($catalogue);
+
+ foreach ($this->messages[$catalogue] as $variant)
+ {
+ // we found it, so return the target translation
+ if (isset($variant[$string]))
+ {
+ $target = $variant[$string];
+
+ if (is_array($target)) {
+ $target = array_shift($target);
+ }
+
+ if ($plural) {
+ $n = reset($args);
+ // search for plural form variant
+ $pluralTarget = $variant["$string|$plural"];
+
+ if (is_array($pluralTarget)) {
+ $pluralTarget = array_shift($pluralTarget);
+ }
+
+ if ($pluralTarget) {
+ // calculate plural form index
+ $formula = array_shift($variant['%PLURAL-FORMS%']);
+ $pid = @eval('return '.str_replace('n',$n,$formula));
+ if (isset($pluralTarget[$pid])) {
+ $target = $pluralTarget[$pid];
+ } else {
+ // target not found, it's likely due to missing data, return the
first translation
+ $target = array_shift($pluralTarget);
+ }
+ } else {
+ // plural form does not exist, this is probably the default
languge, use a simple plural form instead
+ if ($n > 1)
+ $target = $plural;
+ else
+ $target = $string;
+ }
+ }
+
+ // found, but untranslated
+ if (empty($target))
+ {
+ return $this->postscript[0].$this->replaceArgs($string,
$args).$this->postscript[1];
+ }
+ return $this->replaceArgs($target, $args);
+ }
+ }
+
+ // check if plural form is needed for the default language, use simple
plural form rule
+ if (empty($target) && $plural && !empty($args)) {
+ $n = reset($args);
+ if ($n > 1)
+ $string = $plural;
+ }
+
+ // well we did not find the translation string.
+ $this->source->append($string);
+
+ return $this->postscript[0].$this->replaceArgs($string,
$args).$this->postscript[1];
+ }
+}
Copied:
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageSource_gettextPlural.class.php
(from rev 30865,
plugins/sfI18NGettextPluralPlugin/trunk/lib/i18n/sfMessageSource_gettextPlural.class.php)
===================================================================
---
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageSource_gettextPlural.class.php
(rev 0)
+++
plugins/sfI18NGettextPluralPlugin/tags/sfI18NGettextPluralPlugin-1.0.0/lib/i18n/sfMessageSource_gettextPlural.class.php
2010-09-10 01:16:56 UTC (rev 30867)
@@ -0,0 +1,93 @@
+<?php
+/*
+ * This file is part of the sfI18NGettextPlural package.
+ * (c) Edwood Ng
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this plugin.
+ */
+
+/**
+ * sfMessageSource_gettextPlural class extends from sfMessageSource_gettext to
generate
+ * additional key value pairs on plural forms. It also extract Plural-Forms
formula
+ * from the meta data section for plural form calculation.
+ *
+ *
+ * @package sfI18NGettextPlural
+ * @author Edwood Ng <[email protected]>
+ * @version SVN: $Id$
+ */
+class sfMessageSource_gettextPlural extends sfMessageSource_gettext
+{
+ /**
+ * Loads the messages from a MO file.
+ *
+ * @param string $filename MO file.
+ * @return array of messages.
+ * @see sfMessageSource_gettext
+ */
+ public function &loadData($filename)
+ {
+ $mo = TGettext::factory('MO',$filename);
+ $mo->load();
+ $result = $mo->toArray();
+
+ $this->pluralizeResult($result);
+
+ $results = array();
+ $count = 0;
+
+ // add plural forms support
+ foreach ($result['meta'] as $source => $target)
+ {
+ if ($source == 'Plural-Forms')
+ {
+ $source = '%PLURAL-FORMS%';
+ // trim $target
+ $target = trim(substr($target, strpos($target, 'plural=') + 7));
+ $results[$source][] = $target; //target
+ $results[$source][] = $count++; //id
+ $results[$source][] = ''; //comments
+ break;
+ }
+ }
+ // make sure first entry is always used for plural forms
+ if ($count == 0)
+ {
+ $source = '%PLURAL-FORMS%';
+ $results[$source][] = ''; //target
+ $results[$source][] = $count++; //id
+ $results[$source][] = ''; //comments
+ }
+
+ foreach ($result['strings'] as $source => $target)
+ {
+ $results[$source][] = $target; //target
+ $results[$source][] = $count++; //id
+ $results[$source][] = ''; //comments
+ }
+
+ return $results;
+ }
+
+ /**
+ * Generate plural form key value pairs
+ */
+ public function pluralizeResult(&$result)
+ {
+ foreach ($result['strings'] as $source => $target)
+ {
+ $sourceTokens = explode("\0", $source);
+ if (count($sourceTokens) > 1) {
+ // delete the merged entry
+ unset($result['strings'][$source]);
+ // tokenize plural forms
+ $targetTokens = explode("\0", $target);
+ $result['strings'][$sourceTokens[0]] = $targetTokens[0];
+ for ($i = 0; $i < count($targetTokens); $i++) {
+ $result['strings'][$sourceTokens[0].'|'.$sourceTokens[1]][$i] =
$targetTokens[$i];
+ }
+ }
+ }
+ }
+}
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.