import { Picker } from '@react-native-picker/picker';
import * as Localization from 'expo-localization';
import i18n from 'i18n-js';
import chunk from 'lodash/chunk';
import React from 'react';
import { ScrollView, StyleSheet, Text, View } from 'react-native';
import DeprecatedHeading from '../components/DeprecatedHeading';
import HeadingText from '../components/HeadingText';
import ListButton from '../components/ListButton';
import MonoText from '../components/MonoText';
i18n.fallbacks = true;
i18n.locale = Localization.getLocales()[0].languageTag;
i18n.translations = {
en: {
phrase: 'Hello my friend',
default: 'English language only',
},
es: {
phrase: 'Hola mi amigo',
},
};
interface State {
isoCurrencyCodes: any;
currentLocale: any;
preferredLocales: any;
locale?: string;
}
function HooksLocalizationSection() {
const locales = Localization.useLocales();
const calendars = Localization.useCalendars();
return (
<>
Locales in Preference Order (hook)
{JSON.stringify(locales, null, 2)}
Calendars in Preference Order (hook)
{JSON.stringify(calendars, null, 2)}
>
);
}
// See: https://github.com/expo/expo/pull/10229#discussion_r490961694
// eslint-disable-next-line @typescript-eslint/ban-types
export default class LocalizationScreen extends React.Component<{}, State> {
static navigationOptions = {
title: 'Localization',
};
readonly state: State = {
currentLocale: Localization.locale,
preferredLocales: Localization.locales,
isoCurrencyCodes: Localization.isoCurrencyCodes,
};
queryPreferredLocales = async () => {
const preferredLocales = Localization.locales;
const currentLocale = Localization.locale;
this.setState({ preferredLocales, currentLocale });
};
queryCurrencyCodes = async () => {
if (this.state.isoCurrencyCodes.length === 0) {
const isoCurrencyCodes = Localization.isoCurrencyCodes;
this.setState({ isoCurrencyCodes });
}
};
prettyFormatCurrency = () => {
let buffer = '';
let seenCount = 0;
const sample = this.state.isoCurrencyCodes.slice(0, 100);
const grouped = chunk(sample, 10);
let drilldownIndex = 0;
let currentColumn = 0;
while (true) {
while (true) {
if (seenCount === sample.length) return buffer;
if (currentColumn === grouped.length) {
currentColumn = 0;
buffer += '\n';
drilldownIndex++;
continue;
}
buffer += `${grouped[currentColumn][drilldownIndex]}\t`;
seenCount++;
currentColumn++;
}
}
};
changeLocale = (locale: string) => {
i18n.locale = locale;
this.setState({ locale });
};
render() {
return (
Locales in Preference Order
{JSON.stringify(Localization.getLocales(), null, 2)}
Calendars in Preference Order
{JSON.stringify(Localization.getCalendars(), null, 2)}
Localization Table
this.changeLocale(`${value}`)}>
Exists in Both:
{this.state.currentLocale ? i18n.t('phrase') : ''}
Default Case Only:
{this.state.currentLocale ? i18n.t('default') : ''}
Current Locale
{JSON.stringify(this.state.currentLocale, null, 2)}
Locales in Preference Order
{this.state.preferredLocales && this.state.preferredLocales.length > 0 && (
{JSON.stringify(this.state.preferredLocales, null, 2)}
)}
Currency Codes
{this.state.isoCurrencyCodes && this.state.isoCurrencyCodes.length > 0 && (
{this.prettyFormatCurrency()}
)}
);
}
}
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
justifyContent: 'space-between',
},
languageBox: {
padding: 10,
borderWidth: 1,
},
picker: {
borderWidth: 1,
padding: 0,
margin: 0,
},
container: {
padding: 10,
},
});