source: src/webservice/htdocs/inc/translate.php@ 851179e

Last change on this file since 851179e was 8c356c1, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Finished setup
    • Added tests for setup
    • Added translation to setup
  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[8c356c1]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
23define("LOCALE_SESSION_KEY", "gyl_locale");
24define("LOCALE_DEFAULT", "en_US");
25define("LOCALE_REQUEST_PARAN", "lang");
26define("LOCALE_DOMAIN", "messages"); // Must be the same name, as the po and mo files and the directory they are in
27
28require_once("HTTP2.php");
29
30$config = new SiWeather\Webservice\WebserviceConfig();
31
32$numberDelimiterDecimal = array();
33$numberDelimiterDecimal["en_US"] = ".";
34$numberDelimiterDecimal["de_DE"] = ",";
35
36$_localeCurrent = LOCALE_DEFAULT;
37if(isset($_SESSION)){
38 if(array_key_exists(LOCALE_REQUEST_PARAN, $_REQUEST)){
39 $_localeCurrent = $_REQUEST[LOCALE_REQUEST_PARAN];
40 $_SESSION[LOCALE_SESSION_KEY] = $_localeCurrent;
41 }
42 elseif(array_key_exists(LOCALE_SESSION_KEY, $_SESSION)){
43 $_localeCurrent = $_SESSION[LOCALE_SESSION_KEY];
44 }
45 else{
46 $supportedLanguages = Array(LOCALE_DEFAULT);
47 $transDirHandle = opendir($config->getConfig("translationdirectory"));
48 if($transDirHandle){
49 while(false !== ($entry = readdir($transDirHandle))){
50 if($entry != "." && $entry != ".."){
51 $supportedLanguages[str_replace("_", "-", $entry)] = true;
52 }
53 }
54
55 closedir($transDirHandle);
56 }
57 $http = new HTTP2();
58 $_localeCurrent = str_replace("-", "_", $http->negotiateLanguage($supportedLanguages, LOCALE_DEFAULT));
59 $_SESSION[LOCALE_SESSION_KEY] = $_localeCurrent;
60 }
61}
62
63$results = putenv("LC_ALL0=". $_localeCurrent);
64if(!$results){
65 error_log("putenv failed (". $result .")");
66}
67$result = setlocale(LC_ALL, $_localeCurrent);
68if(!$results){
69 error_log("setlocale failed (". $result .")");
70}
71$results = bindtextdomain(LOCALE_DOMAIN, $config->getConfig("translationdirectory"));
72if(!$results){
73 error_log("bindtextdomain failed (". $result .")");
74}
75$results = bind_textdomain_codeset(LOCALE_DOMAIN, "UTF-8");
76if(!$results){
77 error_log("bind_textdomain_codeset failed (". $result .")");
78}
79$results = textdomain(LOCALE_DOMAIN);
80if(!$results){
81 error_log("textdomain failed (". $result .")");
82}
83
84class Translator {
85 public static function swGettext($id)
86 {
87 return vsprintf(gettext($id), array_slice(func_get_args(), 1));
88 }
89}
Note: See TracBrowser for help on using the repository browser.