Jump to content

Get Localized Month/Date from three different Languages on same template by looping through languages


Orkun
 Share

Recommended Posts

Hi Guys

I'am trying to output the localized month names in french, german and italian for my newsletter. At the moment my default language is french.

This code outputs everytime time the french month name:

foreach ($languages->find("name!=en") as $language) {

	$user->language = $languages->get($language->name);

        switch ($language->name) {
	     case 'default':
		   $date = strftime("%d. %B %G", $page->date). "-FR/DEFAULT";
	     break;
					 
	     case 'de':
                   $date = strftime("%d. %B %G", $page->date). "-DE";
             break;
					 
	     case 'it':
		   $date = strftime("%d. %B %G", $page->date). "-IT";
	     break;
        }

        echo $date."<br />";
}

output looks like this:

31. mars 2016-FR/DEFAULT

31. mars 2016-DE

31. mars 2016-IT

I also have set the right locale inside every language translation file. How can I fix this, that it looks like this:

31. mars 2016-FR/DEFAULT

31. März 2016-DE

31. marzo 2016-IT

Link to comment
Share on other sites

Could you add this and see if the locales are actually all correctly set:

echo setlocale(LC_TIME, 0);

Also are you sure these locales are actually available on your system?

@Klenkes

strtotime does not necessarily work correctly with all possible date/time formats, which processwire does provide therefore it's better to go this way:

$timestamp = $page->getUnformatted('date'); // instead of strtotime($page->date)
  • Like 1
Link to comment
Share on other sites

How about that:

$date = strftime("%d. %B %G", strtotime($page->date)). "-FR/DEFAULT";

Solved the month problem for me.

Now i get this :

01. avril 1600-FR/DEFAULT

01. avril 1600-DE

01. avril 1600-IT

My date field configuration looks like this: 

post-3125-0-33209200-1459501405_thumb.pn

post-3125-0-14281900-1459501406_thumb.pn

Link to comment
Share on other sites

Could you add this and see if the locales are actually all correctly set:

echo setlocale(LC_TIME, 0);

Also are you sure these locales are actually available on your system?

Somehow i get every time:

fr_FR.utf8

fr_FR.utf8

fr_FR.utf8

does 

$user->language = $languages->get($language->name);

dont have any effect or what?

Link to comment
Share on other sites

Now i get this :

01. avril 1600-FR/DEFAULT

01. avril 1600-DE

01. avril 1600-IT

That's the strtotime issues I was talking about above :D You should probably also use the getUnformatted() method, this way you can be sure the timestamp is what is saved in the backend and nothing is creating conversion issues because you're supplying a string instead of a timestamp.

Link to comment
Share on other sites

I've solve the problem by setting the setlocale(LC_ALL, 'xx_XX.utf8') in every case with the right locale code. 

SOLVED: 

The endsolution looks like this:

foreach ($languages->find("name!=en") as $language) {

	$user->language = $languages->get($language->name);

        switch ($language->name) {
	     case 'default':
                   setlocale(LC_ALL, 'fr_FR.utf8');
		   $date = strftime("%d. %B %G", $page->getUnformatted('date'));
	     break;
					 
	     case 'de':
                   setlocale(LC_ALL, 'de_DE.utf8');
                   $date = strftime("%d. %B %G", $page->getUnformatted('date'));
             break;
					 
	     case 'it':
                   setlocale(LC_ALL, 'it_IT.utf8');
		   $date = strftime("%d. %B %G", $page->getUnformatted('date'));
	     break;
        }

        echo $date."<br />";
}

I thought, that when I would change the user language on every loop, it would also change the LC_ALL but somehow it doesn't do that. So I set the setlocale manuallay on every loop.

When you need other locale-codes here is a list of all languages(or probably the most of it):

http://www.red-route.org/code/php-international-language-and-locale-codes-demonstration

And also thank you to @LostKobrakai, he pointed me to the right direction!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...