<commit_before>// Copyright © 2015-2019 <NAME>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

/* @flow */

import Cookies from "js-cookie";
import {addLocaleData} from "react-intl";
import en from "react-intl/locale-data/en";
import es from "react-intl/locale-data/es";
import fi from "react-intl/locale-data/fi";
import id from "react-intl/locale-data/id";
import it from "react-intl/locale-data/it";
import nl from "react-intl/locale-data/nl";
import pt from "react-intl/locale-data/pt";
import translations from "./translations.json";
import sortBy from "lodash/sortBy";
import toPairs from "lodash/toPairs";

// TODO: create translations.json at build time, store the english version only in JSX tags to avoid getting out of sync
// https://medium.freecodecamp.com/internationalization-in-react-7264738274a0
// https://github.com/iam-peekay/inbox-react-intl/issues/1

addLocaleData([...en, ...es, ...fi, ...id, ...it, ...nl, ...pt]);

const languagesByCode = {
  en: "English",
  es: "Spanish",
  fi: "Finnish",
  id: "Indonesian",
  it: "Italian",
  nl: "Dutch",
  pt: "Portuguese",
};

export const languages =
  sortBy(
    toPairs(languagesByCode).map(([code, name]) => ({code, name})),
    ({name}) => name);

export function getMessages(language: string): {} {
  return translations[language];
}

function withoutRegionCode(language: string): string {
  return language && language.toLowerCase().split(/[_-]+/)[0]
}

export function changeLanguage(language: string): void {
  Cookies.set('lang', language, {expires: 3 * 365});
  window.location.reload();
}

const languagePreference: string[] = [
  Cookies.get('lang'),
  ...(navigator.languages || []),
  navigator.language,
  (navigator: any).userLanguage, // Internet Explorer
].flatMap(lang => [lang, withoutRegionCode(lang)]);

export const language: string = languagePreference.find(getMessages) || 'en';

export const messages: {} = getMessages(language);<commit_msg>💚 Flow Types doesn't recognize Array.flatMap; use Lodash instead<commit_after>// Copyright © 2015-2019 <NAME>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

/* @flow */

import Cookies from "js-cookie";
import {addLocaleData} from "react-intl";
import en from "react-intl/locale-data/en";
import es from "react-intl/locale-data/es";
import fi from "react-intl/locale-data/fi";
import id from "react-intl/locale-data/id";
import it from "react-intl/locale-data/it";
import nl from "react-intl/locale-data/nl";
import pt from "react-intl/locale-data/pt";
import translations from "./translations.json";
import sortBy from "lodash/sortBy";
import toPairs from "lodash/toPairs";
import flatMap from "lodash/flatMap";

// TODO: create translations.json at build time, store the english version only in JSX tags to avoid getting out of sync
// https://medium.freecodecamp.com/internationalization-in-react-7264738274a0
// https://github.com/iam-peekay/inbox-react-intl/issues/1

addLocaleData([...en, ...es, ...fi, ...id, ...it, ...nl, ...pt]);

const languagesByCode = {
  en: "English",
  es: "Spanish",
  fi: "Finnish",
  id: "Indonesian",
  it: "Italian",
  nl: "Dutch",
  pt: "Portuguese",
};

export const languages =
  sortBy(
    toPairs(languagesByCode).map(([code, name]) => ({code, name})),
    ({name}) => name);

export function getMessages(language: string): {} {
  return translations[language];
}

function withoutRegionCode(language: string): string {
  return language && language.toLowerCase().split(/[_-]+/)[0]
}

export function changeLanguage(language: string): void {
  Cookies.set('lang', language, {expires: 3 * 365});
  window.location.reload();
}

const languagePreference: string[] = flatMap([
  Cookies.get('lang'),
  ...(navigator.languages || []),
  navigator.language,
  (navigator: any).userLanguage, // Internet Explorer
], lang => [lang, withoutRegionCode(lang)]);

export const language: string = languagePreference.find(getMessages) || 'en';

export const messages: {} = getMessages(language);
{'fraction_non_alphanumeric': 0.16642406598738477, 'fraction_numerical': 0.014070839398350316, 'mean_word_length': 4.4036697247706424, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 4, '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': '6836789', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1322, 'n_words': 354}