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.
5 #include "ios/chrome/browser/ui/file_locations.h"
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"
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";
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
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);
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 : "";
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
40 std::string GetIOSLocaleMapping(const std::string& locale) {
41 if (locale == "pt") // Brazillian Portuguese
43 else if (locale == "zh-Hans") // Chinese Simplified script
45 else if (locale == "zh-Hant") // Chinese Traditional script
47 else if (locale == "es-MX")
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
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);
72 if (resource_file.empty() && mappedLocale.length() > 3 &&
73 mappedLocale[3] == '-') {
74 std::string language = mappedLocale.substr(0, 3);
75 resource_file = FindFileInResource(base_name, language, ext);
77 if (resource_file.empty()) {
78 // Default to English if resource is still not found.
79 resource_file = FindFileInResource(base_name, kEnglishLocale, ext);
81 DCHECK(!resource_file.empty());
85 std::string GetTermsOfServicePath() {
86 const std::string& locale = GetApplicationContext()->GetApplicationLocale();
87 return GetLocalizedFileName(kChromeTosFilePrefix, locale, kHtmlFileExtension);