/*
 * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.wso2.carbon.uuf.api.config;

import org.apache.commons.lang3.StringUtils;

import java.util.Locale;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Holds the i18n language resources of an UUF App.
 *
 * @since 1.0.0
 */
public class I18nResources {

    private static final String DEFAULT_LOCALE = "en-us";

    private final SortedMap<String, Properties> i18nResources = new TreeMap<>();

    /**
     * Adds the given language.
     *
     * @param locale language to be add
     * @param i18n   properties
     */
    public void addI18nResource(Locale locale, Properties i18n) {
        // Convert the language key to lower case before adding to the map. This is done because various browsers
        // send the locale in different formats.
        Properties i18nResource = this.i18nResources.get(locale.toLanguageTag());
        if (i18nResource == null) {
            this.i18nResources.put(locale.toLanguageTag(), i18n);
        } else {
            i18nResource.putAll(i18n);
        }
    }

    /**
     * Returns available languages in this resources collection.
     *
     * @return available languages
     */
    public Set<String> getAvailableLanguages() {
        return i18nResources.keySet();
    }

    /**
     * Returns the i18n resource for the given language.
     *
     * @param locale Locale of the i18n resource
     * @return i18n resource
     */
    public Properties getI18nResource(Locale locale) {
        return i18nResources.get(locale.toLanguageTag());
    }


    /**
     * Returns the local for the supported language.
     *
     * @param localeHeaderValue local header value
     * @return Locale object for language support
     */
    public Locale getLocale(String localeHeaderValue) {
        if (localeHeaderValue != null) {
            // example: en,en-us;q=0.7, en-au;q=0.3
            // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
            // Change the locale value to lower case because we store the language keys in lower case.
            String languageCode = localeHeaderValue.toLowerCase().split(",")[0];
            Locale locale = Locale.forLanguageTag(languageCode);
            String currentLang = null;
            for (String language : getAvailableLanguages()) {
                if (StringUtils.isNotEmpty(locale.toLanguageTag()) && language.equalsIgnoreCase(
                        locale.toLanguageTag())) {
                    currentLang = language;
                } else if (StringUtils.isNotEmpty(locale.getLanguage()) && language.startsWith(locale.getLanguage())) {
                    currentLang = language;
                }
            }
            if (currentLang == null) {
                currentLang = DEFAULT_LOCALE.replace("_", "-");
            }
            return Locale.forLanguageTag(currentLang);
        } else {
            return Locale.forLanguageTag(DEFAULT_LOCALE.replace("_", "-"));
        }
    }
}

@@ -2510,9 +2510,9 @@
 lue 
-!
+=
 = nu
@@ -2513,32 +2513,115 @@
  == null) %7B%0A    
+        return Locale.forLanguageTag(DEFAULT_LOCALE.replace(%22_%22, %22-%22));%0A        %7D%0A%0A
         // examp
@@ -2660,20 +2660,16 @@
         
-   
- 
 // https
@@ -2727,20 +2727,16 @@
 sec14.4%0A
-    
         
@@ -2830,28 +2830,24 @@
 se.%0A        
-   
- 
 String langu
@@ -2911,20 +2911,16 @@
         
-    
 Locale l
@@ -2968,28 +2968,24 @@
 e);%0A        
-   
- 
 String curre
@@ -2999,20 +2999,16 @@
 = null;%0A
-    
         
@@ -3065,28 +3065,24 @@
             
-   
- 
 if (StringUt
@@ -3170,20 +3170,16 @@
         
-    
 locale.t
@@ -3205,36 +3205,32 @@
                 
-   
- 
 currentLang = la
@@ -3229,36 +3229,32 @@
 ang = language;%0A
-    
             %7D el
@@ -3361,36 +3361,32 @@
                 
-   
- 
 currentLang = la
@@ -3405,33 +3405,26 @@
         
-    %7D%0A   
+%7D%0A
         
  %7D%0A     
@@ -3407,39 +3407,34 @@
       %7D%0A        
- 
 %7D%0A
-    
         if (curr
@@ -3456,36 +3456,32 @@
 ) %7B%0A            
-   
- 
 currentLang = DE
@@ -3524,25 +3524,18 @@
     
-    %7D%0A   
+%7D%0A
         
  ret
@@ -3522,33 +3522,32 @@
       %7D%0A        
- 
 return Locale.fo
@@ -3577,111 +3577,8 @@
 g);%0A
-        %7D else %7B%0A            return Locale.forLanguageTag(DEFAULT_LOCALE.replace(%22_%22, %22-%22));%0A        %7D%0A
     

add code review changes