Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a setting for date translations #927

Open
paulcmal opened this issue Jul 11, 2016 · 14 comments
Open

Add a setting for date translations #927

paulcmal opened this issue Jul 11, 2016 · 14 comments

Comments

@paulcmal
Copy link

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:

  • days
    • D: Mon → Sun
    • l: Sunday → Saturday
    • S: st/rd/th
  • months
    • F: January → December
    • M: Jan → Dec

Please note that a and A (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.

@mahagr
Copy link
Member

mahagr commented Jul 11, 2016

We might want to use the same solution in Gantry/Grav, we just need to figure out what it would be:

gantry/gantry5#221

@paulcmal
Copy link
Author

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?

@mahagr
Copy link
Member

mahagr commented Jul 14, 2016

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.

@paulcmal
Copy link
Author

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.

@mahagr
Copy link
Member

mahagr commented Jul 14, 2016

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.

@paulcmal
Copy link
Author

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?

@indigoxela
Copy link

indigoxela commented Jan 18, 2017

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

@flaviocopes
Copy link
Contributor

@indigoxela not sure if you are aware, the Intl twig extension can be added using https://github.com/Perlkonig/grav-plugin-twig-extensions

@indigoxela
Copy link

@flaviocopes I am aware of the plugin.
Thanks for adding a link to the plugin repo (which I forgot).

@flaviocopes
Copy link
Contributor

@indigoxela I added your approach to the Learn site getgrav/grav-learn@cd11517

@berteh
Copy link

berteh commented May 23, 2018

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

@DominiqueVial
Copy link

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?

@Perlkonig
Copy link
Contributor

Why only "core" Grav? There are Twig extensions that will localize dates.

@DominiqueVial
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants