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