1 | <?php |
||
23 | class FileTranslator implements Translator |
||
24 | { |
||
25 | /** |
||
26 | * @var array The translations |
||
27 | */ |
||
28 | private $texts; |
||
29 | |||
30 | /** |
||
31 | * Creates instance |
||
32 | * |
||
33 | * @param string $translationDirectory The directory containing the translation files |
||
34 | * @param string $languageCode The language code of which to get the translations |
||
35 | * |
||
36 | * @throws \Exception When the language is not supported (i.e. no translation file can be found for the language) |
||
37 | * @throws \Exception When the translation file is invalid (i.e. no `$texts` array present) |
||
38 | */ |
||
39 | 5 | public function __construct($translationDirectory, $languageCode) |
|
40 | { |
||
41 | 5 | $translationFile = $translationDirectory . '/' . $languageCode . '.php'; |
|
42 | |||
43 | 5 | if (!file_exists($translationFile)) { |
|
44 | 1 | throw new \Exception('Unsupported language (`' . $languageCode . '`).'); |
|
45 | } |
||
46 | |||
47 | 4 | require $translationFile; |
|
48 | |||
49 | 4 | if (!isset($texts)) { |
|
50 | 1 | throw new \Exception( |
|
51 | 1 | 'The translation file (`' . $translationFile . '`) has an invalid format.' |
|
52 | ); |
||
53 | } |
||
54 | |||
55 | 3 | $this->texts = $texts; |
|
56 | 3 | } |
|
57 | |||
58 | /** |
||
59 | * Gets the translation by key if any or a placeholder otherwise |
||
60 | * |
||
61 | * @param string $key The translation key for which to find the translation |
||
62 | * |
||
63 | * @return string The translation or a placeholder when no translation is available |
||
64 | */ |
||
65 | 2 | public function translate($key) |
|
73 | } |
||
74 |