Skip to content

Commit b81ebfd

Browse files
committed
Add localization_channel unit test
Signed-off-by: MuHong Byun <[email protected]>
1 parent fee84ca commit b81ebfd

File tree

6 files changed

+200
-149
lines changed

6 files changed

+200
-149
lines changed

shell/platform/tizen/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ template("embedder_for_profile") {
128128
sources = _flutter_tizen_source
129129
sources += [
130130
"channels/localization_channel.cc",
131+
"channels/localization_channel_device.cc",
131132
"channels/platform_channel.cc",
132133
"channels/settings_channel.cc",
133134
"channels/settings_channel_device.cc",
@@ -244,6 +245,7 @@ template("embedder_executable") {
244245

245246
sources = _flutter_tizen_source
246247
sources += [
248+
"channels/localization_channel.cc",
247249
"channels/localization_channel_stub.cc",
248250
"channels/platform_channel_stub.cc",
249251
"channels/settings_channel.cc",

shell/platform/tizen/channels/localization_channel.cc

Lines changed: 4 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,22 @@
44

55
#include "localization_channel.h"
66

7-
#include <utils_i18n.h>
8-
9-
#include <vector>
10-
117
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
128
#include "flutter/shell/platform/tizen/tizen_log.h"
139
#include "rapidjson/document.h"
1410
#include "rapidjson/writer.h"
1511

16-
static constexpr char kChannelName[] = "flutter/localization";
17-
1812
namespace flutter {
1913

2014
LocalizationChannel::LocalizationChannel(FlutterTizenEngine* engine)
21-
: engine_(engine) {}
15+
: engine_(engine) {
16+
SendLocales();
17+
}
2218

2319
LocalizationChannel::~LocalizationChannel() {}
2420

2521
void LocalizationChannel::SendLocales() {
26-
const char* defualt_locale = nullptr;
27-
FlutterLocale* flutter_locale = nullptr;
28-
std::vector<FlutterLocale*> flutter_locales;
29-
30-
int ret = i18n_ulocale_set_default(getenv("LANG"));
31-
ret = i18n_ulocale_get_default(&defualt_locale);
32-
if (ret != I18N_ERROR_NONE) {
33-
FT_LOGE("i18n_ulocale_get_default() failed.");
34-
return;
35-
}
36-
37-
std::string without_encoding_type(defualt_locale);
38-
auto n = without_encoding_type.find('.');
39-
without_encoding_type.erase(n, without_encoding_type.length() - n);
40-
41-
flutter_locale = GetFlutterLocale(without_encoding_type.data());
42-
if (flutter_locale) {
43-
FT_LOGI("Choose Default locale[%s]", without_encoding_type.data());
44-
flutter_locales.push_back(flutter_locale);
45-
}
46-
47-
int32_t count = i18n_ulocale_count_available();
48-
for (int i = 0; i < count; i++) {
49-
const char* locale = i18n_ulocale_get_available(i);
50-
if (without_encoding_type.compare(locale) != 0) {
51-
flutter_locale = GetFlutterLocale(locale);
52-
if (flutter_locale) {
53-
flutter_locales.push_back(flutter_locale);
54-
}
55-
}
56-
}
22+
std::vector<FlutterLocale*> flutter_locales = GetFlutterLocales();
5723

5824
FT_LOGI("Send %zu available locales", flutter_locales.size());
5925
// Send locales to engine
@@ -66,114 +32,6 @@ void LocalizationChannel::SendLocales() {
6632
}
6733
}
6834

69-
void LocalizationChannel::SendPlatformResolvedLocale() {
70-
const char* locale;
71-
int ret = i18n_ulocale_get_default(&locale);
72-
if (ret != I18N_ERROR_NONE) {
73-
FT_LOGE("i18n_ulocale_get_default() failed.");
74-
return;
75-
}
76-
77-
FlutterLocale* flutter_locale = GetFlutterLocale(locale);
78-
if (!flutter_locale) {
79-
FT_LOGE("Language code is required but not present.");
80-
return;
81-
}
82-
83-
rapidjson::Document document;
84-
auto& allocator = document.GetAllocator();
85-
86-
document.SetObject();
87-
document.AddMember("method", "setPlatformResolvedLocale", allocator);
88-
89-
rapidjson::Value language_code, country_code, script_code, variant_code;
90-
language_code.SetString(flutter_locale->language_code, allocator);
91-
country_code.SetString(
92-
flutter_locale->country_code ? flutter_locale->country_code : "",
93-
allocator);
94-
script_code.SetString(
95-
flutter_locale->script_code ? flutter_locale->script_code : "",
96-
allocator);
97-
variant_code.SetString(
98-
flutter_locale->variant_code ? flutter_locale->variant_code : "",
99-
allocator);
100-
101-
rapidjson::Value args(rapidjson::kArrayType);
102-
args.Reserve(4, allocator);
103-
args.PushBack(language_code, allocator);
104-
args.PushBack(country_code, allocator);
105-
args.PushBack(script_code, allocator);
106-
args.PushBack(variant_code, allocator);
107-
108-
rapidjson::StringBuffer buffer;
109-
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
110-
if (!document.Accept(writer)) {
111-
FT_LOGE("document.Accept failed!");
112-
return;
113-
}
114-
115-
engine_->SendPlatformMessage(
116-
kChannelName, reinterpret_cast<const uint8_t*>(buffer.GetString()),
117-
buffer.GetSize(), nullptr, nullptr);
118-
119-
DestroyFlutterLocale(flutter_locale);
120-
}
121-
122-
FlutterLocale* LocalizationChannel::GetFlutterLocale(const char* locale) {
123-
int capacity = 128;
124-
char buffer[128] = {0};
125-
int bufSize;
126-
int error;
127-
char* language = nullptr;
128-
char* country = nullptr;
129-
char* script = nullptr;
130-
char* variant = nullptr;
131-
132-
// set language code, language code is a required field
133-
error = i18n_ulocale_get_language(locale, buffer, capacity, &bufSize);
134-
if (error == I18N_ERROR_NONE && bufSize > 0) {
135-
language = new char[bufSize + 1];
136-
memcpy(language, buffer, bufSize);
137-
language[bufSize] = '\0';
138-
} else {
139-
FT_LOGE("i18n_ulocale_get_language failed!");
140-
return nullptr;
141-
}
142-
143-
// set country code, country code is an optional field
144-
bufSize = i18n_ulocale_get_country(locale, buffer, capacity, &error);
145-
if (error == I18N_ERROR_NONE && bufSize > 0) {
146-
country = new char[bufSize + 1];
147-
memcpy(country, buffer, bufSize);
148-
country[bufSize] = '\0';
149-
}
150-
151-
// set script code, script code is an optional field
152-
bufSize = i18n_ulocale_get_script(locale, buffer, capacity);
153-
if (bufSize > 0) {
154-
script = new char[bufSize + 1];
155-
memcpy(script, buffer, bufSize);
156-
script[bufSize] = '\0';
157-
}
158-
159-
// set variant code, variant code is an optional field
160-
bufSize = i18n_ulocale_get_variant(locale, buffer, capacity);
161-
if (bufSize > 0) {
162-
variant = new char[bufSize + 1];
163-
memcpy(variant, buffer, bufSize);
164-
variant[bufSize] = '\0';
165-
}
166-
167-
FlutterLocale* flutter_locale = new FlutterLocale;
168-
flutter_locale->struct_size = sizeof(FlutterLocale);
169-
flutter_locale->language_code = language;
170-
flutter_locale->country_code = country;
171-
flutter_locale->script_code = script;
172-
flutter_locale->variant_code = variant;
173-
174-
return flutter_locale;
175-
}
176-
17735
void LocalizationChannel::DestroyFlutterLocale(FlutterLocale* flutter_locale) {
17836
if (flutter_locale) {
17937
if (flutter_locale->language_code) {

shell/platform/tizen/channels/localization_channel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef EMBEDDER_LOCALIZATION_CHANNEL_H_
66
#define EMBEDDER_LOCALIZATION_CHANNEL_H_
77

8+
#include <vector>
9+
810
#include "flutter/shell/platform/embedder/embedder.h"
911

1012
namespace flutter {
@@ -20,6 +22,7 @@ class LocalizationChannel {
2022

2123
private:
2224
void SendPlatformResolvedLocale();
25+
std::vector<FlutterLocale*> GetFlutterLocales();
2326
FlutterLocale* GetFlutterLocale(const char* locale);
2427
void DestroyFlutterLocale(FlutterLocale* flutter_locale);
2528

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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 "localization_channel.h"
6+
#include <utils_i18n.h>
7+
8+
namespace flutter {
9+
10+
namespace {
11+
constexpr char kChannelName[] = "flutter/localization";
12+
}
13+
14+
void LocalizationChannel::SendPlatformResolvedLocale() {
15+
const char* locale;
16+
int ret = i18n_ulocale_get_default(&locale);
17+
if (ret != I18N_ERROR_NONE) {
18+
FT_LOGE("i18n_ulocale_get_default() failed.");
19+
return;
20+
}
21+
22+
FlutterLocale* flutter_locale = GetFlutterLocale(locale);
23+
if (!flutter_locale) {
24+
FT_LOGE("Language code is required but not present.");
25+
return;
26+
}
27+
28+
rapidjson::Document document;
29+
auto& allocator = document.GetAllocator();
30+
31+
document.SetObject();
32+
document.AddMember("method", "setPlatformResolvedLocale", allocator);
33+
34+
rapidjson::Value language_code, country_code, script_code, variant_code;
35+
language_code.SetString(flutter_locale->language_code, allocator);
36+
country_code.SetString(
37+
flutter_locale->country_code ? flutter_locale->country_code : "",
38+
allocator);
39+
script_code.SetString(
40+
flutter_locale->script_code ? flutter_locale->script_code : "",
41+
allocator);
42+
variant_code.SetString(
43+
flutter_locale->variant_code ? flutter_locale->variant_code : "",
44+
allocator);
45+
46+
rapidjson::Value args(rapidjson::kArrayType);
47+
args.Reserve(4, allocator);
48+
args.PushBack(language_code, allocator);
49+
args.PushBack(country_code, allocator);
50+
args.PushBack(script_code, allocator);
51+
args.PushBack(variant_code, allocator);
52+
53+
rapidjson::StringBuffer buffer;
54+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
55+
if (!document.Accept(writer)) {
56+
FT_LOGE("document.Accept failed!");
57+
return;
58+
}
59+
60+
engine_->SendPlatformMessage(
61+
kChannelName, reinterpret_cast<const uint8_t*>(buffer.GetString()),
62+
buffer.GetSize(), nullptr, nullptr);
63+
64+
DestroyFlutterLocale(flutter_locale);
65+
}
66+
67+
std::vector<FlutterLocale*> LocalizationChannel::GetFlutterLocales()
68+
{
69+
const char* defualt_locale = nullptr;
70+
FlutterLocale* flutter_locale = nullptr;
71+
std::vector<FlutterLocale*> flutter_locales;
72+
73+
int ret = i18n_ulocale_set_default(getenv("LANG"));
74+
ret = i18n_ulocale_get_default(&defualt_locale);
75+
if (ret != I18N_ERROR_NONE) {
76+
FT_LOGE("i18n_ulocale_get_default() failed.");
77+
return;
78+
}
79+
80+
std::string without_encoding_type(defualt_locale);
81+
auto n = without_encoding_type.find('.');
82+
without_encoding_type.erase(n, without_encoding_type.length() - n);
83+
84+
flutter_locale = GetFlutterLocale(without_encoding_type.data());
85+
if (flutter_locale) {
86+
FT_LOGI("Choose Default locale[%s]", without_encoding_type.data());
87+
flutter_locales.push_back(flutter_locale);
88+
}
89+
90+
int32_t count = i18n_ulocale_count_available();
91+
for (int i = 0; i < count; i++) {
92+
const char* locale = i18n_ulocale_get_available(i);
93+
if (without_encoding_type.compare(locale) != 0) {
94+
flutter_locale = GetFlutterLocale(locale);
95+
if (flutter_locale) {
96+
flutter_locales.push_back(flutter_locale);
97+
}
98+
}
99+
}
100+
return flutter_locales;
101+
}
102+
103+
104+
FlutterLocale* LocalizationChannel::GetFlutterLocale(const char* locale) {
105+
int capacity = 128;
106+
char buffer[128] = {0};
107+
int bufSize;
108+
int error;
109+
char* language = nullptr;
110+
char* country = nullptr;
111+
char* script = nullptr;
112+
char* variant = nullptr;
113+
114+
// set language code, language code is a required field
115+
error = i18n_ulocale_get_language(locale, buffer, capacity, &bufSize);
116+
if (error == I18N_ERROR_NONE && bufSize > 0) {
117+
language = new char[bufSize + 1];
118+
memcpy(language, buffer, bufSize);
119+
language[bufSize] = '\0';
120+
} else {
121+
FT_LOGE("i18n_ulocale_get_language failed!");
122+
return nullptr;
123+
}
124+
125+
// set country code, country code is an optional field
126+
bufSize = i18n_ulocale_get_country(locale, buffer, capacity, &error);
127+
if (error == I18N_ERROR_NONE && bufSize > 0) {
128+
country = new char[bufSize + 1];
129+
memcpy(country, buffer, bufSize);
130+
country[bufSize] = '\0';
131+
}
132+
133+
// set script code, script code is an optional field
134+
bufSize = i18n_ulocale_get_script(locale, buffer, capacity);
135+
if (bufSize > 0) {
136+
script = new char[bufSize + 1];
137+
memcpy(script, buffer, bufSize);
138+
script[bufSize] = '\0';
139+
}
140+
141+
// set variant code, variant code is an optional field
142+
bufSize = i18n_ulocale_get_variant(locale, buffer, capacity);
143+
if (bufSize > 0) {
144+
variant = new char[bufSize + 1];
145+
memcpy(variant, buffer, bufSize);
146+
variant[bufSize] = '\0';
147+
}
148+
149+
FlutterLocale* flutter_locale = new FlutterLocale;
150+
flutter_locale->struct_size = sizeof(FlutterLocale);
151+
flutter_locale->language_code = language;
152+
flutter_locale->country_code = country;
153+
flutter_locale->script_code = script;
154+
flutter_locale->variant_code = variant;
155+
156+
return flutter_locale;
157+
}
158+
159+
160+
} // namespace flutter

shell/platform/tizen/channels/localization_channel_stub.cc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@
66

77
namespace flutter {
88

9-
LocalizationChannel::LocalizationChannel(FlutterTizenEngine* engine) {}
9+
void LocalizationChannel::SendPlatformResolvedLocale() {}
1010

11-
LocalizationChannel::~LocalizationChannel() {}
11+
std::vector<FlutterLocale*> LocalizationChannel::GetFlutterLocales() {
12+
FlutterLocale* flutter_locale = GetFlutterLocale(nullptr);
13+
std::vector<FlutterLocale*> flutter_locales;
14+
flutter_locales.push_back(flutter_locale);
15+
return flutter_locales;
16+
}
1217

13-
void LocalizationChannel::SendLocales() {}
18+
FlutterLocale* LocalizationChannel::GetFlutterLocale(const char* locale) {
19+
FlutterLocale* flutter_locale = new FlutterLocale;
20+
flutter_locale->struct_size = sizeof(FlutterLocale);
21+
flutter_locale->language_code = new char[1];
22+
flutter_locale->country_code = new char[1];
23+
flutter_locale->script_code = new char[1];
24+
flutter_locale->variant_code = new char[1];
25+
return flutter_locale;
26+
}
1427

1528
} // namespace flutter

0 commit comments

Comments
 (0)