File: locale_util.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 4,348 bytes parent folder | download | duplicates (7)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file contains utility functions for locale change.

#ifndef CHROME_BROWSER_ASH_BASE_LOCALE_UTIL_H_
#define CHROME_BROWSER_ASH_BASE_LOCALE_UTIL_H_

#include <memory>
#include <string>

#include "base/functional/callback_forward.h"

class Profile;
class PrefService;

namespace ash {
namespace locale_util {

struct LanguageSwitchResult {
  LanguageSwitchResult(const std::string& requested_locale,
                       const std::string& loaded_locale,
                       bool success);

  std::string requested_locale;
  std::string loaded_locale;
  bool success;
};

// This callback is called on UI thread, when ReloadLocaleResources() is
// completed on BlockingPool.
// |result| contains:
//   locale - (copy of) locale argument to SwitchLanguage(). Expected locale.
//   loaded_locale - actual locale name loaded.
//   success - if locale load succeeded.
// (const std::string* locale, const std::string* loaded_locale, bool success)
using SwitchLanguageCallback =
    base::OnceCallback<void(const LanguageSwitchResult& result)>;

// This function updates input methods only if requested. In general, you want
// |enable_locale_keyboard_layouts = true|. |profile| is needed because IME
// extensions are per-user.
// Note: in case of |enable_locale_keyboard_layouts = false|, the input method
// currently in use may not be supported by the new locale. Using the new locale
// with an unsupported input method may lead to undefined behavior. Use
// |enable_locale_keyboard_layouts = false| with caution.
// Note 2: |login_layouts_only = true| enables only login-capable layouts.
void SwitchLanguage(const std::string& locale,
                    const bool enable_locale_keyboard_layouts,
                    const bool login_layouts_only,
                    SwitchLanguageCallback callback,
                    Profile* profile);

// This function checks if the given language is allowed according to the list
// of allowed languages (stored in |prefs|, managed by 'AllowedLanguages'
// policy). If the list is empty, every language is allowed.
bool IsAllowedLanguage(const std::string& language, const PrefService* prefs);

// This function checks if the given language is allowed
// by 'AllowedLanguages' and also can be used as a UI locale.
// (see |IsAllowedLanguage|).
bool IsAllowedUILanguage(const std::string& language, const PrefService* prefs);

// This functions checks if the given language is a native UI language or can be
// converted to one. (e.g., 'en-US', 'fr', 'de', 'de-CH', 'fr-CH' etc. are all
// valid, but 'az' is not.
bool IsNativeUILanguage(const std::string& locale);

// This function removes languages that are disallowed by the
// 'AllowedLanguages' policy from the list of preferred languages.
// If no current preferred languages are allowed, this functions sets
// a language provided by GetAllowedFallbackLanguage() as the only preferred
// language.
void RemoveDisallowedLanguagesFromPreferred(PrefService* prefs);

// This function returns a fallback language that is allowed by the
// 'AllowedLanguages' policy and can be used as a UI language
// (see |IsNativeUILanguage|) as well. We check the user's preferred language
// for any of these conditions. If none of them match, we will take the first
// valid UI language in the list of allowed languages. If none of them are UI
// languages, we default to "en-US" (see kAllowedUILanguageFallback).
// languages (stored in |prefs|, managed by 'AllowedLanguages' policy). If none
// of the user's preferred languages is allowed, the function returns the first
// valid entry in the allowed languages list. If the list contains no valid
// entries, the default fallback will be 'en-US'
// (kAllowedLanguagesFallbackLocale);
std::string GetAllowedFallbackUILanguage(const PrefService* prefs);

// This function adds the |locale| to the list of preferred languages (pref
// |kLanguagePreferredLanguages|). Returns true if the locale was newly added
// to the list, false otherwise.
bool AddLocaleToPreferredLanguages(const std::string& locale,
                                   PrefService* prefs);

}  // namespace locale_util
}  // namespace ash

#endif  // CHROME_BROWSER_ASH_BASE_LOCALE_UTIL_H_