summaryrefslogtreecommitdiff
path: root/src/Helpers/Translation/GettextTranslator.php
blob: 7f2299e2d6e34b9f42e88e0333efb361ac5674bd (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
<?php

namespace Engelsystem\Helpers\Translation;

use Gettext\Translator;

class GettextTranslator extends Translator
{
    /**
     * @param string $domain
     * @param string $context
     * @param string $original
     * @return string
     * @throws TranslationNotFound
     */
    public function dpgettext($domain, $context, $original)
    {
        $this->assertHasTranslation($domain, $context, $original);

        return parent::dpgettext($domain, $context, $original);
    }

    /**
     * @param string $domain
     * @param string $context
     * @param string $original
     * @param string $plural
     * @param string $value
     * @return string
     * @throws TranslationNotFound
     */
    public function dnpgettext($domain, $context, $original, $plural, $value)
    {
        $this->assertHasTranslation($domain, $context, $original);

        return parent::dnpgettext($domain, $context, $original, $plural, $value);
    }

    /**
     * @param string $domain
     * @param string $context
     * @param string $original
     * @throws TranslationNotFound
     */
    protected function assertHasTranslation($domain, $context, $original)
    {
        if ($this->getTranslation($domain, $context, $original)) {
            return;
        }

        throw new TranslationNotFound(implode('/', [$domain, $context, $original]));
    }
}