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 |
| 20 // Checks that the requested file exists in the application's resource |
| 21 // bundle and returns the filename. Returns an empty string if resource does not |
| 22 // exist. |
| 23 std::string FindFileInResource(const std::string& base_name, |
| 24 const std::string& language, |
| 25 const std::string& ext) { |
| 26 std::string resource_file(base_name + "_" + language); |
| 27 BOOL exists = |
| 28 [base::mac::FrameworkBundle() |
| 29 URLForResource:[NSString stringWithUTF8String:resource_file.c_str()] |
| 30 withExtension:[NSString stringWithUTF8String:ext.c_str()]] != nil; |
| 31 return exists ? resource_file + "." + ext : ""; |
| 32 } |
| 33 |
| 34 } // namespace |
| 35 |
| 36 // iOS uses certain locale initials that are different from Chrome's. |
| 37 // Most notably, "pt" means "pt-BR" (Brazillian Portuguese). This |
| 38 // function normalizes the locale into language-region code used by |
| 39 // Chrome. |
| 40 std::string GetIOSLocaleMapping(const std::string& locale) { |
| 41 if (locale == "pt") // Brazillian Portuguese |
| 42 return "pt-BR"; |
| 43 else if (locale == "zh-Hans") // Chinese Simplified script |
| 44 return "zh-CN"; |
| 45 else if (locale == "zh-Hant") // Chinese Traditional script |
| 46 return "zh-TW"; |
| 47 else if (locale == "es-MX") |
| 48 return "es-419"; |
| 49 return locale; |
| 50 } |
| 51 |
| 52 // Returns a filename based on the base file name and file extension and |
| 53 // localized for the given locale. Checks the existence of the file based on |
| 54 // |locale| as follows: |
| 55 // * if there is a file for file_<locale>.<ext> |
| 56 // * if not, check the language part as follows file_<locale[0..]>.<ext> |
| 57 // * when all else fails, use the English locale, e.g. file_en.<ext>, which |
| 58 // must exist. |
| 59 // |locale| must be a valid Chrome locale as defined in file |
| 60 // ui/base/l10n/l10n_util.cc. This corresponds to a language or a country code, |
| 61 // e.g. "en", "en-US", "fr", etc. |
| 62 std::string GetLocalizedFileName(const std::string& base_name, |
| 63 const std::string& locale, |
| 64 const std::string& ext) { |
| 65 std::string mappedLocale = GetIOSLocaleMapping(locale); |
| 66 std::string resource_file = FindFileInResource(base_name, mappedLocale, ext); |
| 67 if (resource_file.empty() && mappedLocale.length() > 2 && |
| 68 mappedLocale[2] == '-') { |
| 69 std::string language = mappedLocale.substr(0, 2); |
| 70 resource_file = FindFileInResource(base_name, language, ext); |
| 71 } |
| 72 if (resource_file.empty()) { |
| 73 // Default to English if resource is still not found. |
| 74 resource_file = FindFileInResource(base_name, kEnglishLocale, ext); |
| 75 } |
| 76 DCHECK(!resource_file.empty()); |
| 77 return resource_file; |
| 78 } |
| 79 |
| 80 std::string GetTermsOfServicePath() { |
| 81 const std::string& locale = GetApplicationContext()->GetApplicationLocale(); |
| 82 return GetLocalizedFileName(kChromeTosFilePrefix, locale, kHtmlFileExtension); |
| 83 } |
OLD | NEW |