Auto detect language and display the correct one with javascript - ruby-on-rails

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?

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.

Related

Validation Check for non-english strings

I am trying to build an iOS app and I wish to validate whether a particular string that I have, is a valid email or not. The catch is, the string can be a non-english lanuage as well. Also, lets take Arabic for example, a language that is written Right to Left. So I wish to know how could I validate a non english string. (for the sake of simplicity, lets just assume that a valid email has the format "string+#+.+string").
Programming Language - Swift
I know that we can validate this using regEx
validEmail = "[A-Z0-9a-z\\._%+-]+#([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}"
.
.
Now I am not sure how to do this if the language is not indian, and it is written Right to Left(like in Arabic, urdu, etc.)
Here's the validation to check if the string is in English language or not.
validEnglishAlphabets = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")

Smalltalk: How to make a hyperlink

In smalltalk, How can I add to string a link
example :
I have a string str = "trial string"
I want to add another string to it but when I click on it I go to some destination
and str will appear like
trial string and SomeLocation
If you are using Seaside you can use the following piece of code when your component will be rendered.
renderContentOn: html
html anchor
url: 'http://www.seaside.st';
with: 'Visit the Seaside'.
In any programming language, strings are just sequences of characters. Wether or not a hyperlink (in html markup language) is shown as a clickable link, as it is shown in a web browser, depends on how the string is interpreted by the editor/viewer that shows it.
If you want to show hyperlinks in Smalltalk code, I don't know of any Smalltalk IDE that has support for that. But I would not be surprised if there is some project out there that supports doing that.

Formatting NSString for superscript and subscript

I am writing a utility app for some coworkers. The app is essentially a custom notepad, with buttons that represent the shorthand they use to transcribe a task. All of the buttons add a string to arrays that I have set up to hold the transcript, and I add the strings to the row arrays like this.
[currentRow addObject:#"("];
Some of the shorthand needs to be written in subscript, and some in superscript. There are not Unicode characters for all of the characters that I need, so I have been trying to sort through the code around Attributed Strings,but I'm not quite getting it. Does anyone have advice on this or some sample code?
Also, after this transcript is printed to the screen during transcription, I send it to an email message body.. so I assume I'll need to worry about formatting there as well. I am currently using plain text, but the email could be HTML. Thanks!
If you display the text in a WebView you can use html tags to set superscript. It also has the advantage to run on older iOS versions and you can reuse the text in your mail.
NSString *myText=#"This text contains <sub>subscript</sub> and <sup>superscript</sup> text.";
[self.myWebView loadHTMLString:myText baseURL:nil];

Use non-lating characters for product and category URL key in magento

Magento converts non-Latin characters in the URL key of products and categories to Latin characters. How can I use non-Latin characters?
formatUrlKey in Mage/Catalog/Model/Product/Url.php uses $_convertTable in Mage/Catalog/Helper/Product/Url.php. I've tried to change the code but I can't make Magento save non-Latin URLs and show them correctly in the admin.
I've removed hebrew letters from the $_convertTable as you suggsted.
The problem is that the formatUrlKey replaces characters which are not 0-9 or a-z with '-':
public function formatUrlKey($str)
{
$urlKey = preg_replace('#[^0-9a-z]+#i', '-', Mage::helper('catalog/product_url')->format($str));
$urlKey = strtolower($urlKey);
$urlKey = trim($urlKey, '-');
return $urlKey;
}
So I'm overriding this method and changing it to:
$urlKey = preg_replace('#[^0-9a-zא-ת]+#i', '-', Mage::helper('url')->format($str));
Now magento correctly saves and display the url string but it doesn't work in the browser.
When trying to access the product url I'm getting 404.
If instead of preg_replace, strtolower and trim I'm using only:
$urlKey = urlencode($str);
It also doesn't work because magento calls formatUrlKey several times.
I don't understand why.
Thanks
Hi
This extension will help for you.
http://www.magentocommerce.com/magento-connect/alexhost/extension/6587/magefast_seflinkmultilanguage
Since Magento is just blinding converting from the table, deleting entries from the table will prevent Magento from trying to convert them. Override the helper class and delete the entries you don't want to see and you should be pretty well on your way.
As far as displaying them correctly in the admin panel, is this a separate problem you have if you save those non-Latin characters? More specific information would be helpful.

Convert user title (text) to URL, what instead spaces, #, & and other characters?

I have some form on the website where users can add new pages. I must generate SEO friendly URLs and make this URLs unique.
What characters can I display in URL, I know that spaces I should convert to underscore:
" "->"_" and before it - underscores to something else, for example:
"_"->/underscore
It is easy make title from URL back.
But in my specific title can be all characters from keyboard, even : ##%:"{/\';.>
Are some contraindications to don't use this characters in URL?
Important is:
-easy generating URL and title from URL back (without queries to database)
-each title are unique, so URL must be too
-SEO friendly URLs
Aren't you querying the database to get the content anyway? In which case just grab the title field in the same query.
The only way to reliably get the title back from the URL is to 'URL encode' it (in PHP you use the urlencode() function). However, you will end up with URLs like this:
My%20page%20title
You can't replace any characters because you will then not have unique URLs. If you are replacing spaces with underscores, for example, the following titles will all produce the same URL:
My page title
My_page title
My_page_title
In short: don't worry about one extra database hit and just use SEO-friendly URLs by limiting to lowercase a-z, 0-9 and dashes, like my-page-title. Like I said, you can just grab everything in one query anyway.

Resources