Archana Sharma
Archana Sharma

Reputation: 2071

How to translate mobile app text using language drop-down inside react-native app?

I want to add translate drop-down inside mobile app to translate the app text without using language json file. I just want to add the select list at the top of app to change the language of text, like this: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_google_translate

Is this possible with react native app?

Upvotes: 1

Views: 656

Answers (1)

Ketan Ramteke
Ketan Ramteke

Reputation: 10655

Here is an example of how you can achieve that.

Working Example: Expo Snack

enter image description here


import React, { useState } from 'react';
import { Text, View, StyleSheet, Picker } from 'react-native';
import Constants from 'expo-constants';

const engToHindi = {
  english: 'hello',
  hindi: 'नमस्कार',
};
export default function App() {
  const [language, setLanguage] = useState('english');
  return (
    <View style={styles.container}>
      <Picker
        selectedValue={language}
        style={{ height: 50, width: 100 }}
        onValueChange={(itemValue) => setLanguage(itemValue)}>
        <Picker.Item label="English" value="english" />
        <Picker.Item label="हिन्दी" value="hindi" />
      </Picker>
      <Text>{engToHindi[language]}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});

Online Implementation using Google Translate API would be possible but I don't have the API keys so could not give you the example of that implementation.

Upvotes: 1

Related Questions