<commit_before>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2011 <NAME> (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.gui;

import org.languagetool.Language;

import javax.swing.*;
import java.util.*;

/**
 * Combo box with list of available languages.
 */
public class LanguageComboBox extends JComboBox {

  private final ResourceBundle messages;

  public LanguageComboBox(ResourceBundle messages) {
    this.messages = messages;
    populateLanguageBox();
  }

  void populateLanguageBox() {
    removeAllItems();
    final List<I18nLanguage> i18nLanguages = getAllLanguages();
    preselectDefaultLanguage(i18nLanguages);
  }

  private List<I18nLanguage> getAllLanguages() {
    final List<I18nLanguage> i18nLanguages = new ArrayList<I18nLanguage>();
    for (Language language : Language.LANGUAGES) {
      if (language != Language.DEMO) {
        i18nLanguages.add(new I18nLanguage(language, messages));
      }
    }
    Collections.sort(i18nLanguages);
    return i18nLanguages;
  }

  private void preselectDefaultLanguage(List<I18nLanguage> i18nLanguages) {
    final String defaultLocale = Locale.getDefault().getLanguage();
    final String defaultGuiLocale = getDefaultGuiLanguage(defaultLocale);
    for (final I18nLanguage i18nLanguage : i18nLanguages) {
      addItem(i18nLanguage);
      if (i18nLanguage.toString().equals(defaultGuiLocale)) {
        setSelectedItem(i18nLanguage);
      }
    }
  }

  private String getDefaultGuiLanguage(String defaultLocale) {
    String defaultGuiLocale = null;
    try {
      defaultGuiLocale = messages.getString(defaultLocale);
    } catch (final MissingResourceException e) {
      // language not supported, so don't select a default
    }
    return defaultGuiLocale;
  }

}<commit_msg>check the user's country setting to choose a more specific default<commit_after>/* LanguageTool, a natural language style checker 
 * Copyright (C) 2011 <NAME> (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.gui;

import org.languagetool.Language;

import javax.swing.*;
import java.util.*;

/**
 * Combo box with list of available languages.
 */
public class LanguageComboBox extends JComboBox {

  private final ResourceBundle messages;

  public LanguageComboBox(ResourceBundle messages) {
    this.messages = messages;
    populateLanguageBox();
  }

  void populateLanguageBox() {
    removeAllItems();
    final List<I18nLanguage> i18nLanguages = getAllLanguages();
    preselectDefaultLanguage(i18nLanguages);
  }

  private List<I18nLanguage> getAllLanguages() {
    final List<I18nLanguage> i18nLanguages = new ArrayList<I18nLanguage>();
    for (Language language : Language.LANGUAGES) {
      if (language != Language.DEMO) {
        i18nLanguages.add(new I18nLanguage(language, messages));
      }
    }
    Collections.sort(i18nLanguages);
    return i18nLanguages;
  }

  private void preselectDefaultLanguage(List<I18nLanguage> i18nLanguages) {
    final String defaultGuiLocale = getDefaultGuiLanguage(Locale.getDefault());
    for (final I18nLanguage i18nLanguage : i18nLanguages) {
      addItem(i18nLanguage);
      if (i18nLanguage.toString().equals(defaultGuiLocale)) {
        setSelectedItem(i18nLanguage);
      }
    }
  }

  private String getDefaultGuiLanguage(Locale defaultLocale) {
    String defaultGuiLocale = null;
    try {
      defaultGuiLocale = messages.getString(defaultLocale.getLanguage() + "-" + defaultLocale.getCountry());
    } catch (final MissingResourceException e) {
      // this specific language/variant combination is not supported
      try {
        defaultGuiLocale = messages.getString(defaultLocale.getLanguage());
      } catch (final MissingResourceException e2) {
        // language not supported, so don't select a default
      }
    }
    return defaultGuiLocale;
  }

}
{'fraction_non_alphanumeric': 0.08203198494825964, 'fraction_numerical': 0.020884289746001882, 'mean_word_length': 3.881542699724518, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5630324', 'n_tokens_mistral': 1525, 'n_tokens_neox': 1435, 'n_words': 485}