// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.base;

import java.util.Locale;

/**
 * This class provides the locale related methods for the native library.
 */
@JNINamespace("base::android")
public class LocaleUtils {

    // This is mirrored from base/i18n/rtl.h. Please keep in sync.
    public static final int UNKNOWN_DIRECTION = 0;
    public static final int RIGHT_TO_LEFT = 1;
    public static final int LEFT_TO_RIGHT = 2;

    private LocaleUtils() { /* cannot be instantiated */ }

    /**
     * @return the default locale, translating Android deprecated
     * language codes into the modern ones used by Chromium.
     */
    @CalledByNative
    public static String getDefaultLocale() {
        Locale locale = Locale.getDefault();
        String language = locale.getLanguage();
        String country = locale.getCountry();

        // Android uses deprecated lanuages codes for Hebrew, Indonesian and
        // Yiddish but Chromium uses the updated codes, so apply a mapping.
        // See http://developer.android.com/reference/java/util/Locale.html
        if ("iw".equals(language)) {
            language = "he";
        } else if ("in".equals(language)) {
            language = "id";
        } else if ("ji".equals(language)) {
            language = "yi";
        }
        return country.isEmpty() ? language : language + "-" + country;
    }

    @CalledByNative
    private static Locale getJavaLocale(String language, String country, String variant) {
        return new Locale(language, country, variant);
    }

    @CalledByNative
    private static String getDisplayNameForLocale(Locale locale, Locale displayLocale) {
        return locale.getDisplayName(displayLocale);
    }

    /**
     * Jni binding to base::i18n::IsRTL.
     * @return true if the current locale is right to left.
     */
    public static boolean isRtl() {
        return nativeIsRTL();
    }

    /**
     * Jni binding to base::i18n::GetFirstStrongCharacterDirection
     * @param string String to decide the direction.
     * @return One of the UNKNOWN_DIRECTION, RIGHT_TO_LEFT, and LEFT_TO_RIGHT.
     */
    public static int getFirstStrongCharacterDirection(String string) {
        return nativeGetFirstStrongCharacterDirection(string);
    }

    private static native boolean nativeIsRTL();

    private static native int nativeGetFirstStrongCharacterDirection(String string);
}

@@ -1040,17 +1040,20 @@
 r Hebrew
-,
+ and
  Indones
@@ -1060,31 +1060,8 @@
 ian 
-and%0A        // Yiddish 
 but 
@@ -1077,16 +1077,27 @@
 uses the
+%0A        //
  updated
@@ -1106,11 +1106,94 @@
 odes
-, s
+. Also, Android uses %22tl%22 while Chromium uses %22fil%22 for Tagalog/Filipino.%0A        // S
 o ap
@@ -1446,10 +1446,10 @@
 f (%22
-ji
+tl
 %22.eq
@@ -1494,10 +1494,11 @@
  = %22
-yi
+fil
 %22;%0A 

Fix missing Filipino/Tagalog translations.