1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: I18n.php 711 2013-07-04 12:05:36Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\I18n class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * Class for Scrivo internationalization (i18n) resources. 40 * 41 * This is a class to create a set of i18n key value pairs. By default the 42 * created instance will use Scrivo's own set of i18n resources, but it is 43 * possible to use your own set(s). 44 * 45 * An i18n resource file is a PHP script that declares a single array 46 * variabele ($I18N_TEXT) that contains the i18n key value pairs, where the 47 * keys are the english texts and the values contain the translations, f.i. 48 * the file "nl_NL.php" might contain the following: 49 * 50 * <?php $I18N_TEXT = array("Cancel" => "Annuleer"); ?> 51 * 52 * The file name needs to be named after the language set it contains: 53 * "nl_NL.php" contains the Dutch i18n resources. It is of course strongly 54 * advised to use the UTF-8 encoding for your internationalization files. 55 * 56 * Because we're dealing with hard coded data and it will be excessively 57 * cumbersome to deal I18N keys if you'll have to use the \Scrivo\String wrappers, 58 * you can use plain strings as i18n keys. 59 * 60 * Typical usage: 61 * 62 * $i18n = new \Scrivo\I18n(new \Scrivo\String("nl_NL")); 63 * echo $i18n["Cancel"]; 64 */ 65 class I18n implements \ArrayAccess { 66 67 /** 68 * The i18n key value pairs, where the keys are the English texts and 69 * the values contain the translations. 70 * 71 * @var array 72 */ 73 private $data; 74 75 /** 76 * Create a set of internationalization key/value pairs for a given 77 * language. 78 * 79 * @param \Scrivo\String $langCode The language code for which to create the 80 * set of resources for. 81 * @param \Scrivo\String $dir An optional directory for an alternative 82 * location for the i18n resource file. 83 * 84 * @throws \Exception If no 1i8n resource file was found. 85 */ 86 function __construct(\Scrivo\String $langCode, \Scrivo\String $dir=null) { 87 if (!$dir) { 88 // The location of Scrivo UI language keys. 89 $dir = dirname(__FILE__)."/I18n"; 90 } 91 // Sanitize the file name for security reasons. 92 $file = $dir."/".preg_replace("/[^_a-zA-Z]/u", "", $langCode).".php"; 93 if (file_exists($file)) { 94 require $file; 95 $this->data = $I18N_TEXT; 96 } else { 97 // TODO: only when in debug mode 98 //throw new \Exception("I18n recourse file '$file' was not found."); 99 $this->data = array(); 100 } 101 } 102 103 /** 104 * Get an i18n entry from a set using array brackets. 105 * 106 * Note that this method is part of the implementation of ArrayAccess and 107 * should not be called from another context. 108 * 109 * @param string $i18nKey A character offet in the string. 110 * 111 * @return string The translation of the key, or the key if the key 112 * was not found. 113 */ 114 public function offsetGet($i18nKey) { 115 return isset($this->data[$i18nKey]) ? $this->data[$i18nKey] : $i18nKey; 116 } 117 118 /** 119 * Illegal method: set a character at a specified index location. 120 * 121 * Note that this method is part of the implementation of ArrayAccess. 122 * I18n sets are inmutable so this method implementation is not relevant 123 * and throws an exception if called. 124 * 125 * @param int $offset 126 * @param string $value 127 * 128 * @throws \Scrivo\SystemException If this method is called. 129 */ 130 public function offsetSet($offset, $value) { 131 throw new \Scrivo\SystemException( 132 "offsetSet can't be called on I18n objects"); 133 } 134 135 /** 136 * Check if the specified index location in this string is valid. 137 * 138 * Note that this method is part of the implementation of ArrayAccess. 139 * It is assumed that i18n keys are always set: if not it is pretended 140 * it's set and key itself is returned. So this method implementation is 141 * not relevant and throws an exception if called. 142 * 143 * @param int $offset 144 * 145 * @throws \Scrivo\SystemException If this method is called. 146 */ 147 public function offsetExists($offset) { 148 throw new \Scrivo\SystemException( 149 "offsetExists can't be called on I18n objects"); 150 } 151 152 /** 153 * Illegal method: unset a character at a specified index location. 154 * 155 * Note that this method is part of the implementation of ArrayAccess. 156 * I18n sets are inmutable so this method implementation is not relevant 157 * and throws an exception if called. 158 * 159 * @param int $offset 160 * 161 * @throws \Scrivo\SystemException If this method is called. 162 */ 163 public function offsetUnset($offset) { 164 throw new \Scrivo\SystemException( 165 "offsetUnset can't be called on I18n objects"); 166 } 167 168 } 169
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013