root/trunk/gregarius/cls/l10n.php

Revision 1725, 5.1 kB (checked in by mbonetti, 18 months ago)

new language negotiation method. if rss.ouput.lang.force is set to true,
Gregarius will always serve pages with the language defined in
rss.output.lang, when 'false', it'll negotiate, then fall back to the
language defined in rss.output.lang

Line 
1<?php
2###############################################################################
3# Gregarius - A PHP based RSS aggregator.
4# Copyright (C) 2003 - 2006 Marco Bonetti
5#
6###############################################################################
7# This program is free software and open source software; you can redistribute
8# it and/or modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of the License,
10# or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15# more details.
16#
17# You should have received a copy of the GNU General Public License along
18# with this program; if not, write to the Free Software Foundation, Inc.,
19# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
20# http://www.gnu.org/licenses/gpl.html
21#
22###############################################################################
23# E-mail:      mbonetti at gmail dot com
24# Web page:    http://gregarius.net/
25#
26###############################################################################
27
28require_once(dirname(__FILE__) .'/../extlib/l10n/streams.php');
29require_once(dirname(__FILE__) .'/../extlib/l10n/gettext.php');
30define('RSS_LOCALE_COOKIE','rss_preferred_locale');
31class RSSl10n {
32       
33        var $l10n;
34        var $cache;
35        var $locale;
36        var $isolang;
37       
38        function RSSl10n() {
39                $this -> locale = preg_replace('#[^a-zA-Z_]#','',$this -> __detectUserLang());
40                $this -> isolang=str_replace('_','-',$this -> locale);
41                if (function_exists('version_compare') && version_compare("4.3.0",PHP_VERSION, "<=") && preg_match('#([a-z]{2})_?([A-Z]{2})?#',$this -> locale,$m)) {
42                        $locales=array(
43                                $m[0].'.UTF-8',
44                                $m[0].'.utf-8',
45                                $m[0],
46                                $m[1],                         
47                                $m[1].'_'.strtoupper($m[1])
48                                //$m[2]
49                        );
50                        setlocale(LC_ALL, $locales);   
51                } else {
52                        setlocale(LC_ALL, $this -> locale);
53                }
54       
55                $path = GREGARIUS_HOME .'/intl/' . $this -> locale . '/LC_MESSAGES/messages.mo';
56                $streamer = new FileReader($path);
57                $this -> l10n = new gettext_reader($streamer);
58                $this -> cache = array();
59        }
60       
61        function translate($msg, $cnt = null) {
62                if (isset($this -> cache[$msg . $cnt])) {
63                        return $this -> cache[$msg . $cnt];
64                } 
65                $ret = $this -> l10n -> translate($msg, $cnt);
66                $this -> cache[$msg . $cnt] = $ret;
67                return $ret;
68        }
69       
70        function getLocale() {
71                return $this -> locale;
72        }
73        function getISOLang() {
74                return $this ->isolang;
75        }
76    /**
77     * Detect user's preferred language.
78         * Losely based on http://grep.be/data/accept-to-gettext.inc
79     */
80        function __detectUserLang() {
81               
82                if (getConfig('rss.output.lang.force') === true) {
83                        return getConfig('rss.output.lang');
84                // Language defined in the request (?lang=)
85                }
86               
87                // Language overridden?
88                if (defined('RSS_LANGUAGE_OVERRIDE')) {
89                        return constant('RSS_LANGUAGE_OVERRIDE');
90                }  elseif (isset($_REQUEST['lang']) && preg_match('#^[a-z]{2}_?([A-Z]{2})?$#',$_REQUEST['lang']) && ($_REQUEST['lang'] == 'en' || file_exists(GREGARIUS_HOME .'intl/'.$_REQUEST['lang']))) {
91                        $this -> __setLocaleCookie($_REQUEST['lang']);
92                        rss_invalidate_cache();
93            return  $_REQUEST['lang'];
94                // Cookie
95                } elseif (isset($_COOKIE[RSS_LOCALE_COOKIE])) {
96            return trim($_COOKIE[RSS_LOCALE_COOKIE]);
97                // HTTP_ACCEPT_LANGUAGE HTTP Argument
98       } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
99            $alparts=@preg_split("/,/",$_SERVER['HTTP_ACCEPT_LANGUAGE']);
100            foreach($alparts as $part) {
101                $part=trim($part);
102                if(preg_match("/;/", $part)) {
103                    $lang=@preg_split("/;/",$part);
104                    $ll = $lang[0];
105                } else {
106                    $ll = $part;
107                }
108
109                if (preg_match('#^([a-z]{2})[\-_]?([a-z]{2})?$#i',$ll,$pm)) {
110                        $ret =null;
111                        if (isset($pm[2]) && file_exists(GREGARIUS_HOME .'intl/'.$pm[1] ."_".strtoupper($pm[2]))) {
112                                // xx-yy -> xx_YY
113                                $ret= $pm[1] ."_".strtoupper($pm[2]);
114                        } elseif(file_exists(GREGARIUS_HOME .'intl/'.$pm[1] )) {
115                                // xx  -> xx
116                                $ret= $pm[1];
117                        } elseif($pm[1] == 'en') {
118                                // ugly: a better way would be to look up all the available locales
119                                // and match against that list
120                                $ret='en_US';
121                        }
122                        if ($ret) {
123                                // remember the detected locale for a couple hours
124                                                $this -> __setLocaleCookie($ret);
125                                return $ret;
126                        }
127                }
128               
129            }
130        }
131        // If everything fails, return the user selected language
132        return getConfig('rss.output.lang');
133    }
134
135        function __setLocaleCookie($value) {
136                setcookie(RSS_LOCALE_COOKIE,$value,time()+3600*6,getPath());
137        }
138}
139       
140
141
142function __($msg, $cnt = null) {
143        if (!isset($GLOBALS['rssl10n'])) {
144                $GLOBALS['rssl10n'] = new RSSl10n();
145        }
146        return $GLOBALS['rssl10n'] -> translate($msg, $cnt);
147}
148?>
Note: See TracBrowser for help on using the browser.