Skip to content Skip to sidebar Skip to footer

How To Convert A Foreign Month In A Date String Into English

The below answer to another thread seems to make a start on what I think I need, but I am having difficulties in implementing it. The best library for that purpose would probably

Solution 1:

I think u could use datejs for your purpose. It is capable of parsing your date data in any language available (150+ as the state) via a culturefile and then save it in english or any other language you desire.

Have a look : http://code.google.com/p/datejs/

Solution 2:

The way to do such things in Globalize.js is to read the string using one locale, write it using another locale. You need to check the success of reading by some test that checks against null value (since this value signals an error). Like this:

<scriptsrc="globalize.js"></script><scriptsrc="globalize.cultures.js"></script><script>var monthString = 'sep.'; // replace by code that extracts the stringvar lang = 'ro';          // replace by code that picks up the right languagevar month = Globalize.parseDate(monthString, 'MMM', lang);
if(month) {
  document.write(Globalize.format(month, 'MMM', 'en'));
} else {
  alert('Unrecognized month: ' + monthString);
}

This parses just the month abbreviation, or “short name” of month.

The problem is with the variation inside cultures. For example, for Romanian language, the short name of September is “sep.” in Globalize.js data (mainly based on .NET data; Datejs is probably based on the same data here) but “sept.” in CLDR data, and your example mentions “Set”!

Globalize.js reads names case-insensitively, but otherwise it uses the exact string in its locale definitions (e.g., “sep” won’t do if the definition says “sep.”). There are various ways around this. At the simplest, edit the locale definitions in Globalize.js to match those used in your data. Alternatively, you can create an alternate locale (a variant) with different month names and parse the name using it if it does not parse according to the basic locale. So this is a way to allow, on input, different sets of month names for a language.

Of course, if it’s only a matter of mapping short month names from different languages into English, you could simply write the code for it directly. But you would still need to get the names into your objects from somewhere – from .NET, from CLDR, or from your actual data.

Solution 3:

I've stumble upon this difficulty back in 2013, and did work on a small utility to address the problem.

I published my v3 yesterday : https://framagit.org/Siltaar/month_nb

It supports 69 languages, and you don't have to know the language of your month name to get it's number. It's based on a re-usable standalone JSON tree structure for the data and a small tree-walking loop to find the results.

To use it, simply include both the data and the tree-walker files in a web page :

<scriptsrc="month_nb_json.js"type="text/javascript"></script><scriptsrc="month_nb.js"type="text/javascript"></script>

Then you'll be able to use it :

>> month_nb('août');
8

It's part of my Meta-Press.es project, which got some funding and that I'll continue to work on for at least the next 6 years (started in 2013…).

Else, it exists https://github.com/datejs/Datejs which is a human contributed translation approach.

Post a Comment for "How To Convert A Foreign Month In A Date String Into English"