~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Helper/Translate.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Xibo - Digital Signage - http://www.xibo.org.uk
4
 
 * Copyright (C) 2015 Spring Signage Ltd
5
 
 *
6
 
 * This file (TranslationEngine.php) is part of Xibo.
7
 
 *
8
 
 * Xibo is free software: you can redistribute it and/or modify
9
 
 * it under the terms of the GNU Affero General Public License as published by
10
 
 * the Free Software Foundation, either version 3 of the License, or
11
 
 * any later version.
12
 
 *
13
 
 * Xibo is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
 * GNU Affero General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Affero General Public License
19
 
 * along with Xibo.  If not, see <http://www.gnu.org/licenses/>.
20
 
 */
21
 
namespace Xibo\Helper;
22
 
 
23
 
use CachedFileReader;
24
 
use Gettext\Translations;
25
 
use Gettext\Translator;
26
 
use gettext_reader;
27
 
use Xibo\Service\ConfigServiceInterface;
28
 
 
29
 
/**
30
 
 * Class Translate
31
 
 * @package Xibo\Helper
32
 
 */
33
 
class Translate
34
 
{
35
 
    private static $requestedLanguage;
36
 
    private static $locale;
37
 
    private static $jsLocale;
38
 
 
39
 
    /**
40
 
     * Gets and Sets the Locale
41
 
     * @param ConfigServiceInterface $config
42
 
     * @param $language string[optional] The Language to Load
43
 
     */
44
 
    public static function InitLocale($config, $language = NULL)
45
 
    {
46
 
        $localeDir = PROJECT_ROOT . '/locale';
47
 
        $default = ($language == NULL) ? $config->GetSetting('DEFAULT_LANGUAGE') : $language;
48
 
 
49
 
        // Build an array of supported languages
50
 
        $supportedLanguages = array_map('basename', glob($localeDir . '/*.mo'));
51
 
 
52
 
        // Try to get the local firstly from _REQUEST (post then get)
53
 
        $requestedLanguage = $language;
54
 
        $foundLanguage = '';
55
 
 
56
 
        if ($requestedLanguage != '') {
57
 
            // Serve only the requested language
58
 
            // Firstly, Sanitize it
59
 
            $requestedLanguage = str_replace('-', '_', $requestedLanguage);
60
 
 
61
 
            // Check its valid
62
 
            if (in_array($requestedLanguage . '.mo', $supportedLanguages)) {
63
 
                $foundLanguage = $requestedLanguage;
64
 
            }
65
 
        }
66
 
        else if ($config->GetSetting('DETECT_LANGUAGE') == 1) {
67
 
            // Detect the language, try from HTTP accept
68
 
            // Parse the language header and build a preference array
69
 
            $languagePreferenceArray = Translate::parseHttpAcceptLanguageHeader();
70
 
 
71
 
            if (count($languagePreferenceArray) > 0) {
72
 
 
73
 
                // Go through the list until we have a match
74
 
                foreach ($languagePreferenceArray as $languagePreference => $preferenceRating) {
75
 
 
76
 
                    // We don't ship an en.mo, so fudge in a case where we automatically convert that to en_GB
77
 
                    if ($languagePreference == 'en')
78
 
                        $languagePreference = 'en_GB';
79
 
 
80
 
                    // Sanitize
81
 
                    $languagePreference = str_replace('-', '_', $languagePreference);
82
 
 
83
 
                    // Check it is valid
84
 
                    if (in_array($languagePreference . '.mo', $supportedLanguages)) {
85
 
                        $foundLanguage = $languagePreference;
86
 
                        break;
87
 
                    }
88
 
                }
89
 
            }
90
 
        }
91
 
 
92
 
        // Requested language
93
 
        self::$requestedLanguage = ($foundLanguage == '') ? $default : $foundLanguage;
94
 
 
95
 
        // Are we still empty, then default language from settings
96
 
        if ($foundLanguage == '') {
97
 
            // Check the default
98
 
            if (!in_array($default . '.mo', $supportedLanguages)) {
99
 
                $default = 'en_GB';
100
 
            }
101
 
 
102
 
            // The default is valid
103
 
            $foundLanguage = $default;
104
 
        }
105
 
 
106
 
        // Load translations
107
 
        $translator = new Translator();
108
 
        $translator->loadTranslations(Translations::fromMoFile($localeDir . '/' . $foundLanguage . '.mo'));
109
 
        $translator->register();
110
 
 
111
 
        // Store our resolved language locales
112
 
        self::$locale = $foundLanguage;
113
 
        self::$jsLocale = str_replace('_', '-', $foundLanguage);
114
 
    }
115
 
 
116
 
    /**
117
 
     * Get the Locale
118
 
     * @param null $characters The number of characters to take from the beginning of the local string
119
 
     * @return mixed
120
 
     */
121
 
    public static function GetLocale($characters = null)
122
 
    {
123
 
        return ($characters == null) ? self::$locale : substr(self::$locale, 0, $characters);
124
 
    }
125
 
 
126
 
    public static function GetJsLocale()
127
 
    {
128
 
        return self::$jsLocale;
129
 
    }
130
 
 
131
 
    public static function getRequestedLanguage()
132
 
    {
133
 
        return self::$requestedLanguage;
134
 
    }
135
 
 
136
 
    /**
137
 
     * Parse the HttpAcceptLanguage Header
138
 
     * Inspired by: http://www.thefutureoftheweb.com/blog/use-accept-language-header
139
 
     * @param null $header
140
 
     * @return array Language array where the key is the language identifier and the value is the preference double.
141
 
     */
142
 
    public static function parseHttpAcceptLanguageHeader($header = null)
143
 
    {
144
 
        if ($header == null)
145
 
            $header = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
146
 
 
147
 
        $languages = array();
148
 
 
149
 
        if ($header != '') {
150
 
            // break up string into pieces (languages and q factors)
151
 
            preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $header, $langParse);
152
 
 
153
 
            if (count($langParse[1])) {
154
 
                // create a list like "en" => 0.8
155
 
                $languages = array_combine($langParse[1], $langParse[4]);
156
 
 
157
 
                // set default to 1 for any without q factor
158
 
                foreach ($languages as $lang => $val) {
159
 
                    if ($val === '')
160
 
                        $languages[$lang] = 1;
161
 
                }
162
 
 
163
 
                // sort list based on value
164
 
                arsort($languages, SORT_NUMERIC);
165
 
            }
166
 
        }
167
 
 
168
 
        return $languages;
169
 
    }
170
 
}
 
 
b'\\ No newline at end of file'