[Solar-svn] Revision 2565

pmjones at solarphp.com pmjones at solarphp.com
Mon Jul 2 15:36:43 CDT 2007


Solar_Base and Solar_Locale
---------------------------

* The $replace param may now be an associative array.  If so, the keys are treated as named
  placeholders in the locale string, and are replaced with the values when those keys appear
  in the locale string, like so: "{:key}" (uses 'key' from the replacement array).





Modified: trunk/Solar/Base.php
===================================================================
--- trunk/Solar/Base.php	2007-07-01 12:41:13 UTC (rev 2564)
+++ trunk/Solar/Base.php	2007-07-02 20:36:43 UTC (rev 2565)
@@ -192,13 +192,36 @@
      * This is a convenient shortcut for calling [[Solar::$locale]]->fetch()
      * that automatically uses the current class name.
      * 
+     * You can also pass an array of replacement values.  If the `$replace`
+     * array is sequential, this method will use it with vsprintf(); if the
+     * array is associative, this method will replace "{:key}" with the array
+     * value.
+     * 
+     * For example:
+     * 
+     * {{code: php
+     *     $page  = 2;
+     *     $pages = 10;
+     *     
+     *     // given a locale string TEXT_PAGES => 'Page %d of %d'
+     *     $replace = array($page, $pages);
+     *     return $this->locale('Solar_Example', 'TEXT_PAGES',
+     *         $pages, $replace);
+     *     // returns "Page 2 of 10"
+     *     
+     *     // given a locale string TEXT_PAGES => 'Page {:page} of {:pages}'
+     *     $replace = array('page' => $page, 'pages' => $pages);
+     *     return $this->locale('Solar_Example', 'TEXT_PAGES',
+     *         $pages, $replace);
+     *     // returns "Page 2 of 10"
+     * }}
+     * 
      * @param string $key The key to get a locale string for.
      * 
      * @param string $num If 1, returns a singular string; otherwise, returns
      * a plural string (if one exists).
      * 
-     * @param array $replace An array of replacement values for the string, to
-     * be applied using [[php::vsprintf() | ]].
+     * @param array $replace An array of replacement values for the string.
      * 
      * @return string The locale string, or the original $key if no
      * string found.

Modified: trunk/Solar/Locale.php
===================================================================
--- trunk/Solar/Locale.php	2007-07-01 12:41:13 UTC (rev 2564)
+++ trunk/Solar/Locale.php	2007-07-02 20:36:43 UTC (rev 2565)
@@ -109,6 +109,33 @@
      * 
      * Loads translations as needed.
      * 
+     * You can also pass an array of replacement values.  If the `$replace`
+     * array is sequential, this method will use it with vsprintf(); if the
+     * array is associative, this method will replace "{:key}" with the array
+     * value.
+     * 
+     * For example:
+     * 
+     * {{code: php
+     *     $page  = 2;
+     *     $pages = 10;
+     *     
+     *     // given a class of 'Solar_Example' with a locale string
+     *     // TEXT_PAGES => 'Page %d of %d', uses vsprintf() internally:
+     *     $replace = array($page, $pages);
+     *     echo Solar::$locale->fetch('Solar_Example', 'TEXT_PAGES',
+     *         $pages, $replace);
+     *     // echo "Page 2 of 10"
+     *     
+     *     // given a class of 'Solar_Example' with a locale string
+     *     // TEXT_PAGES => 'Page {:page} of {:pages}', uses str_replace()
+     *     // internally:
+     *     $replace = array('page' => $page, 'pages' => $pages);
+     *     echo Solar::$locale->fetch('Solar_Example', 'TEXT_PAGES',
+     *         $pages, $replace);
+     *     // echo "Page 2 of 10"
+     * }}
+     * 
      * @param string|object $spec The class name (or object) for the translation.
      * 
      * @param string $key The translation key.
@@ -116,11 +143,12 @@
      * @param mixed $num Helps determine whether to get a singular
      * or plural translation.
      * 
-     * @param array $replace An array of replacement values for the string, to
-     * be applied using [[php::vsprintf() | ]].
+     * @param array $replace An array of replacement values for the string.
      * 
      * @return string A translated locale string.
      * 
+     * @see _trans()
+     * 
      * @see Solar_Base::locale()
      * 
      * @see Manual::Solar/Using_locales
@@ -193,8 +221,7 @@
      * @param mixed $num Helps determine if we need a singular or plural
      * translation.
      * 
-     * @param array $replace An array of replacement values for the string, to
-     * be applied using [[php::vsprintf() | ]].
+     * @param array $replace An array of replacement values for the string.
      * 
      * @return string The translation string if it exists, or null if it
      * does not.
@@ -219,9 +246,19 @@
             $string = $trans[0];
         }
         
-        // do replacements
+        // do replacements?
         if ($replace) {
-            $string = vsprintf($string, (array) $replace);
+            // by vsprintf(), or by str_replace?()
+            $key = key($replace);
+            if (is_int($key)) {
+                // sequential array, use vsprintf()
+                $string = vsprintf($string, (array) $replace);
+            } else {
+                // associative array, use str_replace()
+                foreach ($replace as $key => $val) {
+                    $string = str_replace("{:$key}", $val, $string);
+                }
+            }
         }
         
         // done!




More information about the Solar-svn mailing list