Author: dr
Date: Wed Jan 30 16:38:14 2008
New Revision: 7262
Log:
- Implemented issue #9973: Added a translation compiler to convert a string in
the original language, to the translated one without substituting parameters.
This is to have translation support for the Template component.
Modified:
trunk/Translation/ChangeLog
trunk/Translation/src/translation.php
trunk/Translation/tests/translation_test.php
Modified: trunk/Translation/ChangeLog
==============================================================================
--- trunk/Translation/ChangeLog [iso-8859-1] (original)
+++ trunk/Translation/ChangeLog [iso-8859-1] Wed Jan 30 16:38:14 2008
@@ -1,3 +1,12 @@
+1.2alpha1 - [RELEASEDATE]
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+- Implemented issue #9973: Added a translation compiler to convert a string in
+ the original language, to the translated one without substituting
+ parameters. This is to have translation support for the Template
+ component.
+
+
1.1.6 - Wednesday 05 December 2007
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Modified: trunk/Translation/src/translation.php
==============================================================================
--- trunk/Translation/src/translation.php [iso-8859-1] (original)
+++ trunk/Translation/src/translation.php [iso-8859-1] Wed Jan 30 16:38:14 2008
@@ -114,5 +114,83 @@
// exception to tell that there was a missing parameter.
return (string) preg_replace( '@%(([A-Za-z][a-z_]*[a-z])|[1-9])@e',
'$this->parameter_callback("\\1", $params)', $translatedString );
}
+
+ /**
+ * Returns the replacement for the key $key from the parameters $params.
+ *
+ * The params array is an associative array in the form
array('key'=>'value').
+ *
+ * This is a callback function used by the compileTranslation() method for
each
+ * matched parameter in the translated string.
+ *
+ * @param string $key
+ * @param array $params
+ * @return string
+ */
+ private function parameter_callback_compile( $key, array $params )
+ {
+ if ( !isset( $params[strtolower( $key )] ) )
+ {
+ throw new ezcTranslationParameterMissingException( $key );
+ }
+ // We use ctype_upper() here to check if the first character of the key
+ // is an uppercase letter. If it is then we make the first character of
+ // the returned translation also an upper case character. With this
+ // mechanism we can correctly upper case translated strings if word
+ // order changes. See
+ // [EMAIL PROTECTED] ezcTranslationTest::testGetStringWithParameters}
for an
+ // example of this.
+ if ( ctype_upper( $key[0] ) )
+ {
+ $string = "' . ucfirst(". $params[strtolower( $key )] . ") . '";
+ }
+ else
+ {
+ $string = "' . ". $params[strtolower( $key )] . " . '";
+ }
+ return $string;
+ }
+
+ /**
+ * Returns the translated version of the original string $key.
+ *
+ * This method returns a translated string and substitutes the parameters
$param
+ * in the localized string with PHP code to place the variable data into
+ * the string at a later moment. Instead of the values for each of the
+ * parameters, an expression to get to the data should be sumbitted into
+ * the $params array.
+ *
+ * <code>
+ * echo $translation->compileTranslation( "Hello #%nr", array( "nr" =>
'$this->send->nr' ) );
+ * </code>
+ *
+ * Will return something like:
+ * <code>
+ * 'Hallo #' . $this->send->nr . ''
+ * </code>
+ *
+ * @param string $key
+ * @param array(string=>string) $params
+ * @return string
+ */
+ public function compileTranslation( $key, array $params = array() )
+ {
+ if ( !isset( $this->translationMap[$key] ) )
+ {
+ throw new ezcTranslationKeyNotAvailableException( $key );
+ }
+ $translatedString = var_export(
$this->translationMap[$key]->translation, true );
+ // Little optimization to prevent preg if not needed, it bails out too
+ // if there is just a percent sign in the string without a valid
+ // parameter-identifier, but we can live with that.
+ if ( strstr( $translatedString, '%' ) === false )
+ {
+ return $translatedString;
+ }
+ // So we do have a possibility of a parameterized string, replace those
+ // with the parameters. The callback function can actually throw an
+ // exception to tell that there was a missing parameter.
+ return (string) preg_replace( '@%(([A-Za-z][a-z_]*[a-z])|[1-9])@e',
'$this->parameter_callback_compile("\\1", $params)', $translatedString );
+ }
}
?>
Modified: trunk/Translation/tests/translation_test.php
==============================================================================
--- trunk/Translation/tests/translation_test.php [iso-8859-1] (original)
+++ trunk/Translation/tests/translation_test.php [iso-8859-1] Wed Jan 30
16:38:14 2008
@@ -30,6 +30,13 @@
self::assertEquals( 'Dit is een vertaalbare zin', $string );
}
+ public function testCompileExistingString()
+ {
+ $obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
+ $string = $obj->compileTranslation( 'This is a translatable string' );
+ self::assertEquals( "'Dit is een vertaalbare zin'", $string );
+ }
+
public function testGetNonExistingString()
{
$obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
@@ -44,11 +51,32 @@
}
}
+ public function testCompileNonExistingString()
+ {
+ $obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
+ try
+ {
+ $string = $obj->compileTranslation( 'Unknown string' );
+ self::fail( 'Expected exception "Key not available" was not
thrown' );
+ }
+ catch ( ezcTranslationKeyNotAvailableException $e )
+ {
+ self::assertEquals( "The key 'Unknown string' does not exist in
the translation map.", $e->getMessage() );
+ }
+ }
+
public function testGetStringWithParameters()
{
$obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
$string = $obj->getTranslation( '%Apples are not %pears', array(
'apples' => 'appelen', 'pears' => 'peren' ) );
self::assertEquals( 'Peren zijn niet hetzelfde als appelen', $string );
+ }
+
+ public function testCompileStringWithParameters()
+ {
+ $obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
+ $string = $obj->compileTranslation( '%Apples are not %pears', array(
'apples' => '$appelen', 'pears' => '$peren' ) );
+ self::assertEquals( "'' . ucfirst(\$peren) . ' zijn niet hetzelfde als
' . \$appelen . ''", $string );
}
public function testGetStringWithMissingParameters()
@@ -65,6 +93,20 @@
}
}
+ public function testCompileStringWithMissingParameters()
+ {
+ $obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
+ try
+ {
+ $string = $obj->compileTranslation( '%Apples are not %pears',
array( 'apples' => 'appelen' ) );
+ self::fail( 'Expected exception "Parameter missing" was not
thrown' );
+ }
+ catch ( ezcTranslationParameterMissingException $e )
+ {
+ self::assertEquals( "The parameter '%Pears' does not exist.",
$e->getMessage() );
+ }
+ }
+
public function testGetStringWithNumericalParameters()
{
$obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
@@ -72,12 +114,33 @@
self::assertEquals( 'Een koe is geen paard', $string );
}
- public function testGetStringWithMissingNumericalParameters()
+ public function testCompileStringWithNumericalParameters()
+ {
+ $obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
+ $string = $obj->compileTranslation( 'A %1 is not a %2', array( 1 =>
'$koe', 2 => '$paard' ) );
+ self::assertEquals( '\'Een \' . $koe . \' is geen \' . $paard . \'\'',
$string );
+ }
+
+ public function testStringWithMissingNumericalParameters()
{
$obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
try
{
$string = $obj->getTranslation( 'A %1 is not a %2', array( 1 =>
'koe' ) );
+ self::fail( 'Expected exception "Parameter missing" was not
thrown' );
+ }
+ catch ( ezcTranslationParameterMissingException $e )
+ {
+ self::assertEquals( "The parameter '%2' does not exist.",
$e->getMessage() );
+ }
+ }
+
+ public function testCompileStringWithMissingNumericalParameters()
+ {
+ $obj = new ezcTranslation( ezcTranslationTest::setUpTestArray() );
+ try
+ {
+ $string = $obj->compileTranslation( 'A %1 is not a %2', array( 1
=> 'koe' ) );
self::fail( 'Expected exception "Parameter missing" was not
thrown' );
}
catch ( ezcTranslationParameterMissingException $e )
--
svn-components mailing list
[EMAIL PROTECTED]
http://lists.ez.no/mailman/listinfo/svn-components