Nico Huber | ee52fbc | 2023-06-24 11:52:57 +0000 | [diff] [blame^] | 1 | <?php |
| 2 | /* |
| 3 | * SimpleID |
| 4 | * |
| 5 | * Copyright (C) Kelvin Mo 2012 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public |
| 18 | * License along with this program; if not, write to the Free |
| 19 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * $Id$ |
| 22 | */ |
| 23 | /** |
| 24 | * Localisation support. |
| 25 | * |
| 26 | * @package simpleid |
| 27 | * @since 0.9 |
| 28 | * @filesource |
| 29 | */ |
| 30 | |
| 31 | include_once 'lib/gettext/gettext.inc.php'; |
| 32 | |
| 33 | /** |
| 34 | * Initialises the localisation system. |
| 35 | * |
| 36 | * @param string $locale the locale to use |
| 37 | */ |
| 38 | function locale_init($locale) { |
| 39 | T_setlocale(LC_MESSAGES, $locale); |
| 40 | // Set the text domain as 'messages' |
| 41 | $domain = 'messages'; |
| 42 | bindtextdomain($domain, 'locale'); |
| 43 | // bind_textdomain_codeset is supported only in PHP 4.2.0+ |
| 44 | if (function_exists('bind_textdomain_codeset')) |
| 45 | bind_textdomain_codeset($domain, 'UTF-8'); |
| 46 | textdomain($domain); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Translates a string. |
| 51 | * |
| 52 | * @param string $string the string to translate |
| 53 | * @param array $variables an array of replacements variables to be made after |
| 54 | * a translation. Prefix the variable with a @ to make the replacement HTML safe, |
| 55 | * a % to make the replacement HTML safe and surround with <strong> tags, |
| 56 | * and ! to replace as is |
| 57 | * @return string the translated string |
| 58 | */ |
| 59 | function t($string, $variables = array()) { |
| 60 | $translated = gettext($string); |
| 61 | |
| 62 | foreach ($variables as $variable => $value) { |
| 63 | switch ($variable[0]) { |
| 64 | case '@': |
| 65 | $variables[$variable] = htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
| 66 | break; |
| 67 | case '%': |
| 68 | default: |
| 69 | $variables[$variable] = '<strong>' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '</strong>'; |
| 70 | break; |
| 71 | case '!': |
| 72 | // Pass-through. |
| 73 | } |
| 74 | } |
| 75 | return strtr($translated, $variables); |
| 76 | } |
| 77 | ?> |