Skip to content

Commit

Permalink
Pass scriptcode and variantcode to dart:ui Window. (flutter#6493)
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryQian authored Oct 11, 2018
1 parent 0eda0cf commit 61cf4c0
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/ui/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ String _localeClosure() => window._locale.toString();
_LocaleClosure _getLocaleClosure() => _localeClosure;

@pragma('vm:entry-point')
void _updateLocale(String languageCode, String countryCode) {
void _updateLocale(String languageCode, String countryCode, String scriptCode, String variantCode) {
window._locale = new Locale(languageCode, countryCode);
_invoke(window.onLocaleChanged, window._onLocaleChangedZone);
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ui/window/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ void Window::UpdateWindowMetrics(const ViewportMetrics& metrics) {
}

void Window::UpdateLocale(const std::string& language_code,
const std::string& country_code) {
const std::string& country_code,
const std::string& script_code,
const std::string& variant_code) {
std::shared_ptr<tonic::DartState> dart_state = library_.dart_state().lock();
if (!dart_state)
return;
Expand All @@ -173,6 +175,8 @@ void Window::UpdateLocale(const std::string& language_code,
{
StdStringToDart(language_code),
StdStringToDart(country_code),
StdStringToDart(script_code),
StdStringToDart(variant_code),
});
}

Expand Down
4 changes: 3 additions & 1 deletion lib/ui/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ class Window final {
void DidCreateIsolate();
void UpdateWindowMetrics(const ViewportMetrics& metrics);
void UpdateLocale(const std::string& language_code,
const std::string& country_code);
const std::string& country_code,
const std::string& script_code,
const std::string& variant_code);
void UpdateUserSettingsData(const std::string& data);
void UpdateSemanticsEnabled(bool enabled);
void UpdateAccessibilityFeatures(int32_t flags);
Expand Down
12 changes: 9 additions & 3 deletions runtime/runtime_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ std::unique_ptr<RuntimeController> RuntimeController::Clone() const {

bool RuntimeController::FlushRuntimeStateToIsolate() {
return SetViewportMetrics(window_data_.viewport_metrics) &&
SetLocale(window_data_.language_code, window_data_.country_code) &&
SetLocale(window_data_.language_code, window_data_.country_code,
window_data_.script_code, window_data_.variant_code) &&
SetSemanticsEnabled(window_data_.semantics_enabled) &&
SetAccessibilityFeatures(window_data_.accessibility_feature_flags_);
}
Expand All @@ -140,12 +141,17 @@ bool RuntimeController::SetViewportMetrics(const ViewportMetrics& metrics) {
}

bool RuntimeController::SetLocale(const std::string& language_code,
const std::string& country_code) {
const std::string& country_code,
const std::string& script_code,
const std::string& variant_code) {
window_data_.language_code = language_code;
window_data_.country_code = country_code;
window_data_.script_code = script_code;
window_data_.variant_code = variant_code;

if (auto window = GetWindowIfAvailable()) {
window->UpdateLocale(window_data_.language_code, window_data_.country_code);
window->UpdateLocale(window_data_.language_code, window_data_.country_code,
window_data_.script_code, window_data_.variant_code);
return true;
}

Expand Down
6 changes: 5 additions & 1 deletion runtime/runtime_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class RuntimeController final : public WindowClient {
bool SetViewportMetrics(const ViewportMetrics& metrics);

bool SetLocale(const std::string& language_code,
const std::string& country_code);
const std::string& country_code,
const std::string& script_code,
const std::string& variant_code);

bool SetUserSettingsData(const std::string& data);

Expand Down Expand Up @@ -80,6 +82,8 @@ class RuntimeController final : public WindowClient {
ViewportMetrics viewport_metrics;
std::string language_code;
std::string country_code;
std::string script_code;
std::string variant_code;
std::string user_settings_data = "{}";
bool semantics_enabled = false;
bool assistive_technology_enabled = false;
Expand Down
5 changes: 4 additions & 1 deletion shell/common/engine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,15 @@ bool Engine::HandleLocalizationPlatformMessage(

const auto& language = args->value[0];
const auto& country = args->value[1];
const auto& script = args->value[2];
const auto& variant = args->value[3];

if (!language.IsString() || !country.IsString())
return false;

return runtime_controller_->SetLocale(language.GetString(),
country.GetString());
country.GetString(), script.GetString(),
variant.GetString());
}

void Engine::HandleSettingsPlatformMessage(blink::PlatformMessage* message) {
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void setUserSettings() {
}

private void setLocale(Locale locale) {
mFlutterLocalizationChannel.invokeMethod("setLocale", Arrays.asList(locale.getLanguage(), locale.getCountry()));
mFlutterLocalizationChannel.invokeMethod("setLocale", Arrays.asList(locale.getLanguage(), locale.getCountry(), locale.getScript(), locale.getVariant()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,8 +894,16 @@ - (void)onLocaleUpdated:(NSNotification*)notification {
NSLocale* currentLocale = [NSLocale currentLocale];
NSString* languageCode = [currentLocale objectForKey:NSLocaleLanguageCode];
NSString* countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
NSString* scriptCode = [currentLocale objectForKey:NSLocaleScriptCode];
NSString* variantCode = [currentLocale objectForKey:NSLocaleVariantCode];
if (languageCode && countryCode)
[_localizationChannel.get() invokeMethod:@"setLocale" arguments:@[ languageCode, countryCode ]];
// We pass empty strings for undefined scripts and variants to ensure the JSON encoder/decoder
// functions properly.
[_localizationChannel.get() invokeMethod:@"setLocale"
arguments:@[
languageCode, countryCode, scriptCode ? scriptCode : @"",
variantCode ? variantCode : @""
]];
}

#pragma mark - Set user settings
Expand Down
2 changes: 1 addition & 1 deletion testing/dart/window_hooks_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void main() {
};
});

_updateLocale('en', 'US');
_updateLocale('en', 'US', '', '');
expect(runZone, isNotNull);
expect(runZone, same(innerZone));
expect(locale, equals(const Locale('en', 'US')));
Expand Down

0 comments on commit 61cf4c0

Please sign in to comment.