msgFunctions.php ➔ iMsg()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 3
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
1
<?php
2
use Mouf\MoufManager;
3
use Mouf\Utils\I18n\Fine\Language\LanguageDetectionInterface;
4
5
/*
6
 * Copyright (c) 2012-2015 Marc TEYSSIER
7
 *
8
 * See the file LICENSE.txt for copying permission.
9
 */
10
11
/*
12
 * Avoid function re-declaration
13
 */
14
if (!function_exists("iMsg")) {
15
16
    /**
17
     * This function return the translation of a code or a sentence for a language.
18
     * The language is selected in the translation service. It's possible to force it for a specific translation with the parameter languageDetectionInterface.
19
     * The translation is searched in the translation instance. This class must by implements the TranslationInterface. The getTranslation method return the translation for a key for a language or null.
20
     *
21
     *
22
     * @param $key string Code, sentence or key for the translation
23
     * @param $parameters array The index is the value set between {} in message and the value is the value
24
     * @param $languageDetectionInterface LanguageDetectionInterface This value is not mandatory. By default the language set in translationService is used. But it is possible to force it ofr a translation.
25
     * @return string Return the translation
26
     */
27
    function iMsg($key, array $parameters = array(), LanguageDetectionInterface $languageDetectionInterface = null)
28
    {
29
        if (!$languageDetectionInterface) {
30
            static $translationService = null;
31
            if ($translationService === null) {
32
                /* @var $translationService TranslationInterface */
33
                $translationService = MoufManager::getMoufManager()->getInstance("defaultTranslationService");
34
            }
35
        } else {
36
            $translationService = $languageDetectionInterface;
37
        }
38
39
        $translation = $translationService->getTranslation($key, $parameters, $languageDetectionInterface);
40
        if ($translation === null) {
41
            return '???'.$key.'???';
42
        }
43
44
        return $translation;
45
    }
46
47
    /**
48
     * Do an echo of iMsg return
49
     *
50
     * @param $key string Code, sentence or key for the translation
51
     * @param $parameters array The index is the value set between {} in message and the value is the value
52
     * @param $languageDetectionInterface LanguageDetectionInterface This value is not mandatory. By default the language set in translationService is used. But it is possible to force it ofr a translation.
53
     */
54
    function eMsg($key, array $parameters = array(), LanguageDetectionInterface $languageDetectionInterface = null)
55
    {
56
        echo iMsg($key, $parameters, $languageDetectionInterface);
57
    }
58
}
59