001    /*
002     *  Copyright 2012 GWT-Bootstrap
003     *
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.github.gwtbootstrap.datepicker.client.ui.util;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import com.github.gwtbootstrap.client.ui.resources.JavaScriptInjector;
022    import com.github.gwtbootstrap.datepicker.client.ui.resources.Resources;
023    import com.google.gwt.resources.client.TextResource;
024    
025    /**
026     * A utility class to get the User's Browser Locale.
027     *
028     * @author Carlos A Becker
029     * @since 2.0.4.0
030     */
031    public class LocaleUtil {
032    
033        private static String locale = null;
034        private static String LANGUAGE = null;
035    
036        private static List<String> loaded = new ArrayList<String>();
037    
038        public static String getLanguage() {
039            if (LANGUAGE == null) {
040                setupLocale();
041            }
042            return LANGUAGE;
043        }
044    
045        /**
046         * Get the string of locale based on user's browser configuration.
047         *
048         * @return
049         */
050        public static String getLocale() {
051            if (locale == null) {
052                locale = getBrowserLocale();
053            }
054            return locale;
055        }
056    
057        public static TextResource getLocaleJsResource() {
058            String locale = getLocale();
059            if (locale == null) {
060                return null;
061            }
062            return setupLocale();
063        }
064    
065        private static final native String getBrowserLocale() /*-{
066            return $wnd.navigator.userLanguage || $wnd.navigator.language;
067        }-*/;
068    
069    
070        public static final void forceLocale(String locale_) {
071            locale = locale_;
072            TextResource t = setupLocale();
073            if (!loaded.contains(locale) && t != null) {
074                JavaScriptInjector.inject(t.getText());
075            }
076        }
077    
078        private static TextResource setupLocale() {
079            Resources r = Resources.RESOURCES;
080            TextResource tr = null;
081    
082            /*
083               Script used to gen the basic if-else block:
084               for a in `ls`; do echo "else if(locale.equals(\"`echo $a | cut -f2 -d.`\")) { tr = r.`echo $a | cut -f2 -d.`(); LANGUAGE = \"`echo $a | cut -f2 -d.`\"; }"; done
085            */
086    
087    
088            if (locale.equalsIgnoreCase("pt-br") || locale.contains("pt")) {
089                tr = r.br();
090                LANGUAGE = "br";
091            } else if (locale.equals("da")) {
092                tr = r.da();
093                LANGUAGE = "da";
094            } else if (locale.equals("de")) {
095                tr = r.de();
096                LANGUAGE = "de";
097            } else if (locale.equals("es")) {
098                tr = r.es();
099                LANGUAGE = "es";
100            } else if (locale.equals("fi")) {
101                tr = r.fi();
102                LANGUAGE = "fi";
103            } else if (locale.equals("fr")) {
104                tr = r.fr();
105                LANGUAGE = "fr";
106            } else if (locale.equals("is")) {
107                tr = r.is();
108                LANGUAGE = "is";
109            } else if (locale.equals("it")) {
110                tr = r.it();
111                LANGUAGE = "it";
112            } else if (locale.equals("lv")) {
113                tr = r.lv();
114                LANGUAGE = "lv";
115            } else if (locale.equals("nb")) {
116                tr = r.nb();
117                LANGUAGE = "nb";
118            } else if (locale.equals("nl")) {
119                tr = r.nl();
120                LANGUAGE = "nl";
121            } else if (locale.equals("pl")) {
122                tr = r.pl();
123                LANGUAGE = "pl";
124            } else if (locale.equals("ru")) {
125                tr = r.ru();
126                LANGUAGE = "ru";
127            } else if (locale.equals("sv")) {
128                tr = r.sv();
129                LANGUAGE = "sv";
130            } else if (locale.equals("th")) {
131                tr = r.th();
132                LANGUAGE = "th";
133            } else if (locale.equals("tr")) {
134                tr = r.tr();
135                LANGUAGE = "tr";
136            } else if (locale.equals("zh-CN")) {
137                tr = r.zh_CN();
138                LANGUAGE = "zh-CN";
139            } else {
140                tr = null;
141                LANGUAGE = "en";
142            }
143    
144            loaded.add(LANGUAGE);
145    
146            return tr;
147        }
148    }