forked from OblikStudio/kirby-plurals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
52 lines (41 loc) · 1.39 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
use Kirby\Toolkit\Str;
use Kirby\Toolkit\I18n;
use const Oblik\Pluralization\LANGUAGES;
function tp($key, array $data, $locale = null)
{
$lc = $locale ?? I18n::locale();
$map = option('oblik.plurals.map');
if (is_array($map) && !empty($map[$lc])) {
$lc = $map[$lc];
}
$pluralizer = LANGUAGES[$lc] ?? null;
if ($pluralizer) {
if (isset($data['count'])) {
$form = $pluralizer::getCardinal($data['count']);
} else if (isset($data['position'])) {
$form = $pluralizer::getOrdinal($data['position']);
} else if (isset($data['start']) && isset($data['end'])) {
$form = $pluralizer::getRange($data['start'], $data['end']);
}
if (isset($form)) {
$form = $pluralizer::formName($form);
$translations = I18n::translation($locale)[$key] ?? null;
if (is_array($translations)) {
$translation = $translations[$form] ?? null;
}
if (empty($translation)) {
$translation = I18n::translation($locale)[$key . ".$form"] ?? null;
}
if (is_string($translation)) {
return Str::template($translation, $data);
}
}
}
$fallback = I18n::fallback();
if ($fallback !== $locale) {
return tp($key, $data, $fallback);
} else {
return null;
}
}