source: t29-www/lib/messages.php @ 301

Last change on this file since 301 was 301, checked in by sven, 12 years ago

Zwei grosse Bugfixes die einige Backendänderungen nach sich zogen:

  • Impressum: Google Maps-Karte lädt wieder. Dafür wurde endlich ein seitenspezifisches Scriptsystem geschrieben, welches im RessourceLoader verankert Scriptfiles direkt einbindet. Das ermöglicht besseres Debugging, bessere Ladezeit und eine Symmetrie zu seitenspezifischen CSS. Insbesondere aber (bislang eher dreckige) Hooks, mit denen externe Scripts eingebunden werden können, was per pagescripts.js nicht geht. pagescripts.js gibts für kleine Scripte immernoch, könnte man dann ggf. auflösen.
  • Relationale Rücklinks für Geräteseiten: Geräteseiten haben nun wie ehemals einen Rücklink auf die verweisende Seite. Dieser wird anhand ihrer Einordnung in der Navigation erlangt. Bei Seiten, die nicht klar einzuordnen waren, könnte dieses Vorgehen ggf. Fehler erzeugen. Das müsste man dann im Einzelfall überprüfen.
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1<?php
2/**
3 * t29v6 Message subsystem.
4 * now with instanciated message overwriting.
5 *
6 **/
7
8// You can run this script to get all messages as JSON.
9// See also /shared/js/modules/msg.js and js.php.
10if(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
17class t29Messages {
18        /// when instanciated, stores the language to lookup for _()
19        public $lang;
20       
21        /// The order of array elements in $msg. This array maps
22        /// $lang shortstring to array index position.
23        public static $order = array('de' => 0, 'en' => 1);
24       
25        function __construct($lang) {
26                $this->lang = $lang;
27        }
28
29        /**
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        /**
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
40         * @param lang  The wanted target language (string like 'de' or 'en'). Leave empty for object default lang ($this->lang)
41         * @returns Found string in current locale ($lang)
42         **/
43        function _($str_id, $lang=null) {
44                // local overwrites come first
45                if(isset($this->local_msg[$str_id]))
46                        return $this->local_msg[$str_id];
47       
48                $lang = ($lang && isset(self::$order[$lang])) ? $lang : $this->lang;
49                if(!isset(self::$msg[$str_id])) {
50                        return "&lt;$str_id&gt;"; // error; mediawiki style
51                } else {
52                        if(is_array(self::$msg[$str_id])) {
53                                return self::$msg[$str_id][ self::$order[$lang] ];
54                        } else
55                                return self::$msg[$str_id];
56                }
57        }
58       
59        /**
60         * Returns a function which prints the output of _. Usage:
61         *
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;
69                return function($str,$lang=null)use($t) { print $t->_($str,$lang); };
70        }
71
72        /// same like get_shorthand_printer but return instead of print
73        function get_shorthand_returner() {
74                $t = $this;
75                return function($str,$lang=null)use($t) { return $t->_($str,$lang); };
76        }
77
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.
84         * This method doesn't consider the $local_msg's.
85         **/
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
96        /**
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        /**
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         **/
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',
115                'homepage-pagename'      => 'startseite', # seiten_id der startseite
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
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'),
124                'topnav-search-label'    => array('Suchen', 'Search'),
125                'topnav-search-page'     => array('/suche.php', '/search.php'),
126                'opensearch-desc'        => array('technikum29 (de)', 'technikum29 (en)'),
127
128                'js-menu-collapse-out'   => array('Mehr Details', 'Expand menu'),
129                'js-menu-collapse-in'    => array('Weniger Details', 'Fold menu'),
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'   => '&copy; 2003-2012 technikum29.',
134                'footer-legal-link'      => array('Impressum und Kontakt', 'Legal notices'),
135                'footer-legal-file'      => array('/impressum.php', '/contact.php'),
136                'footer-legacy-text'     => array('&copy; 2003-2012 technikum29. Alle Bilder und Fotografien sind kopierrechtlich geschüzt, siehe <a href="/de/impressum.php" class="go">Impressum</a>',
137                                                  '&copy; 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>.'),
138               
139                'nav-hierarchy-current'  => array('Aktuelle Seite', 'Current page'),
140                'nav-hierarchy-ancestor' => array('Übergeordnete Kategorie der aktuellen Seite', 'Parental category of current page'),
141                'nav-rel-prev'           => array('vorherige Seite', 'previous page'),
142                'nav-rel-next'           => array('nächste Seite', 'next page'),
143                'nav-rel-start'          => array('Starte Führrung', 'Start guided tour'),
144               
145                'head-rel-first'         => array('Deutscher Start', 'English start'),
146                'head-rel-prev'          => array('Zur vorherigen Seite (%s)', 'Previous Page (%s)'),
147                'head-rel-next'          => array('Zur folgenden Seite (%s)', 'Next Page (%s)'),
148                'head-rel-interlang'     => array('Deutsche Version dieser Seite (%s)', 'English Version of this page (%s)'),
149               
150                // used in /shared/js/modules/heading_links.js
151                'js-heading-links'       => array("Direktlink zu diesem Abschnitt", "Link to this section"),
152                // used in /shared/js/modules/img_license.js
153                'js-img-license'         => array(
154                                                                                '&copy; technikum29. <a href="/de/impressum.php#image-copyright">Lizenzbestimmungen</a>',
155                                                                                '&copy; technikum29. <a href="/en/contact.php#image-copyright">Licensing terms</a>',
156                                                                        ),
157        );
158}
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License