Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 "rapidjson/document.h"
12#include "rapidjson/stringbuffer.h"
13#include "rapidjson/writer.h"
14#include "runner.h"
16#include "third_party/icu/source/common/unicode/bytestream.h"
17#include "third_party/icu/source/common/unicode/errorcode.h"
18#include "third_party/icu/source/common/unicode/locid.h"
19#include "third_party/icu/source/common/unicode/strenum.h"
20#include "third_party/icu/source/common/unicode/stringpiece.h"
21#include "third_party/icu/source/common/unicode/uloc.h"
22
23using icu::Locale;
24
25namespace flutter_runner {
26
27using fuchsia::intl::Profile;
28
30 const Profile& intl_profile) {
31 rapidjson::Document document;
32 auto& allocator = document.GetAllocator();
33 document.SetObject();
34 document.AddMember("method", "setLocale", allocator);
35 rapidjson::Value args(rapidjson::kArrayType);
36
37 for (const auto& locale_id : intl_profile.locales()) {
38 UErrorCode error_code = U_ZERO_ERROR;
39 icu::Locale locale = icu::Locale::forLanguageTag(locale_id.id, error_code);
40 if (U_FAILURE(error_code)) {
41 FML_LOG(ERROR) << "Error parsing locale ID \"" << locale_id.id << "\"";
42 continue;
43 }
44 args.PushBack(rapidjson::Value().SetString(locale.getLanguage(), allocator),
45 allocator);
46
47 auto country = locale.getCountry() != nullptr ? locale.getCountry() : "";
48 args.PushBack(rapidjson::Value().SetString(country, allocator), allocator);
49
50 auto script = locale.getScript() != nullptr ? locale.getScript() : "";
51 args.PushBack(rapidjson::Value().SetString(script, allocator), allocator);
52
53 std::string variant =
54 locale.getVariant() != nullptr ? locale.getVariant() : "";
55 // ICU4C capitalizes the variant for backward compatibility, even though
56 // the preferred form is lowercase. So we lowercase here.
57 std::transform(begin(variant), end(variant), begin(variant),
58 [](unsigned char c) { return std::tolower(c); });
59 args.PushBack(rapidjson::Value().SetString(variant, allocator), allocator);
60 }
61
62 document.AddMember("args", args, allocator);
63
64 rapidjson::StringBuffer buffer;
65 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
66 document.Accept(writer);
67 auto data = reinterpret_cast<const uint8_t*>(buffer.GetString());
68 return fml::MallocMapping::Copy(data, buffer.GetSize());
69}
70
71} // namespace flutter_runner
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition mapping.h:144
static MallocMapping Copy(const T *begin, const T *end)
Definition mapping.h:162
static const char * begin(const StringSlice &s)
Definition editor.cpp:252
glong glong end
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
#define FML_LOG(severity)
Definition logging.h:82
fml::MallocMapping MakeLocalizationPlatformMessageData(const Profile &intl_profile)
#define ERROR(message)