I'm trying to render some Arabic text which is in the form of Unicode.
I have already tried to render this using Text component like
const arabic = "سُبْحَانَ اللهِ وَبِحَمْدِهِ"
render(){
return(
<Text>
{arabic}
</Text>
)
}
it renders the Unicode as it is, but writing it this way
render(){
return(
<Text>
سُبْحَانَ
#1575;للهِ
وَبِحَمْدِ
هِ
</Text>
)
}
renders the correct output
If you want to store unicode characters / html entities in a variable, you need to replace the html entity with the unicode number.
For example:
const arabic = "س";
needs to be replaced with:
const arabic = "\u0633";
The are several unicode tables online where you can translate your html entity to the raw unicode number.
Working example:
https://snack.expo.io/BJp-jL004
UPDATE with second approach:
Instead of a manual translation of the html entities to unicode numbers, you can use the npm module html-entities. Here the biggest advantage is, that you can use the regular <Text>
Component to render your characters.
Here is an example:
import { Html5Entities } from 'html-entities';
const arabic = "سُبْحَانَ اللهِ وَبِحَمْدِهِ"
render() {
const entities = new Html5Entities();
return (
<SafeAreaView style={styles.container}>
<View>
<Text> {entities.decode(arabic)} </Text>
</View>
</SafeAreaView>
);
}
Output:
Working example:
https://snack.expo.io/Hk5b3IykS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With