Jump to content

change writing direction in a textarea box when user change language


Recommended Posts

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 ?

 

 

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)

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).

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 .

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.

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.

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)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.