From 10e7a30cee74cc630e02a231684cf8c0c55afe3c Mon Sep 17 00:00:00 2001 From: Amegatron Date: Sun, 21 Dec 2014 20:06:18 +0300 Subject: [PATCH] Added support of months in genitive --- .gitignore | 1 + README.md | 24 ++++++++++--------- .../LocalizedCarbon/LocalizedCarbon.php | 12 ++++++++++ src/lang/en/months.php | 16 +++++++++++++ src/lang/ru/months.php | 16 +++++++++++++ src/lang/uk/months.php | 16 +++++++++++++ 6 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 src/lang/en/months.php create mode 100644 src/lang/ru/months.php create mode 100644 src/lang/uk/months.php diff --git a/.gitignore b/.gitignore index 5826402..31d48a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea /vendor composer.phar composer.lock diff --git a/README.md b/README.md index a9f66e2..cdd8a20 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ See also [localized documentation](docs) ## Introduction -Localized Carbon is an extension of a popular Carbon package, designed specially for Laravel framework. By localization I mean its `diffForHumans` function, which returns a human-readable string of time interval. +Localized Carbon is an extension of a popular Carbon package, designed specially for Laravel framework. By localization I mean its `diffForHumans` function, which returns a human-readable string of time interval. This package also supports genitive months by introducing the "%f" key in `formatLocalized` method. ## Usage @@ -42,16 +42,16 @@ Also `LocalizedCarbon` adds an optional second argument, in which you may specif Current version of Localized Carbon ships with these localizations: -+ English (en) -+ Russian (ru) -+ Ukrainian (uk) -+ Dutch (nl) -+ Spanish (es) -+ Portuguese (pt) -+ French (fr) -+ Bulgarian (bg) -+ Slovakian (sk) -+ Turkish (tr) ++ English (en) (full) ++ Russian (ru) (full) ++ Ukrainian (uk) (full) ++ Dutch (nl) (no genitive) ++ Spanish (es) (no genitive) ++ Portuguese (pt) (no genitive) ++ French (fr) (no genitive) ++ Bulgarian (bg) (no genitive) ++ Slovakian (sk) (no genitive) ++ Turkish (tr) (no genitive) But it is extendable, so you may write and use your own localization without altering the contents of the package. See [extending Localized Carbon](#extending). @@ -144,3 +144,5 @@ If you've written a formatter for the language which is not supported by current The formatter should lie in `src/Laravelrus/LocalizedCarbon/DiffFormatters` directory, following a simple naming convention: the class name should start with the desired language in lower-case, but the first letter in upper-case. The rest part of the name should be "DiffFormatter". The file name should correspond to the class name. For example, the formatter for `fr` language would lie in `src/Laravelrus/LocalizedCarbon/DiffFormatters/FrDiffFormatter.php`, and the class name would be `FrDiffFormatter`. + +Also I need the help of the community to complete the list of genitives for all supported languages. If you know a language and it uses genitives in dates, feel free to contribute. See an example of Russian or Ukranian `lang\XX\months.php` files. \ No newline at end of file diff --git a/src/Laravelrus/LocalizedCarbon/LocalizedCarbon.php b/src/Laravelrus/LocalizedCarbon/LocalizedCarbon.php index 07bc96a..13b6850 100644 --- a/src/Laravelrus/LocalizedCarbon/LocalizedCarbon.php +++ b/src/Laravelrus/LocalizedCarbon/LocalizedCarbon.php @@ -65,4 +65,16 @@ public function diffForHumans(Carbon $other = null, $formatter = null) { return $result; } + + public function formatLocalized($format = self::COOKIE) { + $result = parent::formatLocalized($format); + + if (strpos($result, '%f') !== false) { + $langKey = strtolower(parent::format("F")); + $replace = \Lang::get("localized-carbon::months." . $langKey); + $result = str_replace('%f', $replace, $result); + } + + return $result; + } } diff --git a/src/lang/en/months.php b/src/lang/en/months.php new file mode 100644 index 0000000..6e0f084 --- /dev/null +++ b/src/lang/en/months.php @@ -0,0 +1,16 @@ + "January", + "february" => "February", + "march" => "March", + "april" => "April", + "may" => "May", + "june" => "June", + "july" => "July", + "august" => "August", + "september" => "September", + "october" => "October", + "november" => "November", + "december" => "December", +); diff --git a/src/lang/ru/months.php b/src/lang/ru/months.php new file mode 100644 index 0000000..7e7994e --- /dev/null +++ b/src/lang/ru/months.php @@ -0,0 +1,16 @@ + "января", + "february" => "февраля", + "march" => "марта", + "april" => "апреля", + "may" => "мая", + "june" => "июня", + "july" => "июля", + "august" => "августа", + "september" => "сентября", + "october" => "октября", + "november" => "ноября", + "december" => "декабря", +); diff --git a/src/lang/uk/months.php b/src/lang/uk/months.php new file mode 100644 index 0000000..9d94ad0 --- /dev/null +++ b/src/lang/uk/months.php @@ -0,0 +1,16 @@ + "січня", + "february" => "лютого", + "march" => "березня", + "april" => "квітня", + "may" => "травня", + "june" => "червня", + "july" => "липня", + "august" => "серпня", + "september" => "вересня", + "october" => "жовтня", + "november" => "листопада", + "december" => "грудня", +);