Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Translation
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
7 / 7
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 allLocales
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasMultiLocales
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 locale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setLocale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3namespace Dynart\Micro;
4
5class Translation implements TranslationInterface {
6
7    /** The configuration name of all the known translation */
8    const CONFIG_ALL = 'translation.all';
9
10    /** The configuration name of the default translation */
11    const CONFIG_DEFAULT = 'translation.default';
12
13    /** The default locale */
14    const DEFAULT_LOCALE = 'en';
15
16    /** The folders for all the translations in [namespace => path] format */
17    protected array $folders = [];
18
19    /** The loaded translations in [namespace => [id => text]] format */
20    protected array $data = [];
21
22    /** All the known translations */
23    protected array $allLocales = [];
24
25    /** Has a multi locale config? */
26    protected bool $hasMultiLocales = false;
27
28    /** The current locale */
29    protected string $locale = 'en';
30
31    protected ConfigInterface $config;
32
33    /**
34     * Sets the `$locale`, the `$allLocales` and `$hasMultiLocales` members via the `$config`
35     */
36    public function __construct(ConfigInterface $config) {
37        $this->config = $config;
38        $this->locale = $config->get(self::CONFIG_DEFAULT, self::DEFAULT_LOCALE);
39        $this->allLocales = $config->getCommaSeparatedValues(self::CONFIG_ALL);
40        $this->hasMultiLocales = count($this->allLocales) > 1;
41    }
42
43    public function add(string $namespace, string $folder): void {
44        $this->data[$namespace] = null;
45        $this->folders[$namespace] = $folder;
46    }
47
48    public function allLocales(): array {
49        return $this->allLocales;
50    }
51
52    public function hasMultiLocales(): bool {
53        return $this->hasMultiLocales;
54    }
55
56    public function locale(): string {
57        return $this->locale;
58    }
59
60    public function setLocale(string $locale): void {
61        $this->locale = $locale;
62    }
63
64    public function get(string $id, array $params = []): string {
65        $dotPos = strpos($id, ':');
66        $namespace = substr($id, 0, $dotPos);
67        $name = substr($id, $dotPos + 1);
68        $result = '#'.$id.'#';
69        if (!isset($this->folders[$namespace])) {
70            return $result;
71        }
72        if (!isset($this->data[$namespace])) {
73            $path = $this->config->getFullPath($this->folders[$namespace].'/'.$this->locale.'.ini');
74            $iniData = file_exists($path) ? parse_ini_file($path) : [];
75            $this->data[$namespace] = $iniData;
76        }
77        if (isset($this->data[$namespace][$name])) {
78            $result = $this->data[$namespace][$name];
79        }
80        foreach ($params as $name => $value) {
81            $result = str_replace('{' . $name . '}', $value, $result);
82        }
83        return $result;
84    }
85
86}