Karmalakas / grav-plugin-translate-date

Define date formats for each language and easily use for dates in your templates
MIT License
7 stars 5 forks source link

Translate Date Plugin

The Translate Date Plugin is an extension for Grav CMS. Define date formats for each language and easily use for dates in your templates via Twig filter |td

Usage

Basic

If you use basic processor, you should add months and weekdays names in your language to your languages file. Eg.:

en:
  PLUGIN_TRANSLATE_DATE:
    F: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
    M: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    l: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    D: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']

If you intend to use only full names (F and l), you can skip adding translations - these will default to Grav's core translations.

Date format options can be found here.
If format for your language is not found in config, it will default to ISO date + 24 hours format time - Y-m-d H:i.

Intl

NB: Intl extension must be enabled in PHP in order to use it.

Date format options can be found here.
If format for your language is not found in config, it will default to language based locale default format.

Examples

In Twig template you have several options how to use date translations:

You can force both language and format too. Eg. {{ page.date|td('de', 'Y M l') }}

Configuration

Before configuring this plugin, you should copy the user/plugins/translate-date/translate-date.yaml to user/config/plugins/translate-date.yaml and only edit that copy.

Here is the default configuration and an explanation of available options:

enabled: true
processor: basic # `basic|intl`
formats:
  cs: 'd.m.Y H:i'
  de: 'd.m.Y H:i'
  en: 'm/d/Y h:iA'
  es: 'd/m/Y H:i'
  id: 'd/m/Y H:i'
  lt: 'Y-m-d H:i'

Note that if you use the Admin Plugin, a file with your configuration named translate-date.yaml will be saved in the user/config/plugins/-folder once the configuration is saved in the Admin.

Installation

Installing the Translate Date plugin can be done in one of three ways: The GPM (Grav Package Manager) installation method lets you quickly install the plugin with a simple terminal command, the manual method lets you do so via a zip file, and the admin method lets you do so via the Admin Plugin.

GPM Installation (Preferred)

To install the plugin via the GPM, through your system's terminal (also called the command line), navigate to the root of your Grav-installation, and enter:

bin/gpm install translate-date

This will install the Translate Date plugin into your /user/plugins-directory within Grav. Its files can be found under /your/site/grav/user/plugins/translate-date.

Manual Installation

To install the plugin manually, download the zip-version of this repository and unzip it under /your/site/grav/user/plugins. Then rename the folder to translate-date. You can find these files on GitHub or via GetGrav.org.

You should now have all the plugin files under

/your/site/grav/user/plugins/translate-date

Admin Plugin

If you use the Admin Plugin, you can install the plugin directly by browsing the Plugins-menu and clicking on the Add button.