Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rewriting `Message::translate()` to use filters.

Moving placeholder and fallback features to filters.
Adding default filters to switchboard file.
Updating docblocks and tests.
  • Loading branch information...
commit 42125a4740b8294462d8dd51c1638d084d79dabe 1 parent 185c566
@davidpersson davidpersson authored
View
35 app/config/switchboard.php
@@ -19,11 +19,13 @@
use \lithium\http\Router;
use \lithium\core\Environment;
use \lithium\action\Dispatcher;
+use \lithium\g11n\Message;
+use \lithium\util\String;
/**
* Loads application routes before the request is dispatched. Change this to `include_once` if
* more than one request cycle is executed per HTTP request.
- *
+ *
* @see lithium\http\Router
*/
Dispatcher::applyFilter('run', function($self, $params, $chain) {
@@ -34,7 +36,7 @@
/**
* Intercepts the `Dispatcher` as it finds a controller object, and passes the `'request'` parameter
* to the `Environment` class to detect which environment the application is running in.
- *
+ *
* @see lithium\action\Request
* @see lithium\core\Environment
*/
@@ -43,4 +45,33 @@
return $chain->next($self, $params, $chain);
});
+/**
+ * Implements logic for handling cases where `Message::translate()` returns without a result.
+ * The message specified for the `'default'` option will be used as a fall back. By
+ * default the value for the options is the message passed to the method.
+ */
+Message::applyFilter('translate', function($self, $params, $chain) {
+ $params['options'] += array('default' => $params['id']);
+ return $chain->next($self, $params, $chain) ?: $params['options']['default'];
+});
+
+/**
+ * Placeholders in translated messages. Adds support for `String::insert()`-style placeholders
+ * to translated messages. Placeholders may be used within the message and replacements provided
+ * directly within the `options` argument.
+ *
+ * Usage:
+ * {{{
+ * Message::translate('Your {:color} paintings are looking just great.', array(
+ * 'color' => 'silver',
+ * 'locale' => 'fr'
+ * ));
+ * }}}
+ *
+ * @see lithium\util\String::insert()
+ */
+Message::applyFilter('translate', function($self, $params, $chain) {
+ return String::insert($chain->next($self, $params, $chain), $params['options']);
+});
+
?>
View
96 libraries/lithium/g11n/Message.php
@@ -9,7 +9,6 @@
namespace lithium\g11n;
use \lithium\core\Environment;
-use \lithium\util\String;
use \lithium\g11n\Locale;
use \lithium\g11n\Catalog;
@@ -49,83 +48,66 @@
class Message extends \lithium\core\StaticObject {
/**
- * Returns the translation of a message according to the current or provided locale
- * and (if applicable) plural form. The method can be used for both single message
- * or messages with a plural form. The provided message will be used as a fall back
- * if it isn't translateable. You may also use `String::insert()`-style placeholders
- * within message strings and provide replacements as a separate option.
+ * Translates a message according to the current or provided locale
+ * and into it's correct plural form.
*
* Usage:
* {{{
* Message::translate('Mind the gap.');
- * Message::translate('house', array(
- * 'plural' => 'houses', 'count' => 23
- * ));
- * Message::translate('Your {:color} paintings are looking just great.', array(
- * 'replace' => array('color' => 'silver'),
- * 'locale' => 'de'
- * ));
+ * Message::translate('house', array('count' => 23));
* }}}
*
- * @param string $singular Either a single or the singular form of the message.
- * @param array $replace An array with replacements for placeholders.
- * @param array $options Allowed keys are:
- * - `'plural'`: Used as a fall back if needed.
+ * @param string $id The id to use when looking up the translation.
+ * @param array $options Valid options are:
* - `'count'`: Used to determine the correct plural form.
* - `'locale'`: The target locale, defaults to current locale.
* - `'scope'`: The scope of the message.
- * @return string
+ * @return string|void The translation or `null` if none could be found.
+ * @filter
*/
- public static function translate($singular, $replace = array(), $options = array()) {
- $defaults = array(
- 'plural' => null,
- 'count' => 1,
- // 'locale' => Environment::get('G11n.locale')
- 'locale' => 'de',
- 'scope' => null
- );
- extract($options + $defaults);
-
- if (!$translated = static::_translated($singular, $locale, $count, $scope)) {
- $translated = $count == 1 ? $singular : $plural;
- }
- return String::insert($translated, $replace);
+ public static function translate($id, $options = array()) {
+ $params = array($id, $options);
+ return static::_filter(__METHOD__, $params, function($self, $params, $chain) {
+ return $self::invokeMethod('_translated', $params);
+ });
}
/**
- * Retrieves the translation for a message ID. Uses the `Catalog` class to
- * access translation data and determines the correct plural form (if applicable).
+ * Retrieves translations through the `Catalog` class by using `$id` as the lookup
+ * key and taking the current or - if specified - the provided locale as well as the
+ * scope into account. Hereupon the correct plural form is determined by passing the
+ * value of the `'count'` option to a closure.
*
- * @param string $id The message ID.
- * @param string $locale The target locale.
- * @param integer $count Used to determine the correct plural form.
- * @param string $scope The scope of the message ID.
- * @return string|void The translated message or `null` if `$singular` is not
- * translateable or a plural rule couldn't be found.
+ * @param string $id The lookup key.
+ * @param array $options Valid options are:
+ * - `'count'`: Used to determine the correct plural form.
+ * - `'locale'`: The target locale, defaults to current locale.
+ * - `'scope'`: The scope of the message.
+ * @return string|void The translation or `null` if none could be found or the plural
+ * form could not be determined.
* @see lithium\g11n\Catalog
* @todo Message pages need caching.
*/
- protected static function _translated($id, $locale, $count = null, $scope = null) {
- $result = Catalog::read('message.page', $locale, compact('scope'));
+ protected static function _translated($id, $options = array()) {
+ $defaults = array(
+ 'count' => 1,
+ // 'locale' => Environment::get('g11n.locale'),
+ 'locale' => null,
+ 'scope' => null
+ );
+ extract($options + $defaults);
+
+ $page = Catalog::read('message.page', $locale, compact('scope'));
+ $plural = Catalog::read('message.plural', $locale);
- if (empty($result[$locale][$id]['translated'])) {
+ if (empty($page[$locale][$id]['translated']) || !isset($plural[$locale])) {
return null;
}
- $translated = $result[$locale][$id]['translated'];
-
- if (isset($count)) {
- $result = Catalog::read('message.plural', $locale);
-
- if (!isset($result[$locale])) {
- return null;
- }
- $key = $result[$locale]($count);
+ $translated = $page[$locale][$id]['translated'];
+ $key = $plural[$locale]($count);
- if (isset($translated[$key])) {
- return $translated[$key];
- }
- } else {
- return array_shift($translated);
+ if (isset($translated[$key])) {
+ return $translated[$key];
}
}
}
View
8 libraries/lithium/tests/cases/g11n/MessageTest.php
@@ -32,7 +32,7 @@ public function tearDown() {
public function testTranslate() {
$data = array(
- 'de' => function($n) { return $n == 1 ? 0 : 1; }
+ 'root' => function($n) { return $n == 1 ? 0 : 1; }
);
Catalog::write('message.plural', $data, array('name' => 'runtime'));
@@ -44,15 +44,15 @@ public function testTranslate() {
Catalog::write('message.page', $data, array('name' => 'runtime'));
$expected = 'Kuchen';
- $result = Message::translate('lithium', null, array('locale' => 'de'));
+ $result = Message::translate('lithium', array('locale' => 'de'));
$this->assertEqual($expected, $result);
$expected = 'Haus';
- $result = Message::translate('house', null, array('locale' => 'de'));
+ $result = Message::translate('house', array('locale' => 'de'));
$this->assertEqual($expected, $result);
$expected = 'Häuser';
- $result = Message::translate('house', null, array('locale' => 'de', 'count' => 5));
+ $result = Message::translate('house', array('locale' => 'de', 'count' => 5));
$this->assertEqual($expected, $result);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.