From 6c51bde9db85fa63adbddf2240a521b437bbb06a Mon Sep 17 00:00:00 2001 From: wei <> Date: Thu, 1 Jun 2006 03:53:17 +0000 Subject: Add Parameters property to TTranslate --- .../quickstart/protected/pages/Advanced/I18N.page | 6 +- framework/I18N/TTranslate.php | 78 ++++++++++++++++++++-- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/demos/quickstart/protected/pages/Advanced/I18N.page b/demos/quickstart/protected/pages/Advanced/I18N.page index 5b1fafa0..6c86a6c9 100644 --- a/demos/quickstart/protected/pages/Advanced/I18N.page +++ b/demos/quickstart/protected/pages/Advanced/I18N.page @@ -148,9 +148,11 @@ To translate a message or string in the template, use TTranslate.

<com:TTranslate Text="Goodbye" /> -

TTranslate can also perform string substitution. Any attributes of TTranslate will be substituted with {attribute name} in the translation. E.g.

+

TTranslate can also perform string substitution. +The Parameters property can be use to add name values pairs for substitution. Substrings in the translation enclosed with "{" and "}" are consider as the + parameter names during substitution lookup. The following example will substitute the substring "{time}" with the value of the parameter attribute "Parameters.time=<%= time() %>". -<com:TTranslate time="late"> +<com:TTranslate Parameters.time=<%= time() %> > The time is {time}. </com:TTranslate> diff --git a/framework/I18N/TTranslate.php b/framework/I18N/TTranslate.php index 6a30f3a6..da5876d4 100644 --- a/framework/I18N/TTranslate.php +++ b/framework/I18N/TTranslate.php @@ -28,19 +28,19 @@ Prado::using('System.I18N.TI18NControl'); * Depending on the culture set on the page, the phrase "Goodbye" will * be translated. * - * The values of any attribute in TTranslate are consider as values for - * substitution. Strings enclosed with "{" and "}" are consider as the - * parameters. The following example will substitution the string - * "{time}" with the value of the attribute "time="#time()". Note that - * the value of the attribute time is evaluated. + * The {@link getParameters Parameters} property can be use to add name values pairs for + * substitution. Substrings enclosed with "{" and "}" in the translation message are consider as the + * parameter names during substitution lookup. The following example will substitute the substring + * "{time}" with the value of the parameter attribute "Parameters.time=<%= time() %>. Note that + * the value of the parameter named "time" is evaluated. * - * + * > * The unix-time is "{time}". * * * * More complex string substitution can be applied using the - * TParam component. + * TTranslateParameter component. * * Namespace: System.I18N * @@ -133,6 +133,68 @@ class TTranslate extends TI18NControl return $this->getViewState('Trim',true); } + /** + * Returns the list of custom parameters. + * Custom parameters are name-value pairs that may subsititute translation + * place holders during rendering. + * @return TAttributeCollection the list of custom parameters + */ + public function getParameters() + { + if($parameters=$this->getViewState('Parameters',null)) + return $parameters; + else + { + $parameters=new TAttributeCollection; + $this->setViewState('Parameters',$parameters,null); + return $parameters; + } + } + + /** + * @return boolean whether the named parameter exists + */ + public function hasParameter($name) + { + if($parameters=$this->getViewState('Parameters',null)) + return $parameters->contains($name); + else + return false; + } + + /** + * @return string parameter value, null if parameter does not exist + */ + public function getParameter($name) + { + if($parameters=$this->getViewState('Parameters',null)) + return $parameters->itemAt($name); + else + return null; + } + + /** + * @param string parameter name + * @param string value of the parameter + */ + public function setParameter($name,$value) + { + $this->getParameters()->add($name,$value); + } + + /** + * Removes the named parameter. + * @param string the name of the parameter to be removed. + * @return string parameter value removed, null if parameter does not exist. + */ + public function removeParameter($name) + { + if($parameters=$this->getViewState('Parameters',null)) + return $parameters->remove($name); + else + return null; + } + /** * renders the translated string. */ @@ -141,6 +203,8 @@ class TTranslate extends TI18NControl $textWriter=new TTextWriter; $htmlWriter=new THtmlWriter($textWriter); $subs = array(); + foreach($this->getParameters() as $key => $value) + $subs['{'.$key.'}'] = $value; foreach($this->getControls() as $control) { if($control instanceof TTranslateParameter) -- cgit v1.2.3