Flutter Engine
The Flutter Engine
LocalizationChannel.java
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
5package io.flutter.embedding.engine.systemchannels;
6
7import androidx.annotation.NonNull;
8import androidx.annotation.Nullable;
9import androidx.annotation.VisibleForTesting;
10import io.flutter.Log;
11import io.flutter.embedding.engine.dart.DartExecutor;
12import io.flutter.plugin.common.JSONMethodCodec;
13import io.flutter.plugin.common.MethodCall;
14import io.flutter.plugin.common.MethodChannel;
15import java.util.ArrayList;
16import java.util.List;
17import java.util.Locale;
18import org.json.JSONException;
19import org.json.JSONObject;
20
21/** Sends the platform's locales to Dart. */
22public class LocalizationChannel {
23 private static final String TAG = "LocalizationChannel";
24
25 @NonNull public final MethodChannel channel;
26 @Nullable private LocalizationMessageHandler localizationMessageHandler;
27
28 @NonNull @VisibleForTesting
29 public final MethodChannel.MethodCallHandler handler =
31 @Override
32 public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
33 if (localizationMessageHandler == null) {
34 // If no explicit LocalizationMessageHandler has been registered then we don't
35 // need to forward this call to an API. Return.
36 return;
37 }
38
39 String method = call.method;
40 switch (method) {
41 case "Localization.getStringResource":
42 JSONObject arguments = call.<JSONObject>arguments();
43 try {
44 String key = arguments.getString("key");
45 String localeString = null;
46 if (arguments.has("locale")) {
47 localeString = arguments.getString("locale");
48 }
49 result.success(localizationMessageHandler.getStringResource(key, localeString));
50 } catch (JSONException exception) {
51 result.error("error", exception.getMessage(), null);
52 }
53 break;
54 default:
55 result.notImplemented();
56 break;
57 }
58 }
59 };
60
61 public LocalizationChannel(@NonNull DartExecutor dartExecutor) {
62 this.channel =
63 new MethodChannel(dartExecutor, "flutter/localization", JSONMethodCodec.INSTANCE);
65 }
66
67 /**
68 * Sets the {@link LocalizationMessageHandler} which receives all events and requests that are
69 * parsed from the underlying platform channel.
70 */
72 @Nullable LocalizationMessageHandler localizationMessageHandler) {
73 this.localizationMessageHandler = localizationMessageHandler;
74 }
75
76 /** Send the given {@code locales} to Dart. */
77 public void sendLocales(@NonNull List<Locale> locales) {
78 Log.v(TAG, "Sending Locales to Flutter.");
79 // Send the user's preferred locales.
80 List<String> data = new ArrayList<>();
81 for (Locale locale : locales) {
82 Log.v(
83 TAG,
84 "Locale (Language: "
85 + locale.getLanguage()
86 + ", Country: "
87 + locale.getCountry()
88 + ", Variant: "
89 + locale.getVariant()
90 + ")");
91 data.add(locale.getLanguage());
92 data.add(locale.getCountry());
93 data.add(locale.getScript());
94 data.add(locale.getVariant());
95 }
96 channel.invokeMethod("setLocale", data);
97 }
98
99 /**
100 * Handler that receives platform messages sent from Flutter to Android through a given {@link
101 * PlatformChannel}.
102 *
103 * <p>To register a {@code LocalizationMessageHandler} with a {@link PlatformChannel}, see {@link
104 * LocalizationChannel#setLocalizationMessageHandler(LocalizationMessageHandler)}.
105 */
106 public interface LocalizationMessageHandler {
107 /**
108 * The Flutter application would like to obtain the string resource of given {@code key} in
109 * {@code locale}.
110 */
111 @NonNull
112 String getStringResource(@NonNull String key, @NonNull String locale);
113 }
114}
static void v(@NonNull String tag, @NonNull String message)
Definition: Log.java:40
void setLocalizationMessageHandler( @Nullable LocalizationMessageHandler localizationMessageHandler)
static final JSONMethodCodec INSTANCE
void setMethodCallHandler(final @Nullable MethodCallHandler handler)
void invokeMethod(@NonNull String method, @Nullable Object arguments)
GAsyncResult * result
def call(args)
Definition: dom.py:159
#define TAG()
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63