vojtasvoboda / oc-twigextensions-plugin

Twig extensions plugin for OctoberCMS
MIT License
18 stars 26 forks source link

How can I translate the month to my own language? #42

Closed Hessel91 closed 2 years ago

Hessel91 commented 6 years ago

Thanks for this plugin, great work! can you please explain to me how I manage to translate the mont variable into my own language?

{{ post.created_at | strftime('%d.%B.%Y') }} |

bastihilger commented 6 years ago

Hey there,

strftime uses the locale set in your PHP location, which can be changed using setlocale(). You could also change the locale in your OctoberCMS config. For correct translations you would need to make sure that the locale you need is installed on your server.

See the PHP docs for strftime.

And here is an explanation how to add locales to your server (for Ubuntu).

bastihilger commented 6 years ago

Sorry, you actually do have to use setlocale() somewhere to localize strftime (changing the locale in config/app.php has no effect). If you are using a plugin you could use it in the boot() method of your Plugin.php, otherwise you could set it for example at the beginning of your config/app.php.

vojtasvoboda commented 6 years ago

I think that set locale and timezone at /config/app.php should be enough. Otherwise try setLocale() as @manogi suggested.

bastihilger commented 6 years ago

In my case setting locale and timezone did nothing. Also: I tested this with the German locale, and as on my server the installed locale is called "de_DE" I had to call setlocale(LC_TIME, 'de_DE'). It really depends on which locales are installed on your server. Had there been one just called "de" I could have used setlocale(LC_TIME, 'de').

disvst commented 6 years ago

@manogi I have exactly the same issue. Filters seem to work fine, but I can't get monthes displayed in french... Did you achieve this?

bastihilger commented 6 years ago

Hey @didiervassout - a few comments above I wrote about how I managed to get it to work. Have you tried this and do you have specific questions concerning theses steps?

disvst commented 6 years ago

@manogi Yes, I don't understand how to use the boot method in order to have localized dates in my Plugins.

I tried but monthes are still in english...

public function boot() { setlocale(LC_TIME, "fr_FR"); date_default_timezone_set('Europe/Paris'); Carbon::setLocale('fr'); }

Sorry, you actually do have to use setlocale() somewhere to localize strftime (changing the locale in config/app.php has no effect). If you are using a plugin you could use it in the boot() method of your Plugin.php, otherwise you could set it for example at the beginning of your config/app.php.

bastihilger commented 6 years ago

@didiervassout have you checked it fr_FR is actually installed on your server?

disvst commented 6 years ago

@manogi I did and it's installed.

bastihilger commented 6 years ago

@didiervassout

What is your operating system? LC_TIME seems to have problems on some Windows systems, you could try LC_ALL instead.

If you use Linux, what is the output of locale -a?

You don't need Carbon for this, by the way.

Sorry I cannot be of any more help, but that is exactly what I have done:

1.) made sure that the locale is installed on my server

2.) put this at the beginning of my boot() method:

setlocale(LC_TIME, 'de_DE');

If i remove it, the dates are in English, and if I change "de_DE" to "fr_FR" the dates are in French.

disvst commented 6 years ago

@manogi Many thanks for your help. I will try again your procedure, I found a walkaround for the moment.