Jump to content

Can't get PW to output German month names


totoff

Recommended Posts

I was also struggling to display the correct language dates (German). My date field is called news_date - the input and output format of the field set using the drop down is "18 October 2012". I am outputting the field in my template like this

$page->news_date;

I first tried setting locale in the site config.php and then in the page template, but it had no affect.

setlocale(LC_ALL, 'de_DE');

I followed Somas advice in this post, installed the language support and added the German translations. I installed them as the default language.

I changed the value found here "/wire/modules/LanguageSupport/LanguageSupport.module" - "Value to pass to PHP's setlocale(LC_ALL, 'value') function when initializing this language" is set to "de_DE"

However, this also had no affect

What I ended up doing was :

<?php echo strftime("%d %B %Y", strtotime($page->news_date)); ?>

And now it correctly outputs the German date.

Not sure what I did wrong or if this is a bug, but I think the simplest solution would be to add the setlocale option in the site config (instead of installing the extra language support which might not be required).

Link to comment
Share on other sites

Hi Michael. I don't think it's the right way to install language support only for the sake of displaying localized dates. The problem with setlocale can be that the list of installed locales can change from server to server, also in relation to the OS the server runs.

So there is no guarantee that

setlocale(LC_ALL, 'de_DE');

works on all systems.

If you're on a *nix system you can probably look up the available locales by doing,

<?php system('locale -a') ?>

or run this the command, locale -a, from the terminal.

If that doesn't work you can try an array in config.php in which you just put the most common variations, then it will try them all. Something like,

<?php
setlocale(LC_ALL, array('de_DE.UTF-8','de_DE@euro','de_DE','german'));
?>
  • Like 1
Link to comment
Share on other sites

Hi SiNNuT, Thanks for the suggestion.

After looking at the output using system('locale -a) I tried

setlocale(LC_ALL, array('de_DE','de_DE.iso88591','de_DE.iso885915@euro','de_DE.utf8','de_DE@euro'));

but it still outputs English dates. This might be because I have installed the language support, so should probably try this on a fresh installation

Link to comment
Share on other sites

Haven't used language support myself but this is what Soma said:

If not defined it will take the default. Default is english in PW. If you set something in config.php doesn't make it the default as it doesn't have language context there, anyway it will get overwritten by the PW language functions.

So indeed try setting setlocale in config.php without languagesupport installed. Or ofcourse, when you know the working locale value you should also able to set this in this in set in the translation of the language for the LanguageSupport.module. Let me know if it works.

Link to comment
Share on other sites

Just to update on this issue,

I tried on same host with setlocale (no language support installed) = same problem

I tried it on another host (using setlocale and no language support installed) and it also outputs the dates in English.

It’s definitely setting the correct locale, but the standard $page->date; field is always outputting in English and seems to ignore it. The only solution I could work out was to convert the date using strtotime and then format it like this

<?php echo strftime("%d %B %Y", strtotime($page->news_date)); ?>
  • Like 1
Link to comment
Share on other sites

Datefield seems to use date function to format dates: https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Inputfield/InputfieldDatetime/InputfieldDatetime.module#L109

Date doesn't support setlocale: http://php.net/manual/en/function.date.php

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().
  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Having another German date issue. Using the latest development branch of PW.

The problem :

Dates with umlauts or special characters are not outputting correctly - so März looks like M?rz

Here is my test case - I have added this in a separate php file outside of PW on the same server and it works correctly

setlocale(LC_ALL, array('de_DE','de_DE.iso88591','de_DE.iso885915@euro','de_DE.utf8','de_DE@euro'));
$date[0] = mktime( 1, 0, 0, 3, 2, 2012 );

$date_day_month = strftime('%d. %B', $date[0]);
$date_year = strftime('%Y', $date[0]);

echo $date_day_month . $date_year;

Result on test php page = 02. März2012

Result on PW page = 02. M?rz2012

What did I try

I first tried the simplest way by setting locale in the site/config.php - this does work on my local MAMP server, but i have now tried the same config setting on three different shared web hosts (all based in Switzerland incidentally) but it has no affect. Tried the following combinations but it has no affect on the date output format (always in English):

setlocale(LC_ALL, 'de_DE');

setlocale(LC_ALL, 'de_DE.utf8');

setlocale(LC_ALL, array('de_DE','de_DE.iso88591','de_DE.iso885915@euro','de_DE.utf8','de_DE@euro'));

I then tried installing the language support and adding the correct language pack - this changes the language output, but still an issues with the umlaut. In the wire--modules--languagesupport--languagesupport-module -  “Value to pass to PHP's setlocale(LC_ALL, 'value') function when initializing this language” I have tried all the following with no success 

array('de_DE','de_DE.iso88591','de_DE.iso885915@euro','de_DE.utf8','de_DE@euro')

de_DE.utf8

de_DE

Any ideas?

Link to comment
Share on other sites

thanks for the suggestion - writing März works fine, and PW handles umlauts fine in the same template and all other areas of the site, so I don't think it's a UTF-8 encoding issue. This is only happening when using PHP dates and strftime. It does not happen locally on MAMP, so it's probably more of an obscure issue with the cheap and poorly configured shared hosting that all my clients seem to love.

My fall back solution is to create an array with the month names and output them.  

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

Hi,

I also had the Problem with umlauts and strftime. The solution that worked for me was this line of code:

setlocale(LC_ALL, 'de_DE.UTF-8');

@Wanze: when I set this then the umlaut in "Body" of news are not correct! I have the Blog-Template.

My solution:

Processwire is: 2.3.0

date-field is: %A, %d. %B %Y %H:%M

in /wire/config.php: $config->dbCharset = 'utf8'; and $config->dateFormat = 'd.m.Y H:i:s';

in /site/config.php: $config->timezone = 'Europe/Vienna'; - but when default it doesn´t matter

Database should be utf8_general_ci

If you set this you get in Blog-News:

Posted by admin, Dienstag, 26. M�rz 2013 10:32
The only solution I found is to go in source: /site/templates/markup/post.php and include after first line:
setlocale (LC_ALL, 'de_DE.UTF-8');

Now you get:

Posted by admin, Dienstag, 26. März 2013 10:32

Now take a look at the archive: the month-name in the middle content and in sidebar is not correct.

You must include the same code above in two files:

/site/templates/markup/archives.php and /site/templates/archives.php

This works for me.

  • Like 1
Link to comment
Share on other sites

hmm, the same problem was also in MODX Revolution in older versions than 2.2. They fixed it since 2.2 with a system setting.

Wouldn't it be useful to also have a system setting in PW admin for this? Or maybe could be set in the installation process?

So the Admin only has to set the locale 'de_DE.UTF-8' in a field.

Link to comment
Share on other sites

hmm, the same problem was also in MODX Revolution in older versions than 2.2. They fixed it since 2.2 with a system setting.

Wouldn't it be useful to also have a system setting in PW admin for this? Or maybe could be set in the installation process?

So the Admin only has to set the locale 'de_DE.UTF-8' in a field.

I know what you mean, there is a setting in modX, but for correct output you must set the same in the snippet archivist.

When you want to be sure about your output it is best you fix this yourself in the templates with

setlocale(LC_TIME, "de_DE.UTF8");

where your umlaute are not correct.

It also may be a problem when some sites you edit are ansi and not utf8 without BOM. I use notepad++, there I can change this under "Kodierung/UTF8 ohne BOM". When loading from ftp you will see this in the statusbar at the bottom. First thing I do, when I see ansi, I cut the hole sitecode change to utf8 without BOM and paste again the code and save to ftp. With only change and not cut & past your written umlaute i.e. in descriptions will be in cryptic signs.

Make sure that there is not anywhere an include that is in ansi!

Link to comment
Share on other sites

When you want to be sure about your output it is best you fix this yourself in the templates with...

my intention was to ask, if there's a global possibility in PW to set this (e.g. in the config.php)? This would be easier than placing the setlocale in every place you need it.

Maybe like the setting for the date/timezone which Ryan will include in the install routine of version 2.4

(see here: http://processwire.com/talk/topic/2807-maybe-useful-to-check-in-installroutine-of-pw-the-php-default-charset/)

Link to comment
Share on other sites

  • 1 year later...
  • 2 weeks later...

Hi Forum,

I'm working on a site which implements Ryan's Blog Profil (I did import it to the site). Unfortunately nothing of the above helps me to output month names in German - no matter what I do they remain English.

This is what I have in my config.php (the timezone was set by the installer:

$config->timezone = 'Europe/Berlin';
setlocale(LC_TIME, "de_DE.UTF8");

I've tried Ralf's code above in my templates and the other codes in this thread - to no avail.

I'm stuck. Any help is greatly appreciated.

Thanks.

Link to comment
Share on other sites

  • 1 month later...

Hi Guys

I also had problems with the German Output of month names. So I test it on my server with a demo script. What ever I tried the output of date() was english. The solution was handover the timestamp and format it in the template with the strftime() function and it worked. The PHP-Manual for date() say this:

To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

So for me that solves the problem.

Greetings

Jens Weigel

  • 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
×
×
  • Create New...