Change language based on user selection on expo

Viewed 2400

Just wondering if there's any way to change the language of an expo app with typescript based on user selection (so not device language). I read through the docs on expo https://docs.expo.io/versions/latest/sdk/localization/ but there doesn't seem to be anything on it.

Below is what I have currently, and it's only working for device set language.

import * as Localization from 'expo-localization';
import I18n from 'i18n-js';

// import translations
import en from './en.json'
import zh from './zh.json'

// bind translations to i18n
I18n.translations = {
  en,
  zh
}

// set phones language
const getLanguage = async() => {
  try {
    const choice = Localization.locale
    I18n.locale = choice.substr(0, 2)
    I18n.initAsync()
  } catch (error) {
    console.log(error)
  }
}

getLanguage()

// export function
export function t(name: string) {
  return I18n.t(name)
}

Only thing now is that I don't know how to pass a language based on user input.

2 Answers
Related