├── composer.json ├── LICENSE ├── index.php ├── README.md └── composer.lock /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "oblik/kirby-plurals", 3 | "description": "Advanced pluralization for Kirby.", 4 | "type": "kirby-plugin", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Hristiyan Dodov", 9 | "email": "h.dodov@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "oblik/pluralization": "^1.3", 14 | "getkirby/composer-installer": "^1.1" 15 | }, 16 | "extra": { 17 | "installer-name": "plurals" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Oblik Studio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | $count], $locale); 62 | } 63 | 64 | function tpo($key, $position, $locale = null) 65 | { 66 | return tp($key, ['position' => $position], $locale); 67 | } 68 | 69 | function tpr($key, $start, $end, $locale = null) 70 | { 71 | return tp($key, ['start' => $start, 'end' => $end], $locale); 72 | } 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kirby-plurals 2 | 3 | Allows you to use language variables to translate a string according to that language's plural forms defined in [the Unicode CLDR](http://www.unicode.org/cldr/charts/27/supplemental/language_plural_rules.html). For more information, check [php-pluralization](https://github.com/OblikStudio/php-pluralization) which is a dependency of this plugin. 4 | 5 | ## Installation 6 | 7 | With [Composer](https://packagist.org/packages/oblik/kirby-plurals): 8 | 9 | ``` 10 | composer require oblik/kirby-plurals 11 | ``` 12 | 13 | ## Usage 14 | 15 | You get a `tp()` (translate plural) helper function that works similar to other [helper functions](https://getkirby.com/docs/reference/templates/helpers) and especially, [`tc()`](https://getkirby.com/docs/reference/templates/helpers/tc). 16 | 17 | Here's an example language file _en.php_: 18 | 19 | ```php 20 | return [ 21 | 'code' => 'en', 22 | 'default' => true, 23 | 'name' => 'English', 24 | 'translations' => [ 25 | 'apples' => [ 26 | 'one' => '{{ count }} apple', 27 | 'other' => '{{ count }} apples' 28 | ], 29 | 'place' => [ 30 | 'one' => '{{ position }}st', 31 | 'two' => '{{ position }}nd', 32 | 'few' => '{{ position }}rd', 33 | 'other' => '{{ position }}th' 34 | ], 35 | 'cookies' => [ 36 | 'other' => '{{ start }}-{{ end }} cookies' 37 | ] 38 | ] 39 | ]; 40 | ``` 41 | 42 | You can translate: 43 | 44 | - cardinals, by using a `count` key 45 | - ordinals, by using a `position` key 46 | - ranges, by using a `start` and an `end` key 47 | 48 | ```php 49 | tp('apples', [ 'count' => 1 ]); // 1 apple 50 | tp('apples', [ 'count' => 3 ]); // 3 apples 51 | tp('place', [ 'position' => 1 ]); // 1st 52 | tp('place', [ 'position' => 103 ]); // 103rd 53 | tp('cookies', [ 'start' => 3, 'end' => 4 ]); // 3-4 cookies 54 | ``` 55 | 56 | ### Locale Mapping 57 | 58 | If you're using different locale names, you can map them with the `oblik.plurals.map` config option. For example, if you have two languages `en-us` and `en-gb`, you need to map them both to the `en` pluralizator class since both of them have the same pluralization rules: 59 | 60 | `site/config/config.php` 61 | 62 | ```php 63 | return [ 64 | 'oblik.plurals.map' => [ 65 | 'en-us' => 'en', 66 | 'en-gb' => 'en' 67 | ] 68 | ]; 69 | ``` 70 | 71 | Check the available pluralization classes [here](https://github.com/OblikStudio/php-pluralization/blob/master/index.php). 72 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "cc300c81981c263503fd033c78b55150", 8 | "packages": [ 9 | { 10 | "name": "getkirby/composer-installer", 11 | "version": "1.1.4", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/getkirby/composer-installer.git", 15 | "reference": "2d6b8f5601a31caeeea45623e1643fbb437eb94e" 16 | }, 17 | "dist": { 18 | "type": "zip", 19 | "url": "https://api.github.com/repos/getkirby/composer-installer/zipball/2d6b8f5601a31caeeea45623e1643fbb437eb94e", 20 | "reference": "2d6b8f5601a31caeeea45623e1643fbb437eb94e", 21 | "shasum": "" 22 | }, 23 | "require": { 24 | "composer-plugin-api": "^1.0" 25 | }, 26 | "require-dev": { 27 | "composer/composer": "^1.8", 28 | "phpunit/phpunit": "^7.0" 29 | }, 30 | "type": "composer-plugin", 31 | "extra": { 32 | "class": "Kirby\\ComposerInstaller\\Plugin" 33 | }, 34 | "autoload": { 35 | "psr-4": { 36 | "Kirby\\": "src/" 37 | } 38 | }, 39 | "notification-url": "https://packagist.org/downloads/", 40 | "license": [ 41 | "MIT" 42 | ], 43 | "description": "Kirby's custom Composer installer for the Kirby CMS and for Kirby plugins", 44 | "homepage": "https://getkirby.com", 45 | "time": "2019-02-11T20:27:36+00:00" 46 | }, 47 | { 48 | "name": "oblik/pluralization", 49 | "version": "1.3.0", 50 | "source": { 51 | "type": "git", 52 | "url": "https://github.com/OblikStudio/php-pluralization.git", 53 | "reference": "0c00d172095b7e0ff75bca630940170a47d0c2d9" 54 | }, 55 | "dist": { 56 | "type": "zip", 57 | "url": "https://api.github.com/repos/OblikStudio/php-pluralization/zipball/0c00d172095b7e0ff75bca630940170a47d0c2d9", 58 | "reference": "0c00d172095b7e0ff75bca630940170a47d0c2d9", 59 | "shasum": "" 60 | }, 61 | "require-dev": { 62 | "phpunit/phpunit": "^8.3" 63 | }, 64 | "type": "library", 65 | "autoload": { 66 | "files": [ 67 | "index.php" 68 | ], 69 | "psr-4": { 70 | "Oblik\\Pluralization\\": "src/" 71 | } 72 | }, 73 | "notification-url": "https://packagist.org/downloads/", 74 | "license": [ 75 | "MIT" 76 | ], 77 | "authors": [ 78 | { 79 | "name": "Hristiyan Dodov", 80 | "email": "h.dodov@gmail.com" 81 | } 82 | ], 83 | "description": "Pluralization according to Unicode", 84 | "time": "2020-03-30T05:24:57+00:00" 85 | } 86 | ], 87 | "packages-dev": [], 88 | "aliases": [], 89 | "minimum-stability": "stable", 90 | "stability-flags": [], 91 | "prefer-stable": false, 92 | "prefer-lowest": false, 93 | "platform": [], 94 | "platform-dev": [] 95 | } 96 | --------------------------------------------------------------------------------