1 /* 2 * Copyright (C) 2008 ZXing authors 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.google.zxing.client.android; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.preference.PreferenceManager; 22 23 import java.util.Arrays; 24 import java.util.Collection; 25 import java.util.Locale; 26 import java.util.Map; 27 import java.util.HashMap; 28 29 /** 30 * Handles any locale-specific logic for the client. 31 * 32 * @author Sean Owen 33 */ 34 public final class LocaleManager { 35 36 private static final String DEFAULT_TLD = "com"; 37 private static final String DEFAULT_COUNTRY = "US"; 38 private static final String DEFAULT_LANGUAGE = "en"; 39 40 /** 41 * Locales (well, countries) where Google web search is available. 42 * These should be kept in sync with our translations. 43 */ 44 private static final Map<String,String> GOOGLE_COUNTRY_TLD; 45 static { 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 Locale.CANADA.getCountry()51 GOOGLE_COUNTRY_TLD.put(Locale.CANADA.getCountry(), "ca"); Locale.CHINA.getCountry()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 Locale.FRANCE.getCountry()56 GOOGLE_COUNTRY_TLD.put(Locale.FRANCE.getCountry(), "fr"); Locale.GERMANY.getCountry()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 Locale.ITALY.getCountry()62 GOOGLE_COUNTRY_TLD.put(Locale.ITALY.getCountry(), "it"); Locale.JAPAN.getCountry()63 GOOGLE_COUNTRY_TLD.put(Locale.JAPAN.getCountry(), "co.jp"); Locale.KOREA.getCountry()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 Locale.TAIWAN.getCountry()75 GOOGLE_COUNTRY_TLD.put(Locale.TAIWAN.getCountry(), "tw"); 76 GOOGLE_COUNTRY_TLD.put("TR", "com.tr"); // TURKEY 77 GOOGLE_COUNTRY_TLD.put("UA", "com.ua"); // UKRAINE Locale.UK.getCountry()78 GOOGLE_COUNTRY_TLD.put(Locale.UK.getCountry(), "co.uk"); Locale.US.getCountry()79 GOOGLE_COUNTRY_TLD.put(Locale.US.getCountry(), "com"); 80 } 81 82 /** 83 * Google Product Search for mobile is available in fewer countries than web search. See here: 84 * http://support.google.com/merchants/bin/answer.py?hl=en-GB&answer=160619 85 */ 86 private static final Map<String,String> GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD; 87 static { 88 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD = new HashMap<>(); 89 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("AU", "com.au"); // AUSTRALIA 90 //GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.CHINA.getCountry(), "cn"); Locale.FRANCE.getCountry()91 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.FRANCE.getCountry(), "fr"); Locale.GERMANY.getCountry()92 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.GERMANY.getCountry(), "de"); Locale.ITALY.getCountry()93 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.ITALY.getCountry(), "it"); Locale.JAPAN.getCountry()94 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.JAPAN.getCountry(), "co.jp"); 95 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("NL", "nl"); // NETHERLANDS 96 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("ES", "es"); // SPAIN 97 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put("CH", "ch"); // SWITZERLAND Locale.UK.getCountry()98 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.UK.getCountry(), "co.uk"); Locale.US.getCountry()99 GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.US.getCountry(), "com"); 100 } 101 102 /** 103 * Book search is offered everywhere that web search is available. 104 */ 105 private static final Map<String,String> GOOGLE_BOOK_SEARCH_COUNTRY_TLD = GOOGLE_COUNTRY_TLD; 106 107 private static final Collection<String> TRANSLATED_HELP_ASSET_LANGUAGES = 108 Arrays.asList("de", "en", "es", "fa", "fr", "it", "ja", "ko", "nl", "pt", "ru", "uk", "zh-rCN", "zh"); 109 LocaleManager()110 private LocaleManager() {} 111 112 /** 113 * @param context application's {@link Context} 114 * @return country-specific TLD suffix appropriate for the current default locale 115 * (e.g. "co.uk" for the United Kingdom) 116 */ getCountryTLD(Context context)117 public static String getCountryTLD(Context context) { 118 return doGetTLD(GOOGLE_COUNTRY_TLD, context); 119 } 120 121 /** 122 * The same as above, but specifically for Google Product Search. 123 * 124 * @param context application's {@link Context} 125 * @return The top-level domain to use. 126 */ getProductSearchCountryTLD(Context context)127 public static String getProductSearchCountryTLD(Context context) { 128 return doGetTLD(GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD, context); 129 } 130 131 /** 132 * The same as above, but specifically for Google Book Search. 133 * 134 * @param context application's {@link Context} 135 * @return The top-level domain to use. 136 */ getBookSearchCountryTLD(Context context)137 public static String getBookSearchCountryTLD(Context context) { 138 return doGetTLD(GOOGLE_BOOK_SEARCH_COUNTRY_TLD, context); 139 } 140 141 /** 142 * Does a given URL point to Google Book Search, regardless of domain. 143 * 144 * @param url The address to check. 145 * @return True if this is a Book Search URL. 146 */ isBookSearchUrl(String url)147 public static boolean isBookSearchUrl(String url) { 148 return url.startsWith("http://google.com/books") || url.startsWith("http://books.google."); 149 } 150 getSystemCountry()151 private static String getSystemCountry() { 152 Locale locale = Locale.getDefault(); 153 return locale == null ? DEFAULT_COUNTRY : locale.getCountry(); 154 } 155 getSystemLanguage()156 private static String getSystemLanguage() { 157 Locale locale = Locale.getDefault(); 158 if (locale == null) { 159 return DEFAULT_LANGUAGE; 160 } 161 // Special case Chinese 162 if (Locale.SIMPLIFIED_CHINESE.equals(locale)) { 163 return "zh-rCN"; 164 } 165 return locale.getLanguage(); 166 } 167 getTranslatedAssetLanguage()168 static String getTranslatedAssetLanguage() { 169 String language = getSystemLanguage(); 170 return TRANSLATED_HELP_ASSET_LANGUAGES.contains(language) ? language : DEFAULT_LANGUAGE; 171 } 172 doGetTLD(Map<String,String> map, Context context)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; 176 } 177 getCountry(Context context)178 private 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; 183 } 184 return getSystemCountry(); 185 } 186 187 } 188