summaryrefslogtreecommitdiff
path: root/src/Helpers/Translator.php
blob: 94fbd79543582e9582cb791ac1e7890e0124141f (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace Engelsystem\Helpers;

class Translator
{
    /** @var string[] */
    protected $locales;

    /** @var string */
    protected $locale;

    /** @var callable */
    protected $localeChangeCallback;

    /**
     * Translator constructor.
     *
     * @param string   $locale
     * @param string[] $locales
     * @param callable $localeChangeCallback
     */
    public function __construct(string $locale, array $locales = [], callable $localeChangeCallback = null)
    {
        $this->localeChangeCallback = $localeChangeCallback;

        $this->setLocale($locale);
        $this->setLocales($locales);
    }

    /**
     * Get the translation for a given key
     *
     * @param string $key
     * @param array  $replace
     * @return string
     */
    public function translate(string $key, array $replace = []): string
    {
        $translated = $this->translateGettext($key);

        return $this->replaceText($translated, $replace);
    }

    /**
     * Get the translation for a given key
     *
     * @param string $key
     * @param string $pluralKey
     * @param int    $number
     * @param array  $replace
     * @return string
     */
    public function translatePlural(string $key, string $pluralKey, int $number, array $replace = []): string
    {
        $translated = $this->translateGettextPlural($key, $pluralKey, $number);

        return $this->replaceText($translated, $replace);
    }

    /**
     * Replace placeholders
     *
     * @param string $key
     * @param array  $replace
     * @return mixed|string
     */
    protected function replaceText(string $key, array $replace = [])
    {
        if (empty($replace)) {
            return $key;
        }

        return call_user_func_array('sprintf', array_merge([$key], $replace));
    }

    /**
     * Translate the key via gettext
     *
     * @param string $key
     * @return string
     * @codeCoverageIgnore
     */
    protected function translateGettext(string $key): string
    {
        return gettext($key);
    }

    /**
     * Translate the key via gettext
     *
     * @param string $key
     * @param string $keyPlural
     * @param int    $number
     * @return string
     * @codeCoverageIgnore
     */
    protected function translateGettextPlural(string $key, string $keyPlural, int $number): string
    {
        return ngettext($key, $keyPlural, $number);
    }

    /**
     * @return string
     */
    public function getLocale(): string
    {
        return $this->locale;
    }

    /**
     * @param string $locale
     */
    public function setLocale(string $locale)
    {
        $this->locale = $locale;

        if (is_callable($this->localeChangeCallback)) {
            call_user_func_array($this->localeChangeCallback, [$locale]);
        }
    }

    /**
     * @return string[]
     */
    public function getLocales(): array
    {
        return $this->locales;
    }

    /**
     * @param string $locale
     * @return bool
     */
    public function hasLocale(string $locale): bool
    {
        return isset($this->locales[$locale]);
    }

    /**
     * @param string[] $locales
     */
    public function setLocales(array $locales)
    {
        $this->locales = $locales;
    }
}