0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Is there anyway to let say strip out only the english and store it somewhere, then strip out the chinese character and store that else where too? A string could have both chinese any english. "ABC 一二三"
Thanks. This works for my case. Just a curious question, if I have spanish and english this will not work?
For the most part, it wouldn't. It would certainly weed out the Spanish only characters, but that wouldn't be much use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.