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 | * @returns Found string in current locale ($lang) |
---|
33 | **/ |
---|
34 | function _($str_id) { |
---|
35 | if(!isset(self::$msg[$str_id])) { |
---|
36 | return "<$str_id>"; // error; mediawiki style |
---|
37 | } else { |
---|
38 | if(is_array(self::$msg[$str_id])) { |
---|
39 | return self::$msg[$str_id][ self::$order[$this->lang] ]; |
---|
40 | } else |
---|
41 | return self::$msg[$str_id]; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Returns a function which prints the output of _. Usage: |
---|
47 | * |
---|
48 | * $msg = new t29Messages("foo"); |
---|
49 | * print $msg->_("foobar"); // ordinary long version |
---|
50 | * $_ = $msg->get_shorthand_printer(); |
---|
51 | * $_("foobar"); // same but shorter |
---|
52 | **/ |
---|
53 | function get_shorthand_printer() { |
---|
54 | $t = $this; |
---|
55 | return function($str)use($t) { print $t->_($str); }; |
---|
56 | } |
---|
57 | |
---|
58 | /// same like get_shorthand_printer but return instead of print |
---|
59 | function get_shorthand_returner() { |
---|
60 | $t = $this; |
---|
61 | return function($str)use($t) { return $t->_($str); }; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Returns the $msg array as well as the order array encoded as JSON. |
---|
66 | * The output will look like '{order:{de:0,en:1},msg:{'foo':['bar,'baz']}}'. |
---|
67 | * A given $filter_regexp will be run on the msg keys and hence give |
---|
68 | * out only matching entries. Example: $filter_regexp = "/^js-/" |
---|
69 | * would filter out all JavaScript related entries. |
---|
70 | **/ |
---|
71 | static function create_json($filter_regexp=false) { |
---|
72 | $msg = $filter_regexp ? array_intersect_key(self::$msg, |
---|
73 | array_flip(preg_grep($filter_regexp, array_keys(self::$msg))) |
---|
74 | ) : self::$msg; |
---|
75 | return json_encode(array( |
---|
76 | 'order' => self::$order, |
---|
77 | 'msg' => $msg |
---|
78 | )); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * The Messages array maps a message id (string) to the message text |
---|
83 | * (string or numeric array). If the message value is an array, it will be |
---|
84 | * interpreted as multi language string, whereas the mapping from language |
---|
85 | * to index is supposed to be done via the $order array (see above). |
---|
86 | **/ |
---|
87 | public static $msg = array( |
---|
88 | 'html-title' => 'technikum29', |
---|
89 | 'head-h1-title' => array('Zur technikum29 Startseite', 'Go to technikum29 homepage'), |
---|
90 | 'head-h1' => 'technikum29', |
---|
91 | |
---|
92 | 'sidebar-h2-tour' => array('Museumstour', 'Museum Tour'), |
---|
93 | 'sidebar-h2-mainnav' => array('Hauptnavigation', 'Main Navigation'), |
---|
94 | 'sidebar-h2-lang' => array('Sprachauswahl', 'Language'), |
---|
95 | |
---|
96 | 'topnav-interlang-title' => array('Read this page (%s) in English', 'Diese Seite (%s) auf Deutsch lesen'), |
---|
97 | 'topnav-search-label' => array('Suchen', 'Search'), |
---|
98 | |
---|
99 | 'js-menu-collapse-out' => array('Menü ausklappen', 'Expand menu'), |
---|
100 | 'js-menu-collapse-in' => array('Menü einklappen', 'Fold menu'), |
---|
101 | 'js-menu-scroll-show' => array('Menü einblenden', 'Show menu'), |
---|
102 | 'js-menu-scroll-hide' => array('Menü ausblenden', 'Hide menu'), |
---|
103 | |
---|
104 | 'footer-copyright-tag' => '© 2003-2012 technikum29.', |
---|
105 | 'footer-legal-link' => array('Impressum und Kontakt', 'Legal notices'), |
---|
106 | 'footer-legal-file' => array('/impressum.php', '/contact.php'), |
---|
107 | |
---|
108 | 'nav-rel-prev' => array('vorherige Seite', 'previous page'), |
---|
109 | 'nav-rel-next' => array('nächste Seite', 'next page'), |
---|
110 | 'nav-rel-start' => array('Starte Führrung', 'Start guided tour'), |
---|
111 | |
---|
112 | 'head-rel-first' => array('Deutscher Start', 'English start'), |
---|
113 | 'head-rel-prev' => array('Zur vorherigen Seite (%s)', 'Previous Page (%s)'), |
---|
114 | 'head-rel-next' => array('Zur folgenden Seite (%s)', 'Next Page (%s)'), |
---|
115 | 'head-rel-interlang' => array('Englische Version dieser Seite (%s)', 'Deutsche Version dieser Seite (%s)'), |
---|
116 | |
---|
117 | // used in /shared/js/modules/heading_links.js |
---|
118 | 'js-heading-links' => array("Direktlink zu diesem Abschnitt", "Link to this section"), |
---|
119 | // used in /shared/js/modules/img_license.js |
---|
120 | 'js-img-license' => array( |
---|
121 | '© technikum29. <a href="/de-v6/impressum.php#image-copyright">Lizenzbestimmungen</a>', |
---|
122 | '© technikum29. <a href="/en-v6/contact.php#image-copyright">Licensing terms</a>', |
---|
123 | ), |
---|
124 | ); |
---|
125 | } |
---|