Flutter Engine
fuchsia_intl.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "fuchsia_intl.h"
6 
7 #include <sstream>
8 #include <string>
9 #include <vector>
10 
11 #include "loop.h"
12 #include "rapidjson/document.h"
13 #include "rapidjson/stringbuffer.h"
14 #include "rapidjson/writer.h"
15 #include "runner.h"
17 #include "third_party/icu/source/common/unicode/bytestream.h"
18 #include "third_party/icu/source/common/unicode/errorcode.h"
19 #include "third_party/icu/source/common/unicode/locid.h"
20 #include "third_party/icu/source/common/unicode/strenum.h"
21 #include "third_party/icu/source/common/unicode/stringpiece.h"
22 #include "third_party/icu/source/common/unicode/uloc.h"
23 
24 using icu::Locale;
25 
26 namespace flutter_runner {
27 
28 using fuchsia::intl::Profile;
29 
31  const Profile& intl_profile) {
32  rapidjson::Document document;
33  auto& allocator = document.GetAllocator();
34  document.SetObject();
35  document.AddMember("method", "setLocale", allocator);
36  rapidjson::Value args(rapidjson::kArrayType);
37 
38  for (const auto& locale_id : intl_profile.locales()) {
39  UErrorCode error_code = U_ZERO_ERROR;
40  icu::Locale locale = icu::Locale::forLanguageTag(locale_id.id, error_code);
41  if (U_FAILURE(error_code)) {
42  FML_LOG(ERROR) << "Error parsing locale ID \"" << locale_id.id << "\"";
43  continue;
44  }
45  args.PushBack(rapidjson::Value().SetString(locale.getLanguage(), allocator),
46  allocator);
47 
48  auto country = locale.getCountry() != nullptr ? locale.getCountry() : "";
49  args.PushBack(rapidjson::Value().SetString(country, allocator), allocator);
50 
51  auto script = locale.getScript() != nullptr ? locale.getScript() : "";
52  args.PushBack(rapidjson::Value().SetString(script, allocator), allocator);
53 
54  std::string variant =
55  locale.getVariant() != nullptr ? locale.getVariant() : "";
56  // ICU4C capitalizes the variant for backward compatibility, even though
57  // the preferred form is lowercase. So we lowercase here.
58  std::transform(begin(variant), end(variant), begin(variant),
59  [](unsigned char c) { return std::tolower(c); });
60  args.PushBack(rapidjson::Value().SetString(variant, allocator), allocator);
61  }
62 
63  document.AddMember("args", args, allocator);
64 
65  rapidjson::StringBuffer buffer;
66  rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
67  document.Accept(writer);
68  auto data = reinterpret_cast<const uint8_t*>(buffer.GetString());
69  return std::vector<uint8_t>(data, data + buffer.GetSize());
70 }
71 
72 } // namespace flutter_runner
G_BEGIN_DECLS FlValue * args
std::vector< uint8_t > MakeLocalizationPlatformMessageData(const Profile &intl_profile)
Definition: fuchsia_intl.cc:30
#define FML_LOG(severity)
Definition: logging.h:65