Reputation: 238
I am making a website for my friend https://photos4humanity.herokuapp.com/ I'm thinking to pull the post from its facebook page and display it on the website so he doesnt have to duplicate content for both.
Each facebook post has both english and chinese in it. like here : https://www.facebook.com/photosforhumanity/
I would like to auto detect the language from the json file I get from facebook. Then detect which is in English and which is in Chinese then only display the right language according to internatioanlize from rails.
Is there a smart way to do this?
Upvotes: 0
Views: 193
Reputation: 4053
You could use Regex to detect if the string has any English characters or not:
isEnglish = myString.match(/[a-zA-Z]/)
or
isEnglish = myString =~ /[a-zA-Z]/
I haven't tested either of these and I don't know how your json file is organized, but this should work for a singular string.
Edit:
To pull the English characters out of the string, you can use the slice!
method:
englishString = myString.slice!(/[a-zA-Z]/)
After doing that, myString
should only contain non-English characters and englishString
should contain only English characters.
Upvotes: 1