oblik/kirby-plurals

Advanced pluralization for Kirby.

Installs: 178

Dependents: 0

Suggesters: 0

Security: 0

Stars: 17

Watchers: 6

Forks: 1

Open Issues: 0

Type:kirby-plugin

1.0.0 2020-03-30 07:45 UTC

This package is auto-updated.

Last update: 2024-12-04 21:41:11 UTC


README

Allows you to use language variables to translate a string according to that language's plural forms defined in the Unicode CLDR. For more information, check php-pluralization which is a dependency of this plugin.

Installation

With Composer:

composer require oblik/kirby-plurals

Usage

You get a tp() (translate plural) helper function that works similar to other helper functions and especially, tc().

Here's an example language file en.php:

return [
    'code' => 'en',
    'default' => true,
    'name' => 'English',
    'translations' => [
        'apples' => [
            'one' => '{{ count }} apple',
            'other' => '{{ count }} apples'
        ],
        'place' => [
            'one' => '{{ position }}st',
            'two' => '{{ position }}nd',
            'few' => '{{ position }}rd',
            'other' => '{{ position }}th'
        ],
        'cookies' => [
            'other' => '{{ start }}-{{ end }} cookies'
        ]
    ]
];

You can translate:

  • cardinals, by using a count key
  • ordinals, by using a position key
  • ranges, by using a start and an end key
tp('apples', [ 'count' => 1 ]);                 // 1 apple
tp('apples', [ 'count' => 3 ]);                 // 3 apples
tp('place', [ 'position' => 1 ]);               // 1st
tp('place', [ 'position' => 103 ]);             // 103rd
tp('cookies', [ 'start' => 3, 'end' => 4 ]);    // 3-4 cookies

Locale Mapping

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:

site/config/config.php

return [
    'oblik.plurals.map' => [
        'en-us' => 'en',
        'en-gb' => 'en'
    ]
];

Check the available pluralization classes here.