Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 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 #include "ios/chrome/browser/ui/file_locations.h" | |
| 6 | |
| 7 #include "base/mac/bundle_locations.h" | |
| 8 #include "base/mac/foundation_util.h" | |
| 9 #include "base/strings/sys_string_conversions.h" | |
| 10 #include "ios/chrome/browser/application_context.h" | |
| 11 | |
| 12 namespace { | |
| 13 // English is the default locale for the Terms of Service. | |
| 14 const char kEnglishLocale[] = "en"; | |
| 15 // The prefix of the Chrome Terms of Service file name. | |
| 16 const char kChromeTosFilePrefix[] = "terms"; | |
| 17 // The file name extension for HTML files. | |
| 18 const char kHtmlFileExtension[] = "html"; | |
| 19 } // namespace | |
| 20 | |
| 21 namespace file_locations { | |
| 22 | |
| 23 // iOS uses certain locale initials that are different from Chrome's. | |
| 24 // Most notably, "pt" means "pt-BR" (Brazillian Portuguese). This | |
| 25 // function normalizes the locale into language-region code used by | |
| 26 // Chrome. | |
| 27 std::string iosLocaleMapping(const std::string& locale); | |
|
sdefresne
2015/04/02 17:20:28
style: this is a c++ function, so should be called
droger
2015/04/03 08:59:07
This function is used in tests, I can't put it in
| |
| 28 | |
| 29 // Checks that the requested file exists in the application's resource | |
| 30 // bundle and returns the filename. Returns an empty string if resource does not | |
| 31 // exist. | |
| 32 std::string find_file_in_resource(const std::string& base_name, | |
|
sdefresne
2015/04/02 17:20:28
nit: can go in the anonymous namespace.
| |
| 33 const std::string& language, | |
| 34 const std::string& ext) { | |
| 35 std::string resource_file(base_name + "_" + language); | |
| 36 BOOL exists = | |
| 37 [base::mac::FrameworkBundle() | |
| 38 URLForResource:[NSString stringWithUTF8String:resource_file.c_str()] | |
| 39 withExtension:[NSString stringWithUTF8String:ext.c_str()]] != nil; | |
| 40 return exists ? resource_file + "." + ext : ""; | |
| 41 } | |
| 42 | |
| 43 // Returns a filename based on the base file name and file extension and | |
| 44 // localized for the given locale. Checks the existence of the file based on | |
| 45 // |locale| as follows: | |
| 46 // * if there is a file for file_<locale>.<ext> | |
| 47 // * if not, check the language part as follows file_<locale[0..]>.<ext> | |
| 48 // * when all else fails, use the English locale, e.g. file_en.<ext>, which | |
| 49 // must exist. | |
| 50 // |locale| must be a valid Chrome locale as defined in file | |
| 51 // ui/base/l10n/l10n_util.cc. This corresponds to a language or a country code, | |
| 52 // e.g. "en", "en-US", "fr", etc. | |
| 53 std::string localized_file_name(const std::string& base_name, | |
|
sdefresne
2015/04/02 17:20:28
nit: can go in the anonymous namespace.
droger
2015/04/03 08:59:07
This function is used in tests, I can't put it in
| |
| 54 const std::string& locale, | |
| 55 const std::string& ext) { | |
| 56 std::string mappedLocale = iosLocaleMapping(locale); | |
| 57 std::string resource_file = | |
| 58 find_file_in_resource(base_name, mappedLocale, ext); | |
| 59 if (resource_file.empty() && mappedLocale.length() > 2 && | |
| 60 mappedLocale[2] == '-') { | |
| 61 std::string language = mappedLocale.substr(0, 2); | |
| 62 resource_file = find_file_in_resource(base_name, language, ext); | |
| 63 } | |
| 64 if (resource_file.empty()) { | |
| 65 // Default to English if resource is still not found. | |
| 66 resource_file = find_file_in_resource(base_name, kEnglishLocale, ext); | |
| 67 } | |
| 68 DCHECK(!resource_file.empty()); | |
| 69 return resource_file; | |
| 70 } | |
| 71 | |
| 72 std::string TermsOfService() { | |
| 73 const std::string& locale = GetApplicationContext()->GetApplicationLocale(); | |
| 74 return localized_file_name(kChromeTosFilePrefix, locale, kHtmlFileExtension); | |
| 75 } | |
| 76 | |
| 77 std::string iosLocaleMapping(const std::string& locale) { | |
| 78 if (locale == "pt") // Brazillian Portuguese | |
| 79 return "pt-BR"; | |
| 80 else if (locale == "zh-Hans") // Chinese Simplified script | |
| 81 return "zh-CN"; | |
| 82 else if (locale == "zh-Hant") // Chinese Traditional script | |
| 83 return "zh-TW"; | |
| 84 else if (locale == "es-MX") | |
| 85 return "es-419"; | |
| 86 return locale; | |
| 87 } | |
| 88 | |
| 89 } // namespace file_locations | |
| OLD | NEW |