views:

186

answers:

3

We are developing a site that has a Japanese localization. In that site, we display several dynamic fields using asp:label and such controls.

Recently, we were required to display numbers using "double-byte characters".

While it first I thought that was a confusion from the client, he sent me this in an email:

1234567890
1234567890

and this shows I was wrong, and that indeed there are "wider" number characters available.

Any hint on how to make ASP.NET switch to those dynamically?

Update: The second sample seems be called "FULLWIDTH" characters. The question could be rephrased as:

How do I make ASP.NET automatically display FULLWIDTH numbers when the current culture is JP?

A: 

You're confusing font width with DBCS. If you just want a wider font, use the appropriate CSS. If you actually want to support unicode and use wide characters, you need to implement your backend using the appropriate data type.

By default all strings in C# are Unicode. You can control encoding following techniques outlined here.

jeffamaphone
I confused them at first. But please see the example I posted and Jonathan answer's below.
pgb
+2  A: 

Apparently, there are some other characters in unicode that look just like the 'normal' numbers, but display wider by default.

Here's the text from your question pasted into notepad, saved as unicode, and run through XXD:

0000000: fffe 3100 3200 3300 3400 3500 3600 3700  ..1.2.3.4.5.6.7.
0000010: 3800 3900 3000 0d00 0a00 11ff 12ff 13ff  8.9.0...........
0000020: 14ff 15ff 16ff 17ff 18ff 19ff 10ff 0d00  ................
0000030: 0a00 0a                                  ...

It looks like where '1' is 0x0031, 'special 1' is 0xff11. If using CSS to display the 'normal' numbers, but with a wider font isn't an option, I'd say you'll have to take the number, convert it to a string, add 0xff10 to every character between 0x0030 and 0x0039 to get what you're looking for.

Jonathan
Yes, this is correct. The characters are referred as FULLWIDTH in the Unicode documentation (at least that's what I could find). Any idea how to automatically convert asp:labels to that?
pgb
A ControlAdapter might be the cleanest way to make this work, since I'm guessing that subclassing label to handle this is a no-go (judging by your comment on another answer).
Jonathan
A ControlAdapter looks like the way to go, but I'm on ASP.NET 1.1 (and can't migrate at this time)
pgb
In that case, maybe an IHttpModule that buffers the output and does a search/replace on it? It's quite a hack to do it that way, and you'd have to make sure to use smart enough regular expressions so you don't widen any HTML attributes (just content), but in theory you should be able to do it. Not clean, but well, you asked.
Jonathan
@Jonathan: I think IHttpModule is the answer but I would inject a stream into the output chain of streams. Use a state engine to detect which numeric characters should be replaced. As long as the output encoding is unicode and there shouldn't be any danger content size being modified (although I'm not sure that is a big problem).
AnthonyWJones
@AnthonyWJones: Good point -- you could go with that route, then you don't need the memory to buffer everything up before you send it. It's more complicated to write, but code that saves memory usually is.
Jonathan
A: 

Here is a function to do it:-

 static string IntToWideNumbers(int n)
 {
  char[] chars = n.ToString().ToCharArray();
  for (int i = 0; i < chars.Length; i++)
  {
   chars[i] = (char)((uint)chars[i] ^ 0xFF20);
  }
  return new String(chars);
 }
AnthonyWJones
I'm looking into an automatic way of doing this. My site is full of different asp:label's that display different dynamic content. Some contain text and some numbers (mostly pricing information and dates). I need only to translate the labels where in the Japanese version, without affecting the other version.I understand I can run this code and check for the current culture, but I'm actually looking into a more automated solution.
pgb
Its a requirement too far off the norm to be something you can do "automatically".
AnthonyWJones