Beispiel #1
0
 /**
  * Translate a native string in a locale string.
  *
  * @param string $native The native string to translate.
  * @param array $args
  * @param array $options
  *
  * @return string The translated string, or the same native string if no translation could be
  * found.
  */
 public function __invoke($native, array $args = array(), array $options = array())
 {
     $native = (string) $native;
     $messages = $this->messages;
     $translated = null;
     $suffix = null;
     if ($args && array_key_exists(':count', $args)) {
         $count = $args[':count'];
         if ($count == 0) {
             $suffix = '.none';
         } else {
             if ($count == 1) {
                 $suffix = '.one';
             } else {
                 $suffix = '.other';
             }
         }
     }
     $scope = I18n::get_scope();
     if (isset($options['scope'])) {
         if ($scope) {
             $scope .= '.';
         }
         $scope .= is_array($options['scope']) ? implode('.', $options['scope']) : $options['scope'];
     }
     $prefix = $scope;
     while ($scope) {
         $try = $scope . '.' . $native . $suffix;
         if (isset($messages[$try])) {
             $translated = $messages[$try];
             break;
         }
         $pos = strpos($scope, '.');
         if ($pos === false) {
             break;
         }
         $scope = substr($scope, $pos + 1);
     }
     if (!$translated) {
         if (isset($messages[$native . $suffix])) {
             $translated = $messages[$native . $suffix];
         }
     }
     if (!$translated) {
         self::$missing[] = ($prefix ? $prefix . '.' : '') . $native;
         if (!empty($options['default'])) {
             $default = $options['default'];
             if (!$default instanceof \Closure) {
                 return $default;
             }
             $native = $default($this, $native, $options, $args) ?: $native;
         }
         #
         # We couldn't find any translation for the native string provided, in order to avoid
         # another search for the same string, we store the native string as the translation in
         # the locale messages.
         #
         $this->messages[($prefix ? $prefix . '.' : '') . $native] = $native;
         $translated = $native;
     }
     if ($args) {
         $translated = \ICanBoogie\format($translated, $args);
     }
     return $translated;
 }