Skip to content

Instantly share code, notes, and snippets.

@danialkalbasi
Last active October 27, 2020 07:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danialkalbasi/350d960a6d28016a3331f6a9c7baefa4 to your computer and use it in GitHub Desktop.
Save danialkalbasi/350d960a6d28016a3331f6a9c7baefa4 to your computer and use it in GitHub Desktop.
Power Translator Demo
import React, { Component } from 'react';
import { PowerTranslator, ProviderTypes, TranslatorConfiguration } from 'react-native-power-translator';
import { View, ScrollView, TouchableOpacity, Text } from 'react-native';
export default class PowerTranslatorDemo extends Component {
constructor() {
super();
this.state = { languageCode: 'fr' };
}
render() {
const styles = this.getStyles();
TranslatorConfiguration.setConfig(ProviderTypes.Google, 'AIzaSyB5ip6KC-9KCIjO9Q7Rm47dYJDmOdjLgM0', this.state.languageCode);
return (
<ScrollView style={styles.container}>
<View style={styles.languageBar}>
<TouchableOpacity onPress={() => { this.changeLanguage('en') }}><Text style={styles.p}>English</Text></TouchableOpacity>
<TouchableOpacity onPress={() => { this.changeLanguage('fr') }}><Text style={styles.p}>French</Text></TouchableOpacity>
<TouchableOpacity onPress={() => { this.changeLanguage('ru') }}><Text style={styles.p}>Russian</Text></TouchableOpacity>
<TouchableOpacity onPress={() => { this.changeLanguage('de') }}><Text style={styles.p}>German</Text></TouchableOpacity>
</View>
<View>
<PowerTranslator style={styles.title} text={'A Confucian Revival Began'} />
<PowerTranslator style={styles.subtitle} text={'Author: Confucianism'} />
</View>
<View style={styles.section}>
<PowerTranslator style={styles.p} text={'Confucianism was particularly strong during the Han Dynasty, whose greatest thinker was Dong Zhongshu, who integrated Confucianism with the thoughts of the Zhongshu School and the theory of the Five Elements. He also was a promoter of the New Text school, which considered Confucius as a divine figure and a spiritual ruler of China, who foresaw and started the evolution of the world towards the Universal Peace.'} />
</View>
<View style={styles.section}>
<PowerTranslator style={styles.p} text={'Confucianism was particularly strong during the Han Dynasty, whose greatest thinker was Dong Zhongshu, who integrated Confucianism with the thoughts of the Zhongshu School and the theory of the Five Elements. He also was a promoter of the New Text school, which considered Confucius as a divine figure and a spiritual ruler of China, who foresaw and started the evolution of the world towards the Universal Peace.'} />
</View>
<View style={styles.section}>
<PowerTranslator style={styles.p} text={'Engineering physics or engineering science refers to the study of the combined disciplines of physics'} />
</View>
</ScrollView>
);
}
getStyles() {
return {
container: {
padding: 40,
backgroundColor: '#FAFAFA',
},
section: {
marginTop: 15,
marginBottom: 15,
},
title: {
marginTop: 80,
marginBottom: 5,
fontWeight: 'bold',
fontSize: 38,
lineHeight: 38
},
subtitle: {
color: '#B3B3B3',
},
p: {
color: '#828280',
lineHeight: 24
},
languageBar: {
flexDirection: 'row',
justifyContent: 'space-between'
},
languageBarItem: {
color: '#828280',
}
}
}
changeLanguage(languageCode) {
this.setState({ languageCode: languageCode });
}
}
@archanasantsoh
Copy link

archanasantsoh commented Nov 3, 2017

Pls guide I got this error,
my email id is archana.whatkar@gmail.com
screen shot 2017-11-03 at 4 50 13 pm

@waqaramjad
Copy link

Only getting warning when sending wrong language code .

TranslatorFactory.createTranslator()
.translate(MainLanguages[currentKey], langCode)
.then(translated => {})

Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'translations' of undefined

@andy021
Copy link

andy021 commented Oct 22, 2019

Why does your Demo show the translated text without the quotations around it but when I use it it has ugly quotes around all the text?

@Arslan24
Copy link

Arslan24 commented Sep 7, 2020

--Possible Unhandled Promise Rejection (id: 0):
--TypeError: Cannot read property 'translations' of undefined..

Getting this issue. anyone able to Fixthis ?

@harsha-kotapati
Copy link

for me am getting unable TypeError: undefined is not an object (evaluating 'translator.translate') any answers plz

@Arslan24
Copy link

@harsha-kotapati This power translator didnt work for me. so check the above link for a solution
danialkalbasi/react-native-power-translator#18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment