Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring `template\View` to remove dependency on `g11n\Media`. Con…

…tent filters are now applied in `config/bootstrap/g11n.php`. Also implementing `render()` in all subclasses of `view\Renderer`.
  • Loading branch information...
commit f3015b0ca42d0faf493996f696af07d786978468 1 parent d6ce4ac
@nateabele nateabele authored gwoo committed
View
12 app/config/bootstrap/g11n.php
@@ -8,6 +8,7 @@
use \lithium\g11n\Message;
use \lithium\util\String;
+use \lithium\http\Media;
/**
* Implements logic for handling cases where `Message::translate()` returns without a result.
@@ -38,6 +39,17 @@
return String::insert($chain->next($self, $params, $chain), $params['options']);
});
+/**
+ * Embeds message translation content filters into the `View` class (or other content handler,
+ * if specified) when content is rendered. This enables short-hand translation functions, i.e.
+ * `<?=$t("Translated content"); ?>`.
+ */
+Media::applyFilter('_handle', function($self, $params, $chain) {
+ $params['handler'] += array('outputFilters' => array());
+ $params['handler']['outputFilters'] += Message::contentFilters();
+ return $chain->next($self, $params, $chain);
+});
+
/*
* Inflector configuration examples. If your application has custom singular or plural rules, or
* extra non-ASCII characters to transliterate, you can configure that by uncommenting the lines
View
22 libraries/lithium/g11n/Message.php
@@ -72,6 +72,28 @@ public static function translate($id, $options = array()) {
});
}
+
+ /**
+ * Returns an array containing named closures which are used to embed short-hand aliases for
+ * `translate()` in the templating layer.
+ *
+ * @return array Returns an array containing named short-hand translation functions wrapped as
+ * closures.
+ */
+ public static function contentFilters() {
+ $t = function($message, $options = array()) {
+ return Message::translate($message, $options + array(
+ 'default' => $message
+ ));
+ };
+ $tn = function($message1, $message2, $count, $options = array()) {
+ return Message::translate($message1, $options + compact('count') + array(
+ 'default' => $count == 1 ? $message1 : $message2
+ ));
+ };
+ return compact('t', 'tn');
+ }
+
/**
* 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
View
13 libraries/lithium/template/View.php
@@ -10,7 +10,6 @@
use \RuntimeException;
use \lithium\core\Libraries;
-use \lithium\g11n\Message;
class View extends \lithium\core\Object {
@@ -59,17 +58,7 @@ protected function _init() {
$h = function($data) use (&$h) {
return is_array($data) ? array_map($h, $data) : htmlspecialchars((string) $data);
};
- $t = function($message, $options = array()) {
- return Message::translate($message, $options + array(
- 'default' => $message
- ));
- };
- $tn = function($message1, $message2, $count, $options = array()) {
- return Message::translate($message1, $options + compact('count') + array(
- 'default' => $count == 1 ? $message1 : $message2
- ));
- };
- $this->outputFilters += compact('h', 't', 'tn');
+ $this->outputFilters += compact('h');
}
public function render($type, $data = array(), $options = array()) {
View
8 libraries/lithium/tests/cases/template/ViewTest.php
@@ -9,9 +9,10 @@
namespace lithium\tests\cases\template;
use \lithium\template\View;
-use \lithium\template\view\adapter\Simple;
use \lithium\g11n\Catalog;
+use \lithium\g11n\Message;
use \lithium\g11n\catalog\adapter\Memory;
+use \lithium\template\view\adapter\Simple;
class TestViewClass extends \lithium\template\View {
@@ -63,8 +64,9 @@ public function testTranslationOutputFilters() {
);
Catalog::write('message', 'de', $data, array('name' => 'runtime'));
- $t = $this->_view->outputFilters['t'];
- $tn = $this->_view->outputFilters['tn'];
+ $filters = Message::contentFilters();
+ $t = $filters['t'];
+ $tn = $filters['tn'];
$expected = 'Haus';
$result = $t('house', array('locale' => 'de'));
View
3  libraries/lithium/tests/mocks/template/MockRenderer.php
@@ -9,6 +9,9 @@
namespace lithium\tests\mocks\template;
class MockRenderer extends \lithium\template\view\Renderer {
+
+ public function render($template, $data = array(), $options = array()) {
+ }
}
?>
View
3  libraries/lithium/tests/mocks/template/helper/MockFormRenderer.php
@@ -19,6 +19,9 @@ public function request() {
}
return $this->_request;
}
+
+ public function render($template, $data = array(), $options = array()) {
+ }
}
?>
View
3  libraries/lithium/tests/mocks/template/helper/MockHtmlRenderer.php
@@ -9,6 +9,9 @@
namespace lithium\tests\mocks\template\helper;
class MockHtmlRenderer extends \lithium\template\view\Renderer {
+
+ public function render($template, $data = array(), $options = array()) {
+ }
}
?>
View
3  libraries/lithium/tests/mocks/util/MockStringObject.php
@@ -10,6 +10,9 @@
class MockStringObject extends \lithium\template\view\Renderer {
+ public function render($template, $data = array(), $options = array()) {
+ }
+
public function __toString() {
return 'custom object';
}
Please sign in to comment.
Something went wrong with that request. Please try again.