1 // Copyright (c) 2012 The Chromium 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 // This file contains utility functions for dealing with localized 6 // content. 7 8 #ifndef UI_BASE_L10N_L10N_UTIL_H_ 9 #define UI_BASE_L10N_L10N_UTIL_H_ 10 11 #include <string> 12 #include <vector> 13 14 #include "base/strings/string16.h" 15 #include "base/strings/string_util.h" 16 #include "ui/base/ui_export.h" 17 18 #if defined(OS_MACOSX) 19 #include "ui/base/l10n/l10n_util_mac.h" 20 #endif // OS_MACOSX 21 22 namespace l10n_util { 23 // This method translates a generic locale name to one of the locally defined 24 // ones. This method returns true if it succeeds. 25 UI_EXPORT bool CheckAndResolveLocale(const std::string& locale, 26 std::string* resolved_locale); 27 28 // This method is responsible for determining the locale as defined below. In 29 // nearly all cases you shouldn't call this, rather use GetApplicationLocale 30 // defined on browser_process. 31 // 32 // Returns the locale used by the Application. First we use the value from the 33 // command line (--lang), second we try the value in the prefs file (passed in 34 // as |pref_locale|), finally, we fall back on the system locale. We only return 35 // a value if there's a corresponding resource DLL for the locale. Otherwise, 36 // we fall back to en-us. 37 UI_EXPORT std::string GetApplicationLocale(const std::string& pref_locale); 38 39 // Returns true if a display name for |locale| is available in the locale 40 // |display_locale|. 41 UI_EXPORT bool IsLocaleNameTranslated(const char* locale, 42 const std::string& display_locale); 43 44 // Given a locale code, return true if the OS is capable of supporting it. 45 // For instance, Oriya is not well supported on Windows XP and we return 46 // false for "or". 47 bool IsLocaleSupportedByOS(const std::string& locale); 48 49 // This method returns the display name of the locale code in |display_locale|. 50 51 // For example, for |locale| = "fr" and |display_locale| = "en", 52 // it returns "French". To get the display name of 53 // |locale| in the UI language of Chrome, |display_locale| can be 54 // set to the return value of g_browser_process->GetApplicationLocale() 55 // in the UI thread. 56 // If |is_for_ui| is true, U+200F is appended so that it can be 57 // rendered properly in a RTL Chrome. 58 UI_EXPORT base::string16 GetDisplayNameForLocale( 59 const std::string& locale, 60 const std::string& display_locale, 61 bool is_for_ui); 62 63 // Returns the display name of the |country_code| in |display_locale|. 64 UI_EXPORT base::string16 GetDisplayNameForCountry( 65 const std::string& country_code, 66 const std::string& display_locale); 67 68 // Converts all - into _, to be consistent with ICU and file system names. 69 UI_EXPORT std::string NormalizeLocale(const std::string& locale); 70 71 // Produce a vector of parent locales for given locale. 72 // It includes the current locale in the result. 73 // sr_Cyrl_RS generates sr_Cyrl_RS, sr_Cyrl and sr. 74 UI_EXPORT void GetParentLocales(const std::string& current_locale, 75 std::vector<std::string>* parent_locales); 76 77 // Checks if a string is plausibly a syntactically-valid locale string, 78 // for cases where we want the valid input to be a locale string such as 79 // 'en', 'pt-BR', 'fil', 'es-419', 'zh-Hans-CN', 'i-klingon' or 80 // 'de_DE@collation=phonebook', but we don't want to limit it to 81 // locales that Chrome actually knows about, so 'xx-YY' should be 82 // accepted, but 'z', 'German', 'en-$1', or 'abcd-1234' should not. 83 // Case-insensitive. Based on BCP 47, see: 84 // http://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers 85 UI_EXPORT bool IsValidLocaleSyntax(const std::string& locale); 86 87 // 88 // Mac Note: See l10n_util_mac.h for some NSString versions and other support. 89 // 90 91 // Pulls resource string from the string bundle and returns it. 92 UI_EXPORT std::string GetStringUTF8(int message_id); 93 UI_EXPORT base::string16 GetStringUTF16(int message_id); 94 95 // Get a resource string and replace $i with replacements[i] for all 96 // i < replacements.size(). Additionally, $$ is replaced by $. 97 // If non-NULL |offsets| will be replaced with the start points of the replaced 98 // strings. 99 UI_EXPORT base::string16 GetStringFUTF16( 100 int message_id, 101 const std::vector<base::string16>& replacements, 102 std::vector<size_t>* offsets); 103 104 // Convenience wrappers for the above. 105 UI_EXPORT base::string16 GetStringFUTF16(int message_id, 106 const base::string16& a); 107 UI_EXPORT base::string16 GetStringFUTF16(int message_id, 108 const base::string16& a, 109 const base::string16& b); 110 UI_EXPORT base::string16 GetStringFUTF16(int message_id, 111 const base::string16& a, 112 const base::string16& b, 113 const base::string16& c); 114 UI_EXPORT base::string16 GetStringFUTF16(int message_id, 115 const base::string16& a, 116 const base::string16& b, 117 const base::string16& c, 118 const base::string16& d); 119 UI_EXPORT base::string16 GetStringFUTF16(int message_id, 120 const base::string16& a, 121 const base::string16& b, 122 const base::string16& c, 123 const base::string16& d, 124 const base::string16& e); 125 UI_EXPORT std::string GetStringFUTF8(int message_id, 126 const base::string16& a); 127 UI_EXPORT std::string GetStringFUTF8(int message_id, 128 const base::string16& a, 129 const base::string16& b); 130 UI_EXPORT std::string GetStringFUTF8(int message_id, 131 const base::string16& a, 132 const base::string16& b, 133 const base::string16& c); 134 UI_EXPORT std::string GetStringFUTF8(int message_id, 135 const base::string16& a, 136 const base::string16& b, 137 const base::string16& c, 138 const base::string16& d); 139 140 // Variants that return the offset(s) of the replaced parameters. The 141 // vector based version returns offsets ordered by parameter. For example if 142 // invoked with a and b offsets[0] gives the offset for a and offsets[1] the 143 // offset of b regardless of where the parameters end up in the string. 144 UI_EXPORT base::string16 GetStringFUTF16(int message_id, 145 const base::string16& a, 146 size_t* offset); 147 UI_EXPORT base::string16 GetStringFUTF16(int message_id, 148 const base::string16& a, 149 const base::string16& b, 150 std::vector<size_t>* offsets); 151 152 // Convenience functions to get a string with a single number as a parameter. 153 UI_EXPORT base::string16 GetStringFUTF16Int(int message_id, int a); 154 base::string16 GetStringFUTF16Int(int message_id, int64 a); 155 156 // Get a resource string using |number| to decide which of |message_ids| should 157 // be used. |message_ids| must be size 6 and in order: default, singular, zero, 158 // two, few, many. 159 UI_EXPORT base::string16 GetPluralStringFUTF16( 160 const std::vector<int>& message_ids, 161 int number); 162 UI_EXPORT std::string GetPluralStringFUTF8(const std::vector<int>& message_ids, 163 int number); 164 165 // In place sorting of base::string16 strings using collation rules for |locale|. 166 UI_EXPORT void SortStrings16(const std::string& locale, 167 std::vector<base::string16>* strings); 168 169 // Returns a vector of available locale codes. E.g., a vector containing 170 // en-US, es, fr, fi, pt-PT, pt-BR, etc. 171 UI_EXPORT const std::vector<std::string>& GetAvailableLocales(); 172 173 // Returns a vector of locale codes usable for accept-languages. 174 UI_EXPORT void GetAcceptLanguagesForLocale( 175 const std::string& display_locale, 176 std::vector<std::string>* locale_codes); 177 178 // Returns the preferred size of the contents view of a window based on 179 // designer given constraints which might dependent on the language used. 180 UI_EXPORT int GetLocalizedContentsWidthInPixels(int pixel_resource_id); 181 182 } // namespace l10n_util 183 184 #endif // UI_BASE_L10N_L10N_UTIL_H_ 185