2 * Copyright (C) 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package cz.cvut.fel.dce.barcodescanner;
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.preference.PreferenceManager;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Locale;
30 * Handles any locale-specific logic for the client.
34 public final class LocaleManager {
36 private static final String DEFAULT_TLD = "com";
37 private static final String DEFAULT_COUNTRY = "US";
38 private static final String DEFAULT_LANGUAGE = "en";
41 * Locales (well, countries) where Google web search is available.
42 * These should be kept in sync with our translations.
44 private static final Map<String,String> GOOGLE_COUNTRY_TLD;
46 GOOGLE_COUNTRY_TLD = new HashMap<>();
47 GOOGLE_COUNTRY_TLD.put("AR", "com.ar"); // ARGENTINA
48 GOOGLE_COUNTRY_TLD.put("AU", "com.au"); // AUSTRALIA
49 GOOGLE_COUNTRY_TLD.put("BR", "com.br"); // BRAZIL
50 GOOGLE_COUNTRY_TLD.put("BG", "bg"); // BULGARIA
51 GOOGLE_COUNTRY_TLD.put(Locale.CANADA.getCountry(), "ca");
52 GOOGLE_COUNTRY_TLD.put(Locale.CHINA.getCountry(), "cn");
53 GOOGLE_COUNTRY_TLD.put("CZ", "cz"); // CZECH REPUBLIC
54 GOOGLE_COUNTRY_TLD.put("DK", "dk"); // DENMARK
55 GOOGLE_COUNTRY_TLD.put("FI", "fi"); // FINLAND
56 GOOGLE_COUNTRY_TLD.put(Locale.FRANCE.getCountry(), "fr");
57 GOOGLE_COUNTRY_TLD.put(Locale.GERMANY.getCountry(), "de");
58 GOOGLE_COUNTRY_TLD.put("GR", "gr"); // GREECE
59 GOOGLE_COUNTRY_TLD.put("HU", "hu"); // HUNGARY
60 GOOGLE_COUNTRY_TLD.put("ID", "co.id"); // INDONESIA
61 GOOGLE_COUNTRY_TLD.put("IL", "co.il"); // ISRAEL
62 GOOGLE_COUNTRY_TLD.put(Locale.ITALY.getCountry(), "it");
63 GOOGLE_COUNTRY_TLD.put(Locale.JAPAN.getCountry(), "co.jp");
64 GOOGLE_COUNTRY_TLD.put(Locale.KOREA.getCountry(), "co.kr");
65 GOOGLE_COUNTRY_TLD.put("NL", "nl"); // NETHERLANDS
66 GOOGLE_COUNTRY_TLD.put("PL", "pl"); // POLAND
67 GOOGLE_COUNTRY_TLD.put("PT", "pt"); // PORTUGAL
68 GOOGLE_COUNTRY_TLD.put("RO", "ro"); // ROMANIA
69 GOOGLE_COUNTRY_TLD.put("RU", "ru"); // RUSSIA
70 GOOGLE_COUNTRY_TLD.put("SK", "sk"); // SLOVAK REPUBLIC
71 GOOGLE_COUNTRY_TLD.put("SI", "si"); // SLOVENIA
72 GOOGLE_COUNTRY_TLD.put("ES", "es"); // SPAIN
73 GOOGLE_COUNTRY_TLD.put("SE", "se"); // SWEDEN
74 GOOGLE_COUNTRY_TLD.put("CH", "ch"); // SWITZERLAND
75 GOOGLE_COUNTRY_TLD.put(Locale.TAIWAN.getCountry(), "tw");
76 GOOGLE_COUNTRY_TLD.put("TR", "com.tr"); // TURKEY
77 GOOGLE_COUNTRY_TLD.put(Locale.UK.getCountry(), "co.uk");
78 GOOGLE_COUNTRY_TLD.put(Locale.US.getCountry(), "com");
82 * Google Product Search for mobile is available in fewer countries than web search. See here:
83 * http://support.google.com/merchants/bin/answer.py?hl=en-GB&answer=160619
85 private static final Map<String,String> GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD;
87 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD = new HashMap<>();
88 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("AU", "com.au"); // AUSTRALIA
89 //GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.CHINA.getCountry(), "cn");
90 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.FRANCE.getCountry(), "fr");
91 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.GERMANY.getCountry(), "de");
92 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.ITALY.getCountry(), "it");
93 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.JAPAN.getCountry(), "co.jp");
94 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("NL", "nl"); // NETHERLANDS
95 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("ES", "es"); // SPAIN
96 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("CH", "ch"); // SWITZERLAND
97 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.UK.getCountry(), "co.uk");
98 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.US.getCountry(), "com");
102 * Book search is offered everywhere that web search is available.
104 private static final Map<String,String> GOOGLE_BOOK_SEARCH_COUNTRY_TLD = GOOGLE_COUNTRY_TLD;
106 private static final Collection<String> TRANSLATED_HELP_ASSET_LANGUAGES =
107 Arrays.asList("de", "en", "es", "fr", "it", "ja", "ko", "nl", "pt", "ru", "zh-rCN", "zh-rTW", "zh-rHK");
109 private LocaleManager() {}
112 * @param context application's {@link android.content.Context}
113 * @return country-specific TLD suffix appropriate for the current default locale
114 * (e.g. "co.uk" for the United Kingdom)
116 public static String getCountryTLD(Context context) {
117 return doGetTLD(GOOGLE_COUNTRY_TLD, context);
121 * The same as above, but specifically for Google Product Search.
123 * @param context application's {@link android.content.Context}
124 * @return The top-level domain to use.
126 public static String getProductSearchCountryTLD(Context context) {
127 return doGetTLD(GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD, context);
131 * The same as above, but specifically for Google Book Search.
133 * @param context application's {@link android.content.Context}
134 * @return The top-level domain to use.
136 public static String getBookSearchCountryTLD(Context context) {
137 return doGetTLD(GOOGLE_BOOK_SEARCH_COUNTRY_TLD, context);
141 * Does a given URL point to Google Book Search, regardless of domain.
143 * @param url The address to check.
144 * @return True if this is a Book Search URL.
146 public static boolean isBookSearchUrl(String url) {
147 return url.startsWith("http://google.com/books") || url.startsWith("http://books.google.");
150 private static String getSystemCountry() {
151 Locale locale = Locale.getDefault();
152 return locale == null ? DEFAULT_COUNTRY : locale.getCountry();
155 private static String getSystemLanguage() {
156 Locale locale = Locale.getDefault();
157 if (locale == null) {
158 return DEFAULT_LANGUAGE;
160 String language = locale.getLanguage();
161 // Special case Chinese
162 if (Locale.SIMPLIFIED_CHINESE.getLanguage().equals(language)) {
163 return language + "-r" + getSystemCountry();
168 public static String getTranslatedAssetLanguage() {
169 String language = getSystemLanguage();
170 return TRANSLATED_HELP_ASSET_LANGUAGES.contains(language) ? language : DEFAULT_LANGUAGE;
173 private static String doGetTLD(Map<String,String> map, Context context) {
174 String tld = map.get(getCountry(context));
175 return tld == null ? DEFAULT_TLD : tld;
178 public static String getCountry(Context context) {
179 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
180 String countryOverride = prefs.getString(PreferencesActivity.KEY_SEARCH_COUNTRY, "-");
181 if (countryOverride != null && !countryOverride.isEmpty() && !"-".equals(countryOverride)) {
182 return countryOverride;
184 return getSystemCountry();