5

Any ideas for translating PHP weekdays and months to local language? I have script which prints weekdays of next 5 weeks and I want that they are in other language.

$timestamp = strtotime('next Monday');
for ($i = 0; $i < 35; $i++) {
    echo strftime('%A', $timestamp)." ";
    $timestamp = strtotime('+1 day', $timestamp);
}

Is there any good integrations e.g. with PHP and moment.js? I have looked at Use PHP's date() formats in moment.js and GitHub fightbulc/moment.php , but I don't understand how to use those..

Thank you in advance.

lingo
  • 1,848
  • 6
  • 28
  • 56
  • Try google translate api. https://cloud.google.com/translate/ – Manikiran Dec 25 '15 at 18:39
  • PHP already provides support for formatting dates in different languages. I see you already found out [`strftime()`](http://php.net/manual/en/function.strftime.php). It works together with [`setlocale()`](http://php.net/manual/en/function.setlocale.php). The PHP documentation of both functions provides usage examples. – axiac Dec 25 '15 at 18:46
  • @axiac, I added `setlocale(LC_TIME, "fi_FI");` but it doesn't have any effect. Weekdays keep showing in english. – lingo Dec 25 '15 at 18:56
  • 1
    The value to pass as the second argument to `setlocale()` depends on the OS where the PHP interpreter runs. Windows uses different naming rules. Also, sometimes the locale information is not installed for all languages. On Unix-like systems it usually can be installed. – axiac Dec 25 '15 at 19:45
  • Check [this question](http://stackoverflow.com/q/8521388/4265352). Its [accepted answer](http://stackoverflow.com/a/8521517/4265352) provides information about how to find out what value to use as the second argument of [`setlocale()`] – axiac Dec 26 '15 at 13:17

3 Answers3

10

You are probably looking for the following:

http://php.net/manual/en/function.setlocale.php

http://php.net/manual/en/function.strftime.php

UPDATE

setlocale(LC_TIME, "fi");
echo utf8_encode(strftime('%A'));

RESULT

perjantaina

And this as a gift for you guys - the ISO language codes

Blackbam
  • 17,496
  • 26
  • 97
  • 150
Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30
9

You coud use IntlDateFormatter, as follows:

$fmt = new IntlDateFormatter('fin', IntlDateFormatter::FULL,
              IntlDateFormatter::NONE, null, null, "cccc"); 
$timestamp = strtotime('next Monday');
echo $fmt->format($timestamp);

Output:

maanantai

Depending on the grammatical context you may need "eeee" as last argument instead of "cccc":

maanantaina

trincot
  • 317,000
  • 35
  • 244
  • 286
3

I made the following for really translating months and weekdays from and to any language (that is installed on the server).

Even thought this topic has long be answered, I will post it since it might really help some people.

setlocale() is used, but also reset; so it can't mess up the rest of your code.

These are the functions you can use:

weekdayToEnglish($locale,$weekday,$short)
weekdayFromEnglish($locale,$weekday,$short)
monthToEnglish( $locale,$month,$short)
monthFromEnglish($locale,$month,$short)

$locale is the language as you would use in setlocale(), depends on your server, mostly something like 'fr_FR';

$weekday or $month the name of the weekday of month to be translated

(optional) $short if true gives (localized) short notation (like Thu in stead of Thursday or Oct instead of Octobre)

The code:

function weekdayToEnglish($locale,$weekday,$short=false) {
    return dateElementToEnglish($locale, $weekday, $short ? "%a" : "%A",
                                $short? "D" : "l", 'tm_wday', "Sunday +" ," days") ;
}

function weekdayFromEnglish($locale,$weekday,$short=false) {
    return dateElementFromEnglish($locale, $weekday, $short?"%a":"%A");
}

function monthToEnglish( $locale,$month,$short=false) {
    return dateElementToEnglish($locale, $month, $short ? "%b" : "%B",
                                $short ? "M" : "F", 'tm_mon', "January+" ," months") ;
}

function monthFromEnglish($locale,$month,$short=false) {
    return dateElementFromEnglish($locale, $month, $short ? "%b" : "%B");
}

function dateElementToEnglish($locale, $text, $strfTimeformat, $dateFormat,
                              $dateArrayIndex, $strToTimePrefix, $strToTimeSuffix) {
    $saveLocale = setlocale(LC_TIME,0);setlocale(LC_TIME,$locale);
    $translateToNr = strptime($text, $strfTimeformat)[$dateArrayIndex] ;
    $readDate = strtotime($strToTimePrefix . $translateToNr . $strToTimeSuffix);
    $translation = date($dateFormat, $readDate);
    setlocale(LC_TIME,$saveLocale);
    return $translation;
}

function dateElementFromEnglish($locale,$text,$strfTimeformat) {
    $saveLocale = setlocale(LC_TIME,0);setlocale(LC_TIME,$locale);
    $translation = strftime($strfTimeformat,strtotime($text));
    setlocale(LC_TIME,$saveLocale);
    return $translation;
}

For instance:

echo weekdayToEnglish("nl_NL","vrijdag")."<br>";
echo weekdayFromEnglish("fi_FI","Monday")."<br>";
echo weekdayToEnglish("nl_NL","wo", true)."<br>";
echo weekdayFromEnglish("de_DE","Thu", true)."<br>";
echo weekdayFromEnglish("fr_FR",weekdayToEnglish("nl_NL","zaterdag"))."<br>";
echo monthFromEnglish("de_DE", "March")."<br>";
echo monthFromEnglish("fr_FR", "February", true)."<br>";
echo monthToEnglish("fr_FR", "avril")."<br>";
echo monthToEnglish("fi_FI", "joulukuu", true)."<br>";

Will yield:

Friday
maanantai
Wed
Do
samedi
März
févr.
April
Dec
Roemer
  • 1,124
  • 8
  • 23
  • Always returns Thursday (i checked it on a saturday) when use `echo weekdayToEnglish("nl_NL","vrijdag")."
    ";`
    – HenryW Aug 25 '18 at 10:18
  • That will be the case on some servers, I highly suspect because the particular language(s) is/are not installed on that server. If they are, it works fine, just tested it again. – Roemer Aug 28 '18 at 12:35
  • See also https://www.binarytides.com/php-get-list-of-locales-installed-on-system/ and https://stackoverflow.com/questions/10909911/php-setlocale-has-no-effect – Roemer Aug 28 '18 at 12:37