Chromium Code Reviews| Index: ios/chrome/browser/ui/file_locations.mm |
| diff --git a/ios/chrome/browser/ui/file_locations.mm b/ios/chrome/browser/ui/file_locations.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..940dacaa272f915a12331cb8a1eb2ad08c9ff06c |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/file_locations.mm |
| @@ -0,0 +1,89 @@ |
| +// Copyright 2012 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 "ios/chrome/browser/ui/file_locations.h" |
| + |
| +#include "base/mac/bundle_locations.h" |
| +#include "base/mac/foundation_util.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "ios/chrome/browser/application_context.h" |
| + |
| +namespace { |
| +// English is the default locale for the Terms of Service. |
| +const char kEnglishLocale[] = "en"; |
| +// The prefix of the Chrome Terms of Service file name. |
| +const char kChromeTosFilePrefix[] = "terms"; |
| +// The file name extension for HTML files. |
| +const char kHtmlFileExtension[] = "html"; |
| +} // namespace |
| + |
| +namespace file_locations { |
| + |
| +// iOS uses certain locale initials that are different from Chrome's. |
| +// Most notably, "pt" means "pt-BR" (Brazillian Portuguese). This |
| +// function normalizes the locale into language-region code used by |
| +// Chrome. |
| +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
|
| + |
| +// Checks that the requested file exists in the application's resource |
| +// bundle and returns the filename. Returns an empty string if resource does not |
| +// exist. |
| +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.
|
| + const std::string& language, |
| + const std::string& ext) { |
| + std::string resource_file(base_name + "_" + language); |
| + BOOL exists = |
| + [base::mac::FrameworkBundle() |
| + URLForResource:[NSString stringWithUTF8String:resource_file.c_str()] |
| + withExtension:[NSString stringWithUTF8String:ext.c_str()]] != nil; |
| + return exists ? resource_file + "." + ext : ""; |
| +} |
| + |
| +// Returns a filename based on the base file name and file extension and |
| +// localized for the given locale. Checks the existence of the file based on |
| +// |locale| as follows: |
| +// * if there is a file for file_<locale>.<ext> |
| +// * if not, check the language part as follows file_<locale[0..]>.<ext> |
| +// * when all else fails, use the English locale, e.g. file_en.<ext>, which |
| +// must exist. |
| +// |locale| must be a valid Chrome locale as defined in file |
| +// ui/base/l10n/l10n_util.cc. This corresponds to a language or a country code, |
| +// e.g. "en", "en-US", "fr", etc. |
| +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
|
| + const std::string& locale, |
| + const std::string& ext) { |
| + std::string mappedLocale = iosLocaleMapping(locale); |
| + std::string resource_file = |
| + find_file_in_resource(base_name, mappedLocale, ext); |
| + if (resource_file.empty() && mappedLocale.length() > 2 && |
| + mappedLocale[2] == '-') { |
| + std::string language = mappedLocale.substr(0, 2); |
| + resource_file = find_file_in_resource(base_name, language, ext); |
| + } |
| + if (resource_file.empty()) { |
| + // Default to English if resource is still not found. |
| + resource_file = find_file_in_resource(base_name, kEnglishLocale, ext); |
| + } |
| + DCHECK(!resource_file.empty()); |
| + return resource_file; |
| +} |
| + |
| +std::string TermsOfService() { |
| + const std::string& locale = GetApplicationContext()->GetApplicationLocale(); |
| + return localized_file_name(kChromeTosFilePrefix, locale, kHtmlFileExtension); |
| +} |
| + |
| +std::string iosLocaleMapping(const std::string& locale) { |
| + if (locale == "pt") // Brazillian Portuguese |
| + return "pt-BR"; |
| + else if (locale == "zh-Hans") // Chinese Simplified script |
| + return "zh-CN"; |
| + else if (locale == "zh-Hant") // Chinese Traditional script |
| + return "zh-TW"; |
| + else if (locale == "es-MX") |
| + return "es-419"; |
| + return locale; |
| +} |
| + |
| +} // namespace file_locations |