Jump to content
totoff

Can't get PW to output German month names

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).

Share this post


Link to post
Share on other sites

I think some parts of config.php should be "translatable" in a language file. So if you're using the german pack the default timezone, etc. should change automatically :)

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

thanks apesia,

and as I have just learnt in another post, instead of using strtotime( ) you can use the PW function getUnformatted( ) so the example would be

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

Just a thought:

If you write "März" in the template, does it output the Umlaut correctly? If not, then maybe the template file itself is not UTF-8 encoded.

Share this post


Link to post
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.  

Share this post


Link to post
Share on other sites

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');

Hmm PHP why isn't everything utf-8 by default... :rolleyes:

  • Like 3

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

guenter55, couldn't you just include the setlocale directive in config.php? Seems easier to maintain in the long run.

Share this post


Link to post
Share on other sites

guenter55, couldn't you just include the setlocale directive in config.php? Seems easier to maintain in the long run.

I tested it, this has no effect, SiNNuT.

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
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/)

Share this post


Link to post
Share on other sites

Hi,

if it still does not work for someone after all these solutions would be another solution - which worked for me -

utf8_encode(strftime('%d. %B %Y', $page->getUnformatted('date')));

(for the archiv :) and THANKS ryan for the hint with strftime reference at php.net.)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Good morning,

got it working. I had a typo in Ralph's code.  :-X

Sorry for posting.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By kreativmonkey
      Hi,
      i want to set a date field value by api but i have problems to do so. I think this should be work:
      $page->of(false); $page->expire_date = time(); $page->save(); $page->of(true); but when i look into the field i get not the saved time. The output is always: 30 Nov, -0001 00:00  (timestamp: -62169987208).
      when i echo the time directly after the $page->of(true); than i get the current time but avter reload and output the time bevor $page->of(false); i get the time above!
      do you have any idea whats the problem?
    • By abdus
      I'm trying to build a multilanguage website with Turkish as main and English as alternative language. I replaced hardcoded strings in template files and used built-in translation tools to translate template files. I'm using the functions ProcessWire provides:
      // using __() <?= __("There are no products to display.") ?> // or _x() <html lang="<?= echo _x('en', 'HTML language code'); ?>"> // Both works when I use _main.php When I disable auto including _main.php from config.php file, and move or rename _main.php to something else or use another file (other than  _main.php that comes with site-languages profile) for template specific markups, say markup/products.php like below, translations stop working.
      // config.php, disabled auto append // $config->appendTemplateFile = '_main.php'; // home.php include_once("./_main.php"); wireIncludeFile("./_main.php"); // TRANSLATION WORKS include_once("./_home.php"); // DOESNT WORK wireIncludeFile("markup/_main.php"); include_once("markup/_main.php"); // DOESNT WORK // NOTE: home.php and main.php has the same content, same permissions, same ownership I debugged the issue, and the reason, as ridiculous as it sounds, originates from using file other than _main.php. I searched the forums, google, but this seems like a unique issue. Is there anything I'm missing and how can I fix this, because I can't think of anything else after hours lost trying to find the underlying cause?
      Thanks in advance.
    • By Orkun
      I have set the right Datepicker translation file "/wire/modules/Jquery/JqueryUI/i18n/jquery.ui.datepicker-de.js".
      And change the code from:
      /* German initialisation for the jQuery UI date picker plugin. */ /* Written by Milian Wolff (mail@milianw.de). */ jQuery(function($){ $.datepicker.regional['de'] = { closeText: 'schließen', prevText: '<zurück', nextText: 'Vor>', currentText: 'heute', monthNames: ['Januar','Februar','März','April','Mai','Juni', 'Juli','August','September','Oktober','November','Dezember'], monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun', 'Jul','Aug','Sep','Okt','Nov','Dez'], dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'], dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'], dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'], weekHeader: 'KW', dateFormat: 'dd.mm.yy', firstDay: 1, isRTL: false, showMonthAfterYear: false, yearSuffix: ''}; $.datepicker.setDefaults($.datepicker.regional['de']); }); to:
      /* German initialisation for the jQuery UI date picker plugin. */ /* Written by Milian Wolff (mail@milianw.de). */ jQuery(function($){ $.datepicker.regional['de'] = { closeText: 'schließen', prevText: '<zurück', nextText: 'Vor>', currentText: 'heute', monthNames: ['Januar','Februar','März','April','Mai','Juni', 'Juli','August','September','Oktober','November','Dezember'], monthNamesShort: ['Jan','Feb','Mär','Apr','Mai','Jun', 'Jul','Aug','Sep','Okt','Nov','Dez'], dayNames: ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'], dayNamesShort: ['So','Mo','Di','Mi','Do','Fr','Sa'], dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'], weekHeader: 'KW', dateFormat: 'dd.mm.yy', firstDay: 1, isRTL: false, showMonthAfterYear: false, yearSuffix: ''}; $.datepicker.setDefaults($.datepicker.regional['de']); $.timepicker.regional['de'] = { timeText: 'Uhrzeit', hourText: 'Stunde', minuteText: 'Minute', secondText: 'Sekunde', millisecText: 'Millisekunde', timezoneText: 'Zeitzone', currentText: 'jetzt', closeText: 'schliessen', }; $.timepicker.setDefaults($.timepicker.regional['de']); }); But it only affects the date and not the time. What I'm missing?
    • By EvanFreyer
      Hey there,
      I have a problem with URL Segments and Umlauts in it. I want to grab the a segment of the url and use it as a parameter to find a something in Processwire. The problem is, that if I input an umlaut into the url, processwire immediately throws a 404 error page and I am not able to transform my inputs.
      Is there a way to get around that problem?
      Cheers,
      Evan
    • By MarcC
      I'm testing ProcessWIre's localization features, trying out fields like TextAreaLanguage and PageTitleLanguage. I've got them installed, assigned, and filled out. Now, how do I, for example, make Spanish-language fields show up on the public site for Spanish-language visitors?
      It feels like a bit of a dumb question, but I swear I looked at the docs!
      I thought it might be automatic detection based on a header string or something, but that hasn't worked for me. I don't know exactly what details to look for as this is my first time through. Thanks!
×
×
  • Create New...