[255] | 1 | <?php |
---|
[261] | 2 | /** |
---|
| 3 | * t29v6 Message subsystem. |
---|
[289] | 4 | * now with instanciated message overwriting. |
---|
[261] | 5 | * |
---|
| 6 | **/ |
---|
[255] | 7 | |
---|
[261] | 8 | // You can run this script to get all messages as JSON. |
---|
| 9 | // See also /shared/js/modules/msg.js and js.php. |
---|
[255] | 10 | if(realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) { |
---|
| 11 | header('Content-Type: application/json'); |
---|
| 12 | if(isset($_GET['pre'])) echo $_GET['pre']; |
---|
| 13 | echo t29Messages::create_json(); |
---|
| 14 | if(isset($_GET['post'])) echo ";\n".$_GET['post']; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | class t29Messages { |
---|
[261] | 18 | /// when instanciated, stores the language to lookup for _() |
---|
[255] | 19 | public $lang; |
---|
[276] | 20 | |
---|
[261] | 21 | /// The order of array elements in $msg. This array maps |
---|
| 22 | /// $lang shortstring to array index position. |
---|
[255] | 23 | public static $order = array('de' => 0, 'en' => 1); |
---|
| 24 | |
---|
| 25 | function __construct($lang) { |
---|
| 26 | $this->lang = $lang; |
---|
| 27 | } |
---|
| 28 | |
---|
[261] | 29 | /** |
---|
[289] | 30 | * Add new messages to the local overwrite array. |
---|
| 31 | **/ |
---|
| 32 | function set($str_id, $content) { |
---|
| 33 | $this->local_msg[$str_id] = $content; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | /** |
---|
[261] | 37 | * The translate function, shorthand like the gettext shorthand. |
---|
| 38 | * There's actually no long method name version :D |
---|
| 39 | * @param str_id Some key from the $msg array |
---|
[276] | 40 | * @param lang The wanted target language (string like 'de' or 'en'). Leave empty for object default lang ($this->lang) |
---|
[261] | 41 | * @returns Found string in current locale ($lang) |
---|
| 42 | **/ |
---|
[276] | 43 | function _($str_id, $lang=null) { |
---|
[289] | 44 | // local overwrites come first |
---|
| 45 | if(isset($this->local_msg[$str_id])) |
---|
| 46 | return $this->local_msg[$str_id]; |
---|
| 47 | |
---|
[276] | 48 | $lang = ($lang && isset(self::$order[$lang])) ? $lang : $this->lang; |
---|
[255] | 49 | if(!isset(self::$msg[$str_id])) { |
---|
| 50 | return "<$str_id>"; // error; mediawiki style |
---|
| 51 | } else { |
---|
| 52 | if(is_array(self::$msg[$str_id])) { |
---|
[276] | 53 | return self::$msg[$str_id][ self::$order[$lang] ]; |
---|
[255] | 54 | } else |
---|
| 55 | return self::$msg[$str_id]; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
[261] | 60 | * Returns a function which prints the output of _. Usage: |
---|
| 61 | * |
---|
[255] | 62 | * $msg = new t29Messages("foo"); |
---|
| 63 | * print $msg->_("foobar"); // ordinary long version |
---|
| 64 | * $_ = $msg->get_shorthand_printer(); |
---|
| 65 | * $_("foobar"); // same but shorter |
---|
| 66 | **/ |
---|
| 67 | function get_shorthand_printer() { |
---|
| 68 | $t = $this; |
---|
[276] | 69 | return function($str,$lang=null)use($t) { print $t->_($str,$lang); }; |
---|
[255] | 70 | } |
---|
| 71 | |
---|
[261] | 72 | /// same like get_shorthand_printer but return instead of print |
---|
[255] | 73 | function get_shorthand_returner() { |
---|
| 74 | $t = $this; |
---|
[276] | 75 | return function($str,$lang=null)use($t) { return $t->_($str,$lang); }; |
---|
[255] | 76 | } |
---|
| 77 | |
---|
[261] | 78 | /** |
---|
| 79 | * Returns the $msg array as well as the order array encoded as JSON. |
---|
| 80 | * The output will look like '{order:{de:0,en:1},msg:{'foo':['bar,'baz']}}'. |
---|
| 81 | * A given $filter_regexp will be run on the msg keys and hence give |
---|
| 82 | * out only matching entries. Example: $filter_regexp = "/^js-/" |
---|
| 83 | * would filter out all JavaScript related entries. |
---|
[289] | 84 | * This method doesn't consider the $local_msg's. |
---|
[261] | 85 | **/ |
---|
[255] | 86 | static function create_json($filter_regexp=false) { |
---|
| 87 | $msg = $filter_regexp ? array_intersect_key(self::$msg, |
---|
| 88 | array_flip(preg_grep($filter_regexp, array_keys(self::$msg))) |
---|
| 89 | ) : self::$msg; |
---|
| 90 | return json_encode(array( |
---|
| 91 | 'order' => self::$order, |
---|
| 92 | 'msg' => $msg |
---|
| 93 | )); |
---|
| 94 | } |
---|
| 95 | |
---|
[261] | 96 | /** |
---|
[289] | 97 | * Since the static messages are considered as `const`, any changes/overwrites |
---|
| 98 | * will be applied to the instanciated object. Use the gettext for correct |
---|
| 99 | * handling. |
---|
| 100 | * This array simply maps message id to string without language handling since |
---|
| 101 | * the instances don't know languages. |
---|
| 102 | */ |
---|
| 103 | public $local_msg = array(); |
---|
| 104 | |
---|
| 105 | /** |
---|
[261] | 106 | * The Messages array maps a message id (string) to the message text |
---|
| 107 | * (string or numeric array). If the message value is an array, it will be |
---|
| 108 | * interpreted as multi language string, whereas the mapping from language |
---|
| 109 | * to index is supposed to be done via the $order array (see above). |
---|
| 110 | **/ |
---|
[255] | 111 | public static $msg = array( |
---|
| 112 | 'html-title' => 'technikum29', |
---|
| 113 | 'head-h1-title' => array('Zur technikum29 Startseite', 'Go to technikum29 homepage'), |
---|
| 114 | 'head-h1' => 'technikum29', |
---|
[279] | 115 | 'homepage-pagename' => 'startseite', # seiten_id der startseite |
---|
[255] | 116 | |
---|
| 117 | 'sidebar-h2-tour' => array('Museumstour', 'Museum Tour'), |
---|
| 118 | 'sidebar-h2-mainnav' => array('Hauptnavigation', 'Main Navigation'), |
---|
| 119 | 'sidebar-h2-lang' => array('Sprachauswahl', 'Language'), |
---|
| 120 | |
---|
[276] | 121 | 'topnav-interlang-title' => array('Die Seite "%s" auf Deutsch lesen', 'Read the page "%s" in English'), |
---|
| 122 | 'topnav-interlang-active' => array('Sie betrachten gerade die Seite "%s" auf Deutsch', 'You currently read the page "%s" in English'), |
---|
| 123 | 'topnav-interlang-nonexistent' => array('Diese Seite steht auf Deutsch nicht zur Verfügung', 'This page is not available in English'), |
---|
[259] | 124 | 'topnav-search-label' => array('Suchen', 'Search'), |
---|
[273] | 125 | 'topnav-search-page' => array('/suche.php', '/search.php'), |
---|
| 126 | 'opensearch-desc' => array('technikum29 (de)', 'technikum29 (en)'), |
---|
[259] | 127 | |
---|
[278] | 128 | 'js-menu-collapse-out' => array('Mehr Details', 'Expand menu'), |
---|
| 129 | 'js-menu-collapse-in' => array('Weniger Details', 'Fold menu'), |
---|
[255] | 130 | 'js-menu-scroll-show' => array('Menü einblenden', 'Show menu'), |
---|
| 131 | 'js-menu-scroll-hide' => array('Menü ausblenden', 'Hide menu'), |
---|
| 132 | |
---|
| 133 | 'footer-copyright-tag' => '© 2003-2012 technikum29.', |
---|
| 134 | 'footer-legal-link' => array('Impressum und Kontakt', 'Legal notices'), |
---|
[259] | 135 | 'footer-legal-file' => array('/impressum.php', '/contact.php'), |
---|
[299] | 136 | 'footer-legacy-text' => array('© 2003-2012 technikum29. Alle Bilder und Fotografien sind kopierrechtlich geschüzt, siehe <a href="/de/impressum.php" class="go">Impressum</a>', |
---|
[292] | 137 | '© 2003-2012 technikum29. You must not use contents and photographies without the permission of the owner. <a href="/en/contact.php" class="go">Legal Information</a>.'), |
---|
[259] | 138 | |
---|
| 139 | 'nav-rel-prev' => array('vorherige Seite', 'previous page'), |
---|
| 140 | 'nav-rel-next' => array('nächste Seite', 'next page'), |
---|
| 141 | 'nav-rel-start' => array('Starte Führrung', 'Start guided tour'), |
---|
| 142 | |
---|
| 143 | 'head-rel-first' => array('Deutscher Start', 'English start'), |
---|
| 144 | 'head-rel-prev' => array('Zur vorherigen Seite (%s)', 'Previous Page (%s)'), |
---|
| 145 | 'head-rel-next' => array('Zur folgenden Seite (%s)', 'Next Page (%s)'), |
---|
[276] | 146 | 'head-rel-interlang' => array('Deutsche Version dieser Seite (%s)', 'English Version of this page (%s)'), |
---|
[260] | 147 | |
---|
| 148 | // used in /shared/js/modules/heading_links.js |
---|
| 149 | 'js-heading-links' => array("Direktlink zu diesem Abschnitt", "Link to this section"), |
---|
| 150 | // used in /shared/js/modules/img_license.js |
---|
| 151 | 'js-img-license' => array( |
---|
[288] | 152 | '© technikum29. <a href="/de/impressum.php#image-copyright">Lizenzbestimmungen</a>', |
---|
| 153 | '© technikum29. <a href="/en/contact.php#image-copyright">Licensing terms</a>', |
---|
[260] | 154 | ), |
---|
[255] | 155 | ); |
---|
| 156 | } |
---|