lofaifa Posted March 19, 2012 Share Posted March 19, 2012 using javascript i wanna change the writing direction when the user change his language .. its about some problems with hebrew and arabic since they have rtl directions and my users want to switch bettween english and arabic .. soo default = rtl and when they change language = ltr <textarea class="question" name="question_text"> </textarea> any idea ? Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/ Share on other sites More sharing options...
nogray Posted March 19, 2012 Share Posted March 19, 2012 JavaScript can't tell when the user change the language, what you can do is check the last character the user type and compare it to the language letters and change the directions accordingly. There will be some characters that you should ignore (like ., and numbers) Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329227 Share on other sites More sharing options...
lofaifa Posted March 19, 2012 Author Share Posted March 19, 2012 and how can i detect the user language when hes typing something ? Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329232 Share on other sites More sharing options...
Adam Posted March 19, 2012 Share Posted March 19, 2012 You can detect the user's language using the "Accept-Language" HTTP request header. I don't believe you're able to reliably get this through JavaScript though, so you'd need to use PHP (through $_SERVER['HTTP_ACCEPT_LANGUAGE']). Using that though, as you generate the page you can set the HTML dir attribute or the CSS direction property appropriately. Edit Don't try to detect the user's language as they type. Just base it off the language their browser is configured with (using the header I mentioned before). Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329237 Share on other sites More sharing options...
lofaifa Posted March 20, 2012 Author Share Posted March 20, 2012 php isnt dynamic , i will get one value (the browser/user) language , but i need to make the "dir" attribute of the tag change with the user language. soo for example here we have by default 'ltr' soo it will stay ltr as long as i type latin letters , but when ill change the language and switch to arabic for example the arabic new text start to apear from right to left . this is my problem . Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329251 Share on other sites More sharing options...
Adam Posted March 20, 2012 Share Posted March 20, 2012 Then you need to define what you mean by "change language"? Is this a drop-down type menu that user selects their language from or something? Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329252 Share on other sites More sharing options...
lofaifa Posted March 20, 2012 Author Share Posted March 20, 2012 yes . or if we want to be more specific , the type of characters "Change" from latin to arabic .. is there a way that when the user change the characters type 'dir' changes also ? Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329256 Share on other sites More sharing options...
nogray Posted March 20, 2012 Share Posted March 20, 2012 you would actually check the character and compare it to a list of pre-determined characters for different languages. E.g. If I start typing "Something..." you would check the S and find it's not a RTL language If I start typing "هذا" you would check the characters to your list and determin it's a RTL. Since most languages are LTR, you only need to check if the character is a RTL language character or not. Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329259 Share on other sites More sharing options...
Adam Posted March 20, 2012 Share Posted March 20, 2012 I'd say it seems excessive to check a character on a key stroke event against (up-to) every character for every right-to-left language. (Admittedly though I have no clue just how many characters that is.) Having said that it's not impossible of course, but I would utilise the server to do the work (through an AJAX request) so that as entering text the textarea doesn't become slow or unresponsive, which may be more likely on mobile browsers. I would offer the option of disabling this too, and making it manually selectable. Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329390 Share on other sites More sharing options...
lofaifa Posted March 20, 2012 Author Share Posted March 20, 2012 ok . ill try to go with checking every character thank u Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329484 Share on other sites More sharing options...
requinix Posted March 20, 2012 Share Posted March 20, 2012 The Arabic Unicode block has 253 characters. The Hebrew block has 87. There are other RTL alphabets. Detecting those characters would be obscene. Enjoy trying to do that. Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329497 Share on other sites More sharing options...
lofaifa Posted March 20, 2012 Author Share Posted March 20, 2012 i tried regular expression (arabic range) $(document).ready(function(){ $('.question').keyup(function(){ var string=$(this).attr('value'); var last_character=string.substr(-1); var last_last_character=string.substr(-2,1); var arabic = /[\u0600-\u06FF]/; //var character=string.charAt(string.length-1); if(last_last_character=='\n' && arabic.test(last_character)==false && last_character!='\n'){ $('.question').attr('dir', 'ltr'); } else if(last_last_character=='\n' && arabic.test(last_character)==true) { $('.question').attr('dir', 'rtl'); } }); }); and it worked fine .. (i need to switch direction when the user start a new line and write something in latin . default=rtl) Quote Link to comment https://forums.phpfreaks.com/topic/259279-change-writing-direction-in-a-textarea-box-when-user-change-language/#findComment-1329533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.