diff --git a/CHANGELOG.md b/CHANGELOG.md index 90722f5..13a35fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,23 +1,32 @@ +# v0.2.2 +## 20-03-2021 + +1. [](#bugfix) + * Fix how language is parsed if not provided +2. [](#improved) + * Update README + * Prepare for a release to submit to GPM + # v0.2.1 -## 03/11/2021 +## 11-03-2021 1. [](#bugfix) * Fix getting custom format for Intl # v0.2.0 -## 03/11/2021 +## 11-03-2021 1. [](#new) - * Add Intl support (may choose via config) + * Add Intl support (may choose via config) # v0.1.1 -## 03/09/2021 +## 09-03-2021 1. [](#bugfix) * Fallback to `GRAV.` translation if custom translation not found # v0.1.0 -## 03/08/2021 +## 08-03-2021 1. [](#new) - * ChangeLog started... + * ChangeLog started... diff --git a/README.md b/README.md index 9d4e0c7..f0e6a13 100644 --- a/README.md +++ b/README.md @@ -3,39 +3,46 @@ The **Translate Date** Plugin is an extension for [Grav CMS](http://github.com/getgrav/grav). Define date formats for each language and easily use for dates in your templates via Twig filter `|td` -## 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. +## Usage -> **NB:** Currently, only manual installation available. Maybe plugin will be added to GPM in the future, but not yet. +### Basic -### GPM Installation (Preferred) +If you use `basic` processor, you should add months and weekdays names in your language to your languages file. Eg.: -To install the plugin via the [GPM](http://learn.getgrav.org/advanced/grav-gpm), through your system's terminal (also -called the command line), navigate to the root of your Grav-installation, and enter: +```yml +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'] +``` - bin/gpm install translate-date +If you intend to use only full names (`F` and `l`), you can skip adding translations - these will default to Grav's core +translations. -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`. +Date format options can be found [here](https://www.php.net/manual/en/datetime.format.php). +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`. -### Manual Installation +### Intl -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](https://github.com/karmalakas/grav-plugin-translate-date) or -via [GetGrav.org](http://getgrav.org/downloads/plugins#extras). +> **NB:** Intl extension must be enabled in PHP in order to use it. -You should now have all the plugin files under +Date format options can be found [here](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table). +If format for your language is not found in config, it will default to language based locale default format. - /your/site/grav/user/plugins/translate-date +### Examples -### Admin Plugin +In Twig template you have several options how to use date translations: +- Leave to config - `{{ page.date|td }}` + This will take date format from config and translate months and/or weekdays if found + Eg. `Y-m l` will become `2021-03 Tuesday` +- Force language - `{{ page.date|td('lt') }}` + Even on EN page `Y-m D` will become `2021-03 Ant` +- Force format - `{{ page.date|td(null, 'eeee, MMMM dd YYYY, HH:mm:ss') }}` (Intl format) + This will output `Thursday, March 11 2021, 03:15:16` if Intl processor is chosen -If you use the Admin Plugin, you can install the plugin directly by browsing the `Plugins`-menu and clicking on -the `Add` button. +You can force both language and format too. Eg. `{{ page.date|td('de', 'Y M l') }}` ## Configuration @@ -55,47 +62,37 @@ formats: 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. -## Usage - -### Basic +## Installation -If you use `basic` processor, you should add months and weekdays names in your language to your languages file. Eg.: +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. -```yml -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'] -``` +### GPM Installation (Preferred) -If you intend to use only full names (`F` and `l`), you can skip adding translations - these will default to Grav's core -translations. +To install the plugin via the [GPM](http://learn.getgrav.org/advanced/grav-gpm), through your system's terminal (also +called the command line), navigate to the root of your Grav-installation, and enter: -Date format options can be found [here](https://www.php.net/manual/en/datetime.format.php). -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`. + 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`. -### Intl +### Manual Installation -> **NB:** Intl extension must be enabled in PHP in order to use it. +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](https://github.com/karmalakas/grav-plugin-translate-date) or +via [GetGrav.org](http://getgrav.org/downloads/plugins#extras). -Date format options can be found [here](https://unicode-org.github.io/icu/userguide/format_parse/datetime/#date-field-symbol-table). -If format for your language is not found in config, it will default to language based locale default format. +You should now have all the plugin files under -### Examples + /your/site/grav/user/plugins/translate-date -In Twig template you have several options how to use date translations: -- Leave to config - `{{ page.date|td }}` - This will take date format from config and translate months and/or weekdays if found - Eg. `Y-m l` will become `2021-03 Tuesday` -- Force language - `{{ page.date|td('lt') }}` - Even on EN page `Y-m D` will become `2021-03 Ant` -- Force format - `{{ page.date|td(null, 'eeee, MMMM dd YYYY, HH:mm:ss') }}` (Intl format) - This will output `Thursday, March 11 2021, 03:15:16` if Intl processor is chosen +### Admin Plugin -You can force both language and format too. Eg. `{{ page.date|td('de', 'Y M l') }}` +If you use the Admin Plugin, you can install the plugin directly by browsing the `Plugins`-menu and clicking on +the `Add` button. ## To Do diff --git a/blueprints.yaml b/blueprints.yaml index 3e8af08..297d126 100644 --- a/blueprints.yaml +++ b/blueprints.yaml @@ -1,15 +1,13 @@ name: Translate Date slug: translate-date type: plugin -version: 0.2.1 +version: 0.2.2 description: Define date formats for each language and easily use for dates in your templates -icon: plug +icon: calendar author: name: Karmalakas - email: cronus.raven@gmail.com homepage: https://github.com/Karmalakas/grav-plugin-translate-date -demo: http://demo.yoursite.com -keywords: grav, plugin, etc +keywords: grav, plugin, date, translation, languages, multi-language bugs: https://github.com/Karmalakas/grav-plugin-translate-date/issues docs: https://github.com/Karmalakas/grav-plugin-translate-date/blob/develop/README.md license: MIT diff --git a/classes/Twig/FilterTd.php b/classes/Twig/FilterTd.php index 74c75a4..0d0fa8e 100644 --- a/classes/Twig/FilterTd.php +++ b/classes/Twig/FilterTd.php @@ -58,7 +58,8 @@ public function translateDate($date, ?string $language = null, ?string $format = return $date; } - $format = $format ?: $this->getFormat($language); + $language = $language ?? $this->getLanguage(); + $format = $format ?: $this->getFormat($language); if ($this->config->get('plugins.translate-date.processor') === 'intl') { return $this->translateDateIntl($date, $language, $format); @@ -68,41 +69,61 @@ public function translateDate($date, ?string $language = null, ?string $format = } /** - * @param string|null $language + * @return string + */ + protected function getLanguage(): string + { + $language = $this->language->getLanguage() ?: null; + + if ($language) { + return $language; + } + + if ($this->config->get('plugins.translate-date.processor') === 'intl') { + return \Locale::getDefault(); + } + + return 'en'; + } + + /** + * @param string $language * * @return string */ - protected function getFormat(?string $language = null): string + protected function getFormat(string $language): string { - $formats = $this->config->get('plugins.translate-date.formats', []); - $language = $language ?? ($this->language->getLanguage() ?: null); + $formats = $this->config->get('plugins.translate-date.formats', []); - if (empty($formats[$language])) { - $language = $this->language->getFallbackLanguages($language, true)[0] ?? null; + if (!empty($formats[$language])) { + return (string)$formats[$language]; } - if ( - empty($formats[$language]) - && $this->config->get('plugins.translate-date.processor') !== 'intl' - ) { - return 'Y-m-d H:i'; + if ($this->config->get('plugins.translate-date.processor') === 'intl') { + $formatter = \IntlDateFormatter::create( + $language, + \IntlDateFormatter::NONE, + \IntlDateFormatter::NONE + ); + + return $formatter->getPattern(); } - return $formats[$language] ?? (string)$formats[$language] :: null; + return 'Y-m-d H:i'; } /** - * @param DateTime $date - * @param string|null $language - * @param string|null $format + * @param DateTime $date + * @param string $locale + * @param string $format * * @return string */ - protected function translateDateIntl(DateTime $date, ?string $language = null, ?string $format = null): string + protected function translateDateIntl(DateTime $date, string $locale, string $format): string { try { $formatter = \IntlDateFormatter::create( - $language ?? \Locale::getDefault(), + $locale, \IntlDateFormatter::NONE, \IntlDateFormatter::NONE, \IntlTimeZone::createTimeZone($date->getTimezone()->getName()), @@ -112,7 +133,11 @@ protected function translateDateIntl(DateTime $date, ?string $language = null, ? return $formatter->format($date->getTimestamp()); } catch (\Exception $exception) { - return $date->format($this->getFormat($language)); + $language_parts = explode('_', $locale); + + return $date->format( + $this->getFormat(reset($language_parts)) + ); } } @@ -123,9 +148,9 @@ protected function translateDateIntl(DateTime $date, ?string $language = null, ? * * @return string */ - protected function translateDateBasic(DateTime $date, ?string $language = null, ?string $format = null): string + protected function translateDateBasic(DateTime $date, string $language, string $format): string { - $languages = $language ? [$language] : null; + $languages = [$language]; $replacements = $this->getReplacements($date, $format, $languages); if (empty($replacements)) { @@ -143,7 +168,7 @@ protected function translateDateBasic(DateTime $date, ?string $language = null, continue; } - $format = substr_replace($format, $index, $pos, strlen($data['char'])); + $format = (string)substr_replace($format, $index, $pos, strlen($data['char'])); } return str_replace( @@ -154,13 +179,13 @@ protected function translateDateBasic(DateTime $date, ?string $language = null, } /** - * @param DateTime $date - * @param string $format - * @param array|null $languages + * @param DateTime $date + * @param string $format + * @param array $languages * * @return array */ - protected function getReplacements(DateTime $date, string $format, ?array $languages): array + protected function getReplacements(DateTime $date, string $format, array $languages): array { $replacements = []; diff --git a/translate-date.yaml b/translate-date.yaml index a7134ff..29e2833 100644 --- a/translate-date.yaml +++ b/translate-date.yaml @@ -1,5 +1,5 @@ enabled: true -processor: basic # Can also be `intl` +processor: basic # `basic|intl` formats: en: 'm/d/Y h:mA' lt: 'Y-m-d H:i'