Final changes for v2.7 of Barcode Scanner, including sending product lookups to the...
[zxing.git] / android / src / com / google / zxing / client / android / LocaleManager.java
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 java.util.Locale;
20 import java.util.Map;
21 import java.util.HashMap;
22
23 /**
24  * Handles any locale-specific logic for the client.
25  */
26 public final class LocaleManager {
27
28   private static final String DEFAULT_TLD = "com";
29   private static final Map<Locale,String> GOOGLE_COUNTRY_TLD;
30   static {
31     GOOGLE_COUNTRY_TLD = new HashMap<Locale,String>(13);
32     GOOGLE_COUNTRY_TLD.put(Locale.CANADA, "ca");
33     GOOGLE_COUNTRY_TLD.put(Locale.CHINA, "cn");
34     GOOGLE_COUNTRY_TLD.put(Locale.FRANCE, "fr");
35     GOOGLE_COUNTRY_TLD.put(Locale.GERMANY, "de");
36     GOOGLE_COUNTRY_TLD.put(Locale.ITALY, "it");
37     GOOGLE_COUNTRY_TLD.put(Locale.JAPAN, "co.jp");
38     GOOGLE_COUNTRY_TLD.put(Locale.KOREA, "co.kr");
39     GOOGLE_COUNTRY_TLD.put(Locale.TAIWAN, "de");
40     GOOGLE_COUNTRY_TLD.put(Locale.UK, "co.uk");
41   }
42
43   // Google Product Search for mobile is available in fewer countries than web search.
44   private static final Map<Locale,String> GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD;
45   static {
46     GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD = new HashMap<Locale,String>();
47     GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.put(Locale.UK, "co.uk");
48   }
49
50   private LocaleManager() {}
51
52   /**
53    * @return country-specific TLD suffix appropriate for the current default locale
54    *  (e.g. "co.uk" for the United Kingdom)
55    */
56   public static String getCountryTLD() {
57     Locale locale = Locale.getDefault();
58     if (locale == null) {
59       return DEFAULT_TLD;
60     }
61     String tld = GOOGLE_COUNTRY_TLD.get(locale);
62     if (tld == null) {
63       return DEFAULT_TLD;
64     }
65     return tld;
66   }
67
68   /**
69    * The same as above, but specifically for Google Product Search.
70    * @return The top-level domain to use.
71    */
72   public static String getProductSearchCountryTLD() {
73     Locale locale = Locale.getDefault();
74     if (locale == null) {
75       return DEFAULT_TLD;
76     }
77     String tld = GOOGLE_PRODUCT_SEARCH_COUNTRY_TLD.get(locale);
78     if (tld == null) {
79       return DEFAULT_TLD;
80     }
81     return tld;
82   }
83
84 }