source: extensions/AMetaData/JpegMetaData/Common/L10n.class.php @ 7569

Last change on this file since 7569 was 5232, checked in by grum, 15 years ago

fix bug : character encoding was wrong if there was no key to translate

  • Property svn:executable set to *
File size: 5.7 KB
Line 
1<?php
2/*
3 * --:: JPEG MetaDatas ::-------------------------------------------------------
4 *
5 *  Author    : Grum
6 *   email    : grum at piwigo.org
7 *   website  : http://photos.grum.fr
8 *
9 *   << May the Little SpaceFrog be with you ! >>
10 *
11 *
12 * +-----------------------------------------------------------------------+
13 * | JpegMetaData - a PHP based Jpeg Metadata manager                      |
14 * +-----------------------------------------------------------------------+
15 * | Copyright(C) 2010  Grum - http://www.grum.fr                          |
16 * +-----------------------------------------------------------------------+
17 * | This program is free software; you can redistribute it and/or modify  |
18 * | it under the terms of the GNU General Public License as published by  |
19 * | the Free Software Foundation                                          |
20 * |                                                                       |
21 * | This program is distributed in the hope that it will be useful, but   |
22 * | WITHOUT ANY WARRANTY; without even the implied warranty of            |
23 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
24 * | General Public License for more details.                              |
25 * |                                                                       |
26 * | You should have received a copy of the GNU General Public License     |
27 * | along with this program; if not, write to the Free Software           |
28 * | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
29 * | USA.                                                                  |
30 * +-----------------------------------------------------------------------+
31 *
32 *
33 * -----------------------------------------------------------------------------
34 *
35 * The L10n class is used for tag translation, reading the .mo files
36 *
37 * There is one directory per language, the directories tree must respect this
38 * structure
39 *
40 * --\ Locale                           => main directory for languages
41 *     |
42 *     +--\ en_UK                       => language
43 *     |    |
44 *     |    +--\ LC_MESSAGES
45 *     |         |
46 *     |         +--\ Tag.mo            => names & values translations
47 *     |         +--\ TadDesc.mo        => descriptions translations
48 *     |
49 *     +--\ fr_FR
50 *          |
51 *          +--\ LC_MESSAGES
52 *               |
53 *               +--\ Tag.mo
54 *               +--\ TadDesc.mo
55 *
56 *
57 * -----------------------------------------------------------------------------
58 *
59 * .. Notes ..
60 *
61 * This class don't use the PHP gettext functions, but the php-gettext extension
62 * ==> See files in External/php-gettext for more information about this project
63 *
64 *
65 *
66 * This class provides theses public functions :
67 *  - (static) setLanguage
68 *  - (static) getLanguage
69 *  - (static) get
70 *  - (static) getDesc
71 *
72 * -----------------------------------------------------------------------------
73 */
74
75
76require_once(JPEG_METADATA_DIR."External/php-gettext/gettext.inc");
77
78/**
79 * $supported_locales is a global variable need by the php-gettext package.
80 * the array is automatically initialized with the setLanguage() function
81 */
82$supported_locales = array();
83
84/**
85 * assume a default language is set in any case...
86 */
87L10n::setLanguage();
88
89
90class L10n
91{
92  const JMD_TAG = "Tag";
93  const JMD_TAGDESC = "TagDesc";
94
95  static private $language = "";
96
97  /**
98   * This function is used to set the locale language.
99   * If no language is given, assume the default "en_UK" language
100   *
101   * If there is no translation file for the the given language, assume the
102   * default "en_UK" language
103   *
104   * @param String $language : the language
105   * @param String $charset  : charset encoding (UTF-8)
106   * @return String : the defined language
107   */
108  static function setLanguage($language="en_UK", $charset="UTF-8")
109  {
110    global $supported_locales;
111
112    /*
113     * Scan the Locale directory.
114     * Any directory with a sub directory "LC_MESSAGES" is considered as a
115     * supported locale language
116     */
117    $directory=scandir(JPEG_METADATA_DIR."Locale");
118    foreach($directory as $key => $file)
119    {
120      if(is_dir(JPEG_METADATA_DIR."Locale/".$file) and
121         $file!='.' and
122         $file!='..' and
123         file_exists(JPEG_METADATA_DIR."Locale/".$file."/LC_MESSAGES/Tag.mo"))
124         $supported_locales[]=$file;
125
126    }
127
128    /*
129     * if the desired language doesn't exist, apply the en_UK locale
130     * (assuming the en_UK exists and was not deleted)
131     */
132    if(!in_array($language, $supported_locales))
133      self::$language='en_UK';
134    else
135      self::$language=$language;
136
137    /*
138     * set the locale
139     */
140    T_setlocale(LC_MESSAGES, self::$language);
141
142    /*
143     * set one domain "TAG" for tags name&values, and one domaine "TAGDESC" for
144     * tags description
145     */
146    T_bindtextdomain(self::JMD_TAG, dirname(dirname(__FILE__))."/Locale");
147    T_bindtextdomain(self::JMD_TAGDESC, dirname(dirname(__FILE__))."/Locale");
148
149    /*
150     * set the charsets
151     */
152    if($charset!="")
153    {
154      T_bind_textdomain_codeset(self::JMD_TAG, $charset);
155      T_bind_textdomain_codeset(self::JMD_TAGDESC, $charset);
156    }
157
158    return(self::$language);
159  }
160
161  /**
162   * returns the defined current language
163   *
164   * @return String
165   */
166  static function getLanguage()
167  {
168    return(self::$language);
169  }
170
171  /**
172   * returns the translation in current language for the given key
173   *
174   * @return String
175   */
176  static function get($key)
177  {
178    T_textdomain(self::JMD_TAG);
179    return(@T_dgettext(self::JMD_TAG, $key));
180  }
181
182  /**
183   * returns the description in current language for the given tagName
184   *
185   * @return String
186   */
187  static function getDesc($tagName)
188  {
189    T_textdomain(self::JMD_TAGDESC);
190    return(@T_dgettext(self::JMD_TAGDESC, $tagName));
191  }
192
193}
194
195
196?>
Note: See TracBrowser for help on using the repository browser.