source: src/webservice/htdocs/inc/translate.php@ cf345ee

Last change on this file since cf345ee was 7e8f16c, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago

Added php package requirements (as comments) to the translate.php file

  • Property mode set to 100644
File size: 3.0 KB
Line 
1<?php
2######################
3#
4# Copyright (C) 2015 EngSaS - Engineering Soluations and Services Langenbach. All rights reserved.
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19# MA 02110-1301 USA.
20#
21######################
22
23// Require per-http2
24// Require php5-gettext
25
26define("LOCALE_SESSION_KEY", "gyl_locale");
27define("LOCALE_DEFAULT", "en_US");
28define("LOCALE_REQUEST_PARAN", "lang");
29define("LOCALE_DOMAIN", "messages"); // Must be the same name, as the po and mo files and the directory they are in
30
31require_once("HTTP2.php");
32
33$config = new SiWeather\Webservice\WebserviceConfig();
34
35$numberDelimiterDecimal = array();
36$numberDelimiterDecimal["en_US"] = ".";
37$numberDelimiterDecimal["de_DE"] = ",";
38
39$_localeCurrent = LOCALE_DEFAULT;
40if(isset($_SESSION)){
41 if(array_key_exists(LOCALE_REQUEST_PARAN, $_REQUEST)){
42 $_localeCurrent = $_REQUEST[LOCALE_REQUEST_PARAN];
43 $_SESSION[LOCALE_SESSION_KEY] = $_localeCurrent;
44 }
45 elseif(array_key_exists(LOCALE_SESSION_KEY, $_SESSION)){
46 $_localeCurrent = $_SESSION[LOCALE_SESSION_KEY];
47 }
48 else{
49 $supportedLanguages = Array(LOCALE_DEFAULT);
50 $transDirHandle = opendir($config->getConfig("translationdirectory"));
51 if($transDirHandle){
52 while(false !== ($entry = readdir($transDirHandle))){
53 if($entry != "." && $entry != ".."){
54 $supportedLanguages[str_replace("_", "-", $entry)] = true;
55 }
56 }
57
58 closedir($transDirHandle);
59 }
60 $http = new HTTP2();
61 $_localeCurrent = str_replace("-", "_", $http->negotiateLanguage($supportedLanguages, LOCALE_DEFAULT));
62 $_SESSION[LOCALE_SESSION_KEY] = $_localeCurrent;
63 }
64}
65
66$results = putenv("LC_ALL0=". $_localeCurrent);
67if(!$results){
68 error_log("putenv failed (". $result .")");
69}
70$result = setlocale(LC_ALL, $_localeCurrent);
71if(!$results){
72 error_log("setlocale failed (". $result .")");
73}
74$results = bindtextdomain(LOCALE_DOMAIN, $config->getConfig("translationdirectory"));
75if(!$results){
76 error_log("bindtextdomain failed (". $result .")");
77}
78$results = bind_textdomain_codeset(LOCALE_DOMAIN, "UTF-8");
79if(!$results){
80 error_log("bind_textdomain_codeset failed (". $result .")");
81}
82$results = textdomain(LOCALE_DOMAIN);
83if(!$results){
84 error_log("textdomain failed (". $result .")");
85}
86
87class Translator {
88 public static function swGettext($id)
89 {
90 return vsprintf(gettext($id), array_slice(func_get_args(), 1));
91 }
92}
Note: See TracBrowser for help on using the repository browser.