Erebot  latest
A modular IRC bot for PHP 5.3+
DurationVariable.php
1 <?php
2 /*
3  This file is part of Erebot, a modular IRC bot written in PHP.
4 
5  Copyright © 2010 François Poirotte
6 
7  Erebot is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Erebot is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Erebot. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Erebot\Styling\Variables;
22 
27 class DurationVariable implements \Erebot\Styling\Variables\DurationInterface
28 {
30  protected $value;
31 
38  public function __construct($value)
39  {
40  $this->value = $value;
41  }
42 
44  {
45  $locale = $translator->getLocale(\Erebot\IntlInterface::LC_MESSAGES);
46  $coreTranslator = new \Erebot\Intl('Erebot\\Styling');
47  $coreTranslator->setLocale(\Erebot\IntlInterface::LC_MESSAGES, $locale);
48 
49  // DO NOT CHANGE THE CODE BELOW, ESPECIALLY COMMENTS & WHITESPACES.
50  // It has all been carefully crafted to make both xgettext and
51  // PHP_CodeSniffer happy! Also, it avoids relying on OS line endings
52  // as it breaks xgettext on at least Windows platforms.
53 
54  $rule = $coreTranslator->_(
55  // I18N: ICU rule used to format durations (using words).
56  // Eg. 12345 becomes "3 hours, 25 minutes, 45 seconds" (in english).
57  // For examples of valid rules, see: http://goo.gl/q94xS
58  // For the complete syntax, see also: http://goo.gl/jp2Bd
59  "%with-words:\n".
60  " 0: 0 seconds;\n".
61  " 1: 1 second;\n".
62  " 2: =#0= seconds;\n".
63  " 60/60: <%%min<;\n".
64  " 61/60: <%%min<, >%with-words>;\n".
65  " 3600/60: <%%hr<;\n".
66  " 3601/60: <%%hr<, >%with-words>;\n".
67  " 86400/86400: <%%day<;\n".
68  " 86401/86400: <%%day<, >%with-words>;\n".
69  " 604800/604800: <%%week<;\n".
70  " 604801/604800: <%%week<, >%with-words>;\n".
71  "%%min:\n".
72  " 1: 1 minute;\n".
73  " 2: =#0= minutes;\n".
74  "%%hr:\n".
75  " 1: 1 hour;\n".
76  " 2: =#0= hours;\n".
77  "%%day:\n".
78  " 1: 1 day;\n".
79  " 2: =#0= days;\n".
80  "%%week:\n".
81  " 1: 1 week;\n".
82  " 2: =#0= weeks;"
83  );
84 
85  $formatter = new \NumberFormatter(
86  $locale,
87  \NumberFormatter::PATTERN_RULEBASED,
88  $rule
89  );
90  return (string) $formatter->format($this->value);
91  }
92 
93  public function getValue()
94  {
95  return $this->value;
96  }
97 }
Interface to provide internationalization.
Definition: CLI.php:21
$value
The duration to format (in seconds).
A class used to format durations.
render(\Erebot\IntlInterface $translator)
$translator
Translator to use to improve rendering.
Definition: Styling.php:116