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 package io.flutter.embedding.engine.systemchannels; 6 7 import android.os.Build; 8 import android.support.annotation.NonNull; 9 10 import java.util.ArrayList; 11 import java.util.List; 12 import java.util.Locale; 13 14 import io.flutter.Log; 15 import io.flutter.embedding.engine.dart.DartExecutor; 16 import io.flutter.plugin.common.JSONMethodCodec; 17 import io.flutter.plugin.common.MethodChannel; 18 19 /** 20 * Sends the platform's locales to Dart. 21 */ 22 public class LocalizationChannel { 23 private static final String TAG = "LocalizationChannel"; 24 25 @NonNull 26 public final MethodChannel channel; 27 LocalizationChannel(@onNull DartExecutor dartExecutor)28 public LocalizationChannel(@NonNull DartExecutor dartExecutor) { 29 this.channel = new MethodChannel(dartExecutor, "flutter/localization", JSONMethodCodec.INSTANCE); 30 } 31 32 /** 33 * Send the given {@code locales} to Dart. 34 */ sendLocales(@onNull List<Locale> locales)35 public void sendLocales(@NonNull List<Locale> locales) { 36 Log.v(TAG, "Sending Locales to Flutter."); 37 List<String> data = new ArrayList<>(); 38 for (Locale locale : locales) { 39 Log.v(TAG, "Locale (Language: " + locale.getLanguage() 40 + ", Country: " + locale.getCountry() 41 + ", Variant: " + locale.getVariant() + ")"); 42 data.add(locale.getLanguage()); 43 data.add(locale.getCountry()); 44 // locale.getScript() was added in API 21. 45 data.add(Build.VERSION.SDK_INT >= 21 ? locale.getScript() : ""); 46 data.add(locale.getVariant()); 47 } 48 channel.invokeMethod("setLocale", data); 49 } 50 51 } 52