0

A user on my website could possibly add a comment on an item that contains both Arabic and English characters. sometimes maybe just Arabic, others just English, and others French!

It's an international website you can't expect the characters being stored in my application. My website has nothing to do with the Facebook but I need my comments' TextBoxes to be able to accept and show any characters from any language!

...So how do you think I could achieve this ?

Mazen Elkashef
  • 3,430
  • 6
  • 44
  • 72
  • 3
    What do you mean? All .NET strings are unicode strings already so ASP.NET side should handle it just fine. If you're gonna write them to the database you'll need to store them in an `nvarchar` field. Is there something specific that's not working for you? – Roman Apr 11 '11 at 22:13
  • @R0MANARMY .. It's not that simple. Check this: http://stackoverflow.com/questions/3560173/store-arabic-in-sql-database – Mazen Elkashef Apr 11 '11 at 22:16
  • To quote the accepted answer to that question "You need to choose an Arabic collation for your varchar/char columns **or use Unicode (`nchar`/`nvarchar`)**" (which is basically what I referred to as well in regards to the database). That's why I asked if you were running into any specific issues. – Roman Apr 11 '11 at 22:23
  • @@R0MANARMY .. And is it "Normal" for a big application to have all of it's INSERT statements have this prefix N'...' !?? – Mazen Elkashef Apr 11 '11 at 22:28
  • 1
    If you use [parameterized queries](http://msdn.microsoft.com/en-us/magazine/cc163799.aspx#S5), you can specify that the field type is `NVarChar` and ADO.NET will take care of adding the prefix for you. So to answer your question, no, if you execute queries out of code you don't need to worry about it. If you are writing stored procedures and whatnot, you will probably need them, yes. – Roman Apr 11 '11 at 22:37
  • @R0MANARMY .. Great (Y) .. could summarize your comments because this is my answer =) +1 – Mazen Elkashef Apr 11 '11 at 22:39
  • Done, good luck with your site. Be sure to handle dates properly too. – Roman Apr 11 '11 at 22:58
  • @R0MANARMY .. Thanks I really need it :D .. Sorry, what about dates !? – Mazen Elkashef Apr 11 '11 at 23:31
  • That some places (US) use mm/dd/yyyy formatting and I think everyone else in the world uses dd/mm/yyyy. If you're going to be taking dates as a user input, it's less ambiguous as a date picker. Also, not everyone uses the Gregorian calendar (especially in the middle east), so don't assume everyone is in the year 2010 =). – Roman Apr 11 '11 at 23:34
  • Thanks for the tip =D .. but I think the international format is much clearer and about the Gregorian calendar .. I'm from Egypt and more than 99.99% in our daily life the Gregorian is used and even if any middle eastern country doesn't use the Gregorian calendar this often it will still be able to understand =) and I'm not intending to translate my website to Arabic .. Thanks for your help and tips .. My comment on the middle east thing was just to give you some knowledge about my countries, I hope I helped too or at least added something =) – Mazen Elkashef Apr 12 '11 at 12:59

4 Answers4

1

All strings in .NET are unicode strings (see documentation of the String class for more information). So taking input from the user in a mix of languages shouldn't be a problem.

If you plan to store this information in the database you will need to make sure the database columns are of type nchar or nvarchar. As others pointed out, when you run queries against these columns out of SSMS you will need to prefix Unicode strings with N to make sure they are handled properly. When executing queries from code you should use parameterized queries (or an ORM, which would probably be better) and ADO.NET will take care of properly constructing queries for you.

Community
  • 1
  • 1
Roman
  • 19,581
  • 6
  • 68
  • 84
0

There are two elements here:

  1. displaying the characters - this is handled on the user side, if something is missing there the outcome will be giberish, however you can't affect that
  2. what you can affect is the way you save characters in your database - convert everything to utf-8 regardless of the input. Any popular browser is able to render utf-8.
Femaref
  • 60,705
  • 7
  • 138
  • 176
0

If you use Unicode as a charset on the web pages and the database then you dont have to worry where are users from, since they will all type in unicode into your textboxes.

Marino Šimić
  • 7,318
  • 1
  • 31
  • 61
  • Well, I tested it my column datatype is NVARCHAR and it stores the arabic letters as "?????" .. but I just saw this question http://stackoverflow.com/questions/3560173/store-arabic-in-sql-database and it worked well to add an N before the input but .. will I always add this N to every INSERT statement in my whole application ! – Mazen Elkashef Apr 11 '11 at 22:19
  • 1
    @lKashef: That `N` is just to tell SQL Server that the input is unicode. See this question for more information [N prefix before string in Transact-SQL query](http://stackoverflow.com/questions/217535/n-prefix-before-string-in-transact-sql-query) – Roman Apr 11 '11 at 22:28
0

First, be sure for the fields of your database that will store data may be unicode charactters change these fields to Nvarchar not varchar

and you must know that NVarchar takes double value of row ex. the maximum row size in sqlserver is 8000 character that mean when you make a field nvarchar and make it 4000 that mean you have take the all 8000 character

Second, using according to language user select in browsing, set your charset in your code or page like

read from url like http://website.tld/ar/

<meta http-equiv="Content-Language" content="ar" >
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" >

so according to change the language i n url you change the meta tags of your page and that is it and change it according to your language

Regards

Mhmd
  • 4,989
  • 3
  • 22
  • 29
  • . I don't think that it's ever possible to predict the language or languages that the user might type .. so the solution is to make the application to be ready to deal with any language. – Mazen Elkashef Apr 11 '11 at 22:42
  • what if somebody from england wants to read both russian and arabic texts? – Femaref Apr 11 '11 at 22:43
  • i want application to be for any language, i have edited the solution to avoid misunderstanding – Mhmd Apr 11 '11 at 23:03