asbjorn Posted April 18, 2017 Share Posted April 18, 2017 I have a webpage that I (think have) worked well with translation of dates based on a setlocale() code in a _init.php file until now. The page is in Norwegian (default) and English (english). But the date shown front end is now only in English. I have a multi-language textfield (locale) where I input nb_NO and en_US, and then I use the follow code in _init.php: $homepage = $pages->get('/'); // For change of language, set default language switch($user->language->name) { case 'default': setlocale (LC_ALL, $homepage->locale); break; } To check what is output, I used this: echo setlocale(LC_ALL, 0); And it only outputs C both on the Norwegian and English page. (It should output nb_NO and en_US). I have the same setup on another page, at least I think so, but something must be different. I also tried changing the locale, and editing LanguageSupport.module, removing the setlocale() code from above, with no luck. I check both in development (MAMP) and on my production server. I use ProcessWire 3.0.59 (live) and 3.0.60 (development). There could be other code I have that messes this up. But I hope this could shine som light. My question is: What decides the locale and how could I fix the «C»? Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 18, 2017 Share Posted April 18, 2017 I'm not sure this is the issue, because you're experiencing it locally as well as in production, but setlocale() can only work if the locale is actually installed on the running machine. If not it will silently refuse to change. Link to comment Share on other sites More sharing options...
abdus Posted April 18, 2017 Share Posted April 18, 2017 Not a definitive solution, but you can use strftime for locale-specific dates. From its documentation Quote Format the time and/or date according to locale settings. Month and weekday names and other language-dependent strings respect the current locale set with setlocale(). then you'd do something like this. <?php function formatDate($format, $timestamp) { // set first possible locale from the list setlocale(LC_TIME, ['no_NO', 'no_NN', 'no_NB']); return strftime($format, $timestamp); } One thing to note is that you need to use strftime specific date format. 1 Link to comment Share on other sites More sharing options...
asbjorn Posted April 19, 2017 Author Share Posted April 19, 2017 Thank you, @abdus, your solution worked. So it buys me some time to figure out what happened with my initial setup. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now