1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150

base / i18n / message_formatter.cc [blame]

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/i18n/message_formatter.h"

#include <string_view>

#include "base/check.h"
#include "base/i18n/unicodestring.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/time/time.h"
#include "third_party/icu/source/common/unicode/unistr.h"
#include "third_party/icu/source/common/unicode/utypes.h"
#include "third_party/icu/source/i18n/unicode/fmtable.h"
#include "third_party/icu/source/i18n/unicode/msgfmt.h"

using icu::UnicodeString;

namespace base {
namespace i18n {
namespace {
UnicodeString UnicodeStringFromStringView(std::string_view str) {
  return UnicodeString::fromUTF8(
      std::string_view(str.data(), base::checked_cast<int32_t>(str.size())));
}
}  // anonymous namespace

namespace internal {
MessageArg::MessageArg() : formattable(nullptr) {}

MessageArg::MessageArg(const char* s)
    : formattable(new icu::Formattable(UnicodeStringFromStringView(s))) {}

MessageArg::MessageArg(std::string_view s)
    : formattable(new icu::Formattable(UnicodeStringFromStringView(s))) {}

MessageArg::MessageArg(const std::string& s)
    : formattable(new icu::Formattable(UnicodeString::fromUTF8(s))) {}

MessageArg::MessageArg(const std::u16string& s)
    : formattable(new icu::Formattable(UnicodeString(s.data(), s.size()))) {}

MessageArg::MessageArg(int i) : formattable(new icu::Formattable(i)) {}

MessageArg::MessageArg(int64_t i) : formattable(new icu::Formattable(i)) {}

MessageArg::MessageArg(double d) : formattable(new icu::Formattable(d)) {}

MessageArg::MessageArg(const Time& t)
    : formattable(new icu::Formattable(
          static_cast<UDate>(t.InMillisecondsFSinceUnixEpoch()))) {}

MessageArg::~MessageArg() = default;

// Tests if this argument has a value, and if so increments *count.
bool MessageArg::has_value(int *count) const {
  if (formattable == nullptr)
    return false;

  ++*count;
  return true;
}

}  // namespace internal

std::u16string MessageFormatter::FormatWithNumberedArgs(
    std::u16string_view msg,
    const internal::MessageArg& arg0,
    const internal::MessageArg& arg1,
    const internal::MessageArg& arg2,
    const internal::MessageArg& arg3,
    const internal::MessageArg& arg4,
    const internal::MessageArg& arg5,
    const internal::MessageArg& arg6) {
  int32_t args_count = 0;
  icu::Formattable args[] = {
      arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
      arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
      arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
      arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
      arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
      arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
      arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
  };

  UnicodeString msg_string(msg.data(), msg.size());
  UErrorCode error = U_ZERO_ERROR;
  icu::MessageFormat format(msg_string,  error);
  icu::UnicodeString formatted;
  icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
  format.format(args, args_count, formatted, ignore, error);
  if (U_FAILURE(error)) {
    LOG(ERROR) << "MessageFormat(" << msg << ") failed with "
               << u_errorName(error);
    return std::u16string();
  }
  return i18n::UnicodeStringToString16(formatted);
}

std::u16string MessageFormatter::FormatWithNamedArgs(
    std::u16string_view msg,
    std::string_view name0,
    const internal::MessageArg& arg0,
    std::string_view name1,
    const internal::MessageArg& arg1,
    std::string_view name2,
    const internal::MessageArg& arg2,
    std::string_view name3,
    const internal::MessageArg& arg3,
    std::string_view name4,
    const internal::MessageArg& arg4,
    std::string_view name5,
    const internal::MessageArg& arg5,
    std::string_view name6,
    const internal::MessageArg& arg6) {
  icu::UnicodeString names[] = {
      UnicodeStringFromStringView(name0), UnicodeStringFromStringView(name1),
      UnicodeStringFromStringView(name2), UnicodeStringFromStringView(name3),
      UnicodeStringFromStringView(name4), UnicodeStringFromStringView(name5),
      UnicodeStringFromStringView(name6),
  };
  int32_t args_count = 0;
  icu::Formattable args[] = {
      arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
      arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
      arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
      arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
      arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
      arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
      arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
  };

  UnicodeString msg_string(msg.data(), msg.size());
  UErrorCode error = U_ZERO_ERROR;
  icu::MessageFormat format(msg_string, error);

  icu::UnicodeString formatted;
  format.format(names, args, args_count, formatted, error);
  if (U_FAILURE(error)) {
    LOG(ERROR) << "MessageFormat(" << msg << ") failed with "
               << u_errorName(error);
    return std::u16string();
  }
  return i18n::UnicodeStringToString16(formatted);
}

}  // namespace i18n
}  // namespace base