summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/LocaleUtils.java
blob: 8e4addc7b0758c5aa282fc3d9cc0b1d33b4b2e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* -*- Mode: Java; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*- */
/* vim: set ts=2 et sw=2: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.util;

import android.text.TextUtils;
import java.util.Locale;

public class LocaleUtils {
  // Locale.getLanguage() may return legacy language code until Java 17
  // https://developer.android.com/reference/java/util/Locale#legacy_language_codes
  public static String getLanguageTagForAcceptLanguage(final Locale locale) {
    String language = locale.getLanguage();
    if (language.equals("in")) {
      language = "id";
    } else if (language.equals("iw")) {
      language = "he";
    } else if (language.equals("ji")) {
      language = "yi";
    }
    final StringBuilder out = new StringBuilder(language);
    final String country = locale.getCountry();
    if (!TextUtils.isEmpty(country)) {
      out.append('-').append(country);
    }
    // e.g. "en", "en-US"
    return out.toString();
  }
}