summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/i18n/src/Translation.php
blob: 52639daec172adc6385cfe5a181d9706b87cf4f5 (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
<?php

namespace ipl\I18n;

trait Translation
{
    /**
     * The domain to use in methods {@see Translation::translate()} and {@see Translation::translatePlural()}
     *
     * Set this to your desired domain and use both mentioned methods as usual, if you never require the
     * default translation domain. (It's still being used as a fallback if your domain doesn't provide a
     * particular message.)
     *
     * @var string
     */
    protected $translationDomain;

    /**
     * Translate a message
     *
     * @param string $message
     * @param string $context Message context
     *
     * @return string Translated message or original message if no translation is found
     */
    public function translate($message, $context = null)
    {
        return $this->translationDomain === null
            ? StaticTranslator::$instance->translate($message, $context)
            : StaticTranslator::$instance->translateInDomain($this->translationDomain, $message, $context);
    }

    /**
     * Translate a message in the given domain
     *
     * If no translation is found in the specified domain, the translation is also searched for in the default domain.
     *
     * @param string $domain
     * @param string $message
     * @param string $context Message context
     *
     * @return string Translated message or original message if no translation is found
     */
    public function translateInDomain($domain, $message, $context = null)
    {
        return StaticTranslator::$instance->translateInDomain($domain, $message, $context);
    }

    /**
     * Translate a plural message
     *
     * The returned message is based on the given number to decide between the singular and plural forms.
     * That is also the case if no translation is found.
     *
     * @param string $singular Singular message
     * @param string $plural   Plural message
     * @param int    $number   Number to decide between the returned singular and plural forms
     * @param string $context  Message context
     *
     * @return string Translated message or original message if no translation is found
     */
    public function translatePlural($singular, $plural, $number, $context = null)
    {
        return $this->translationDomain === null
            ? StaticTranslator::$instance->translatePlural($singular, $plural, $number, $context)
            : StaticTranslator::$instance->translatePluralInDomain(
                $this->translationDomain,
                $singular,
                $plural,
                $number ?? 0,
                $context
            );
    }

    /**
     * Translate a plural message in the given domain
     *
     * If no translation is found in the specified domain, the translation is also searched for in the default domain.
     *
     * The returned message is based on the given number to decide between the singular and plural forms.
     * That is also the case if no translation is found.
     *
     * @param string $domain
     * @param string $singular Singular message
     * @param string $plural   Plural message
     * @param int    $number   Number to decide between the returned singular and plural forms
     * @param string $context  Message context
     *
     * @return string Translated message or original message if no translation is found
     */
    public function translatePluralInDomain($domain, $singular, $plural, $number, $context = null)
    {
        return StaticTranslator::$instance->translatePluralInDomain(
            $domain,
            $singular,
            $plural,
            $number ?? 0,
            $context
        );
    }
}