问题
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 be Globalize. It allows you to specify the locale (it is called culture but it is actually the same), the format (built-in or your own) and actually parse string to given date:
var dateString = "lunes, 29 de agosto de 2011"; // seems like long date format in Spanish var date = Globalize.parseDate( dateString, "D", "es" );
You would need to attach appropriate culture file as well as reference to Globalize to make it work. Please mind that cultures are already defined in the library, so don't be afraid of the message on the web page, you actually don't need .Net.
If someone could turn this into a working example I could then take this and adjust to what I need.
However ... does anyone know of an alternative method?
My string for example is:
"Set 24, 2012 20:40:20" which has the month in Romanian.
I need it to be
"Sep 24, 2012 20:40:20" which has the month in English.
I need a solution that can take the language code (ie en, es, pl, ru, ro etc) and convert the month in the string (needs to be able to handle all 12 months, not just the one in the above string) from the foreign language to English.
Any speedy help would be most appreciated so that I can get my site back up and running at full speed again.
If you require any further information from me please ask :)
Regards Ross
@Jukka K. Korpela
I have this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing Globalize</title>
<meta name="keywords" content="Globalize, Testing">
<meta name="description" content="Trying to get globalize to work.">
<script src="https://github.com/jquery/globalize/blob/master/lib/globalize.js"></script>
<script src="https://github.com/jquery/globalize/blob/master/lib/cultures/globalize.cultures.js"></script>
<script>
var monthString = 'sep'; // replace by code that extracts the string
var lang = 'ro'; // replace by code that picks up the right language
var month = Globalize.parseDate(monthString, 'MMM', lang);
if(month) {
document.write(Globalize.format(month, 'MMM', 'en'));
} else {
alert('Unrecognized month: ' + monthString);
}
</script>
</head>
But it doesnt work when I test in "http://htmledit.squarefree.com/" please help :)
回答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/
回答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:
<script src="globalize.js"></script>
<script src="globalize.cultures.js"></script>
<script>
var monthString = 'sep.'; // replace by code that extracts the string
var lang = 'ro'; // replace by code that picks up the right language
var 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.
回答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 :
<script src="month_nb_json.js" type="text/javascript"></script>
<script src="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.
来源:https://stackoverflow.com/questions/12591985/how-to-convert-a-foreign-month-in-a-date-string-into-english