summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/i18n_util.cc
blob: a84bdc577b506f7055c1062382b89e2ac97c7ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "weblayer/browser/i18n_util.h"

#include "base/i18n/rtl.h"
#include "base/no_destructor.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "net/http/http_util.h"

#if defined(OS_ANDROID)
#include "base/android/locale_utils.h"
#include "base/task/post_task.h"
#include "ui/base/resource/resource_bundle.h"
#include "weblayer/browser/java/jni/LocaleChangedBroadcastReceiver_jni.h"
#endif

namespace {

base::CallbackList<void()>& GetLocaleChangeCallbacks() {
  static base::NoDestructor<base::CallbackList<void()>> instance;
  return *instance;
}

}  // namespace

namespace weblayer {
namespace i18n {

std::string GetApplicationLocale() {
  // The locale is set in ContentMainDelegateImpl::InitializeResourceBundle().
  return base::i18n::GetConfiguredLocale();
}

std::string GetAcceptLangs() {
#if defined(OS_ANDROID)
  std::string locale_list = base::android::GetDefaultLocaleListString();
#else
  std::string locale_list = GetApplicationLocale();
#endif
  return net::HttpUtil::ExpandLanguageList(locale_list);
}

std::unique_ptr<LocaleChangeSubscription> RegisterLocaleChangeCallback(
    base::RepeatingClosure locale_changed) {
  return GetLocaleChangeCallbacks().Add(locale_changed);
}

#if defined(OS_ANDROID)
static void JNI_LocaleChangedBroadcastReceiver_LocaleChanged(JNIEnv* env) {
  base::ThreadPool::PostTaskAndReply(
      FROM_HERE, {base::MayBlock(), base::TaskPriority::USER_VISIBLE},
      base::BindOnce([]() {
        // Passing an empty |pref_locale| means the Android system locale will
        // be used (base::android::GetDefaultLocaleString()).
        ui::ResourceBundle::GetSharedInstance().ReloadLocaleResources(
            {} /*pref_locale*/);
      }),
      base::BindOnce([]() { GetLocaleChangeCallbacks().Notify(); }));
  // TODO(estade): need to update the ResourceBundle for non-Browser processes
  // as well.
}
#endif

}  // namespace i18n
}  // namespace weblayer