public function translate()

in src/internationalization/PhutilTranslator.php [75:158]


  public function translate($text /* , ... */) {
    $args = func_get_args();

    if ($this->willTranslateCallback) {
      call_user_func_array($this->willTranslateCallback, $args);
    }

    if (isset($this->translations[$text])) {
      $translation = $this->translations[$text];
    } else {
      $translation = $text;
    }

    while (is_array($translation)) {
      $arg = next($args);
      $translation = $this->chooseVariant($translation, $arg);
      if ($translation === null) {
        $pos = key($args);

        if (is_object($arg)) {
          $kind = get_class($arg);
        } else {
          $kind = gettype($arg);
        }

        return sprintf(
          '[Invalid Translation!] The "%s" language data offers variant '.
          'translations for the plurality or gender of argument %s, but '.
          'the value for that argument is not an integer, PhutilNumber, or '.
          'PhutilPerson (it is a value of type "%s"). Raw input: <%s>.',
          $this->localeCode,
          $pos,
          $kind,
          $text);
      }
    }
    array_shift($args);

    foreach ($args as $k => $arg) {
      if ($arg instanceof PhutilNumber) {
        $args[$k] = $this->formatNumber($arg->getNumber(), $arg->getDecimals());
      }
    }

    // Check if any arguments are PhutilSafeHTML. If they are, we will apply
    // any escaping necessary and output HTML.
    $is_html = false;
    foreach ($args as $arg) {
      if ($arg instanceof PhutilSafeHTML ||
          $arg instanceof PhutilSafeHTMLProducerInterface) {
        $is_html = true;
        break;
      }
    }

    if ($is_html) {
      foreach ($args as $k => $arg) {
        $args[$k] = (string)phutil_escape_html($arg);
      }
    }

    $result = vsprintf($translation, $args);
    if ($result === false) {
      // If vsprintf() fails (often because the translated string references
      // too many parameters), show the bad template with a note instead of
      // returning an empty string. This makes it easier to figure out what
      // went wrong and fix it.
      $result = pht('[Invalid Translation!] %s', $translation);
    }

    if ($this->shouldPostProcess) {
      $result = $this->locale->didTranslateString(
        $text,
        $translation,
        $args,
        $result);
    }

    if ($is_html) {
      $result = phutil_safe_html($result);
    }

    return $result;
  }