Translator.php
2.56 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos;
/**
* Basic english only 'translator' for diffForHumans()
*/
class Translator
{
/**
* Translation strings.
*
* @var array
*/
public static $strings = [
'year' => '1 year',
'year_plural' => '{count} years',
'month' => '1 month',
'month_plural' => '{count} months',
'week' => '1 week',
'week_plural' => '{count} weeks',
'day' => '1 day',
'day_plural' => '{count} days',
'hour' => '1 hour',
'hour_plural' => '{count} hours',
'minute' => '1 minute',
'minute_plural' => '{count} minutes',
'second' => '1 second',
'second_plural' => '{count} seconds',
'ago' => '{time} ago',
'from_now' => '{time} from now',
'after' => '{time} after',
'before' => '{time} before',
];
/**
* Check if a translation key exists.
*
* @param string $key The key to check.
* @return bool Whether or not the key exists.
*/
public function exists($key)
{
return isset(static::$strings[$key]);
}
/**
* Get a plural message.
*
* @param string $key The key to use.
* @param string $count The number of items in the translation.
* @param array $vars Additional context variables.
* @return string The translated message or ''.
*/
public function plural($key, $count, array $vars = [])
{
if ($count == 1) {
return $this->singular($key, $vars);
}
return $this->singular($key . '_plural', ['count' => $count] + $vars);
}
/**
* Get a singular message.
*
* @param string $key The key to use.
* @param array $vars Additional context variables.
* @return string The translated message or ''.
*/
public function singular($key, array $vars = [])
{
if (isset(static::$strings[$key])) {
$varKeys = array_keys($vars);
foreach ($varKeys as $i => $k) {
$varKeys[$i] = '{' . $k . '}';
}
return str_replace($varKeys, $vars, static::$strings[$key]);
}
return '';
}
}