12

Does anyone have any good "architecture" for the internationalization of dates? like in English its Monday, Chinese: 星期一, Dutch: maandag, Japanese: 月曜日

So my first idea is to create some sort of class that stores the strings of Monday to Sunday in 59 different languages. Apparently this isn't scalable at all, imagine now I need to display "12:34 A.M, Monday, 1st Jan 2000" I will then need another translation for A.M, P.M, the months (both long and short forms), the ordinals, etc, etc.

It's too much work, what's the solution?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • possible duplicate of [Internationalization in Javascript](http://stackoverflow.com/questions/3084675/internationalization-in-javascript) – Aron Rotteveel May 09 '11 at 10:47
  • 2
    There isn't any universal shortcut, if that's what you're asking. Yes, i18n and l10n is "much work", but that **is** the solution. Sorry. – Piskvor left the building May 09 '11 at 11:39
  • 2
    Believing that just translating all the parts of your date/time string is all you have to do is somehow cute :) – OregonGhost May 09 '11 at 11:47
  • What technology are you using server-side? It may already have anything you'll need. – OregonGhost May 09 '11 at 11:49
  • I think the best way for internationalization is to use a Framework will provide you an architecture and simplify your life. If you don't want, you can see this http://v-flauder.developpez.com/tutoriels/php/i18n/. It's a tutorial to simply implement a basic internationalization based on XML. – Elorfin May 09 '11 at 11:52
  • @OregonGhost i don't want to rely on the server-side for this. My server side is only responsible for passing data, everything is on the javascript (less work to do when swapping server side technologies) – Pacerier May 09 '11 at 11:56
  • 1
    when it comes to dates, localisation is just as important as internationalisation -- the date formats used in various countries differ wildly. – Spudley May 09 '11 at 12:01
  • @Pacerier Sorry, I don't take care of language used. I will try to find it in english – Elorfin May 09 '11 at 12:08
  • @Pacerier: I've merged your two accounts together. If you are having issues with your account, please ask a for help on [meta](http://meta.stackoverflow.com/) describing the issues you are having. –  May 09 '11 at 12:20
  • As Piskvor says, there is no universal shortcut. Programmers tend to approach these things with the eye to a solution but i18n & l10n are not finite states, there is always an exception so you deal with 99% of it automatically with rules but the remains 1% will take 99% of your time and still break. And of course you will never ever write a website that need 160 languages and date system. – PurplePilot May 09 '11 at 13:20

5 Answers5

7

Paul Irish said

date.js was abandoned and the version on the homepage is buggy.

and

moment.js is supergood and should be your first pick for date parsing, manip and formatting.

So I guess you people looking for a reliable date javascript library with i18n should use moment.js

Julien
  • 9,312
  • 10
  • 63
  • 86
4

The approach you suggest is not scalable. The Microsoft approach deals with 350 cultures; it has been suggested that there are 160 globally traded currencies etc. Maintaining a class that deals with this range of possibilities is a potential nightmare in the making.

@Aron suggests that your question is a duplicate of Internationalization in Javascript. One of the links in an answer to that question suggests a scalable and maintainable way forward. I would not normally advocate a Microsoft approach, but in this case they do seem to have the right idea for an architecture. Separate out the locale specific material into classes that can be incorporated into your application. Reference the locale specific material by a key based only on the culture (or locale). Don't attempt to maintain culture specific material yourself - unless you HAVE to, you really don't want to go there.

Community
  • 1
  • 1
Chris Walton
  • 2,513
  • 3
  • 25
  • 39
4

Datejs should be able to do what you are trying to do:

http://code.google.com/p/datejs/

Getting Started With Datejs

Its a javascript date library with about 157 different date-cultures/languages supported.
They took the approach of having a separate .js file for each culture. See: datejs source - trunk/src/globalization

codeulike
  • 22,514
  • 29
  • 120
  • 167
2

Much better handled on the server side. ASP.NET, for example, provides support for converting dates to strings in at least 30 or 40 different languages. I don't know about other server languages.

Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • i don't want to rely on the server-side for this. My server side is only responsible for passing data, everything is on the javascript (less work to do when swapping server side technologies) – Pacerier May 09 '11 at 11:56
  • Exactly. This problem is already solved and tested a million times for you on the server side. I18N is not a simple problem: if you reimplement it on the client side, you'll either waste a lot of time or will come up with something that is insufficient. – andref May 09 '11 at 11:59
  • 3
    @Pacerier Implement this as simple REST service: `http://example.com/de-DE/2011/05/09`. As long as you keep the contract, you can swap the technology anytime you want. – andref May 09 '11 at 12:01
1

I advise you to have a look at the jQuery framework which can solve your problem without having to re-inventing the wheel: http://jqueryui.com/demos/datepicker/#localization

Note: jquery-ui is a add-on framework which contains plenty of UI components.

Hope this helps

рüффп
  • 5,172
  • 34
  • 67
  • 113