Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
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
6
7#include <string>
8#include <vector>
9
10#include "rapidjson/document.h"
11#include "rapidjson/stringbuffer.h"
12#include "rapidjson/writer.h"
13#include "third_party/icu/source/common/unicode/locid.h"
14#include "third_party/icu/source/common/unicode/stringpiece.h"
15
16using icu::Locale;
17
18namespace flutter_runner {
19
20using fuchsia::intl::Profile;
21
23 const Profile& intl_profile) {
24 rapidjson::Document document;
25 auto& allocator = document.GetAllocator();
26 document.SetObject();
27 document.AddMember("method", "setLocale", allocator);
28 rapidjson::Value args(rapidjson::kArrayType);
29
30 for (const auto& locale_id : intl_profile.locales()) {
31 UErrorCode error_code = U_ZERO_ERROR;
32 icu::Locale locale = icu::Locale::forLanguageTag(locale_id.id, error_code);
33 if (U_FAILURE(error_code)) {
34 FML_LOG(ERROR) << "Error parsing locale ID \"" << locale_id.id << "\"";
35 continue;
36 }
37 args.PushBack(rapidjson::Value().SetString(locale.getLanguage(), allocator),
38 allocator);
39
40 auto country = locale.getCountry() != nullptr ? locale.getCountry() : "";
41 args.PushBack(rapidjson::Value().SetString(country, allocator), allocator);
42
43 auto script = locale.getScript() != nullptr ? locale.getScript() : "";
44 args.PushBack(rapidjson::Value().SetString(script, allocator), allocator);
45
46 std::string variant =
47 locale.getVariant() != nullptr ? locale.getVariant() : "";
48 // ICU4C capitalizes the variant for backward compatibility, even though
49 // the preferred form is lowercase. So we lowercase here.
50 std::transform(begin(variant), end(variant), begin(variant),
51 [](unsigned char c) { return std::tolower(c); });
52 args.PushBack(rapidjson::Value().SetString(variant, allocator), allocator);
53 }
54
55 document.AddMember("args", args, allocator);
56
57 rapidjson::StringBuffer buffer;
58 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
59 document.Accept(writer);
60 auto data = reinterpret_cast<const uint8_t*>(buffer.GetString());
61 return fml::MallocMapping::Copy(data, buffer.GetSize());
62}
63
64} // 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
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
#define FML_LOG(severity)
Definition logging.h:101
std::shared_ptr< ImpellerAllocator > allocator
fml::MallocMapping MakeLocalizationPlatformMessageData(const Profile &intl_profile)
const size_t end