Open paulcmal opened 8 years ago
We might want to use the same solution in Gantry/Grav, we just need to figure out what it would be:
I just experimented with jenssegers/date and came up with grav-plugin-datetranslations.
I setup a test site with a date template:
{% extends 'partials/base.html.twig' %}
{% block content %}
First day of next month: {{ date_translate('first day of next month', 'd F Y') }}
<br>
Last day of next month: {{ date_translate('last day of next month', 'l') }}
{{ page.content }}
{% endblock %}
And a 02.date/date.en.md
page:
---
title: Date test
---
# TEST
This is just a test. According to the twig template, date translated in the current active language should be displayed above this.
This page only exists as `date.en.md`, there is no French translation for the Markdown.
Please note that I did not provide a page translation to be sure the wrong language wouldn't be picked up. You can see it here and here.
The code is here.
Could some people try it out as well?
I'm not sure if this is enough; to translate time you also need to consider different format from country to country, like 01/31/2016 vs 31.01.2016.
Well, this is true.
But first, that's already a lot better than what we have now in Grav... which is nothing.
Second, what you say is actually implementable. There's been a discussion about it, and we could probably implement this solution.
That would mean having translation strings for the date formats in every language, and then using these translation strings as format to translate the dates with.
It actually makes a lot of sense.
The question is just, can we find localized dateFormat strings to work with, or do we have to write our own? See this pull request for some strings.
One way I have been thinking of, is to have a map of formats like
['d F Y' => 'd.m.Y']
That would be just a simple translate of the format into another one based on locale. If there's no mapping found, it could just use the one specified.
Okay I've updated the repo.
Now I'm using a languages.yaml
for date format translations.
You can use the function dt
as a Twig function and/or filter, like 'now'|dt('short')
Could somebody try messing around with it to see if it behaves as expected?
Hi, another way to get dates localized would be to use the Intl extension for Twig. https://twig-extensions.readthedocs.io/en/latest/intl.html
Example:
{{ page.date|localizeddate('long', 'none', 'fr') }}
outputs
16 décembre 2016
This filter is less flexible though. PHP date format characters aren't supported.
Update: there is support for a different date pattern.
Example:
{{ page.date|localizeddate('long', 'none', 'de', 'Europe/Berlin', 'EEEE MMMM') }}
outputs
Montag Januar
@indigoxela not sure if you are aware, the Intl twig extension can be added using https://github.com/Perlkonig/grav-plugin-twig-extensions
@flaviocopes I am aware of the plugin. Thanks for adding a link to the plugin repo (which I forgot).
@indigoxela I added your approach to the Learn site https://github.com/getgrav/grav-learn/commit/cd11517dfc6d7c63e1806632a4cb8225f5fb65f5
pardon my stupid question, but why not rely on the Intl functions of php ? They handle these details pretty well, and could be used based on the active language IntlDateFormatter
Sorry for another stupid question but does this mean current version of Grav (1.5) does not handle date translations from scratch? And does this also mean that if I want to use only core features for example to create a blog, then displaying date to blog items, these dates won't be localised?
Why only "core" Grav? There are Twig extensions that will localize dates.
Thank you for your answer. I have found this link earlier in the issue.
Why only "core" Grav? Because "core" Grav manage – or is said to manage or have to manage – multilanguage. And it appear to not be true. The effort has been stopped before reaching the final goal.
Of course one with few skills can do the trick using Twig or PHP but this is not the point. From scratch Grav does not achieve it completely and skills are needed to reach the goal. So this is not easy for common people.
By default, dates are not translated in Grav, which is a concern for internationalization.
Translating dates is a challenge, but jenssegers/date does it very well. Otherwise, we can hack around with regex, like I did here.
The thing is once the site admin has changed the default dateformat to match their language, we don't have so much to translate.
The [date] page in the PHP manual lists the following things to translate:
Please note that
a
andA
(am/pm) don't need translation as it's mostly an English-speaking concern.S would be the most difficult parameter to translate due
The question is, can we have a core setting to enable/disable date translations, so each subproject doesn't need its own setting for this?
If we can get a core setting for date translations, I'll be glad to write a Twig function to handle this and PR translated dates support in Antimatter and archives plugin.