1<?php
2
3/**
4 * Plugin RefNotes: Localization
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Mykola Ostrovskyy <dwpforge@gmail.com>
8 */
9
10/**
11 * Plugins that rely on refnotes_localization should use this trait.
12 */
13trait refnotes_localization_plugin {
14    /**
15     *
16     */
17    public function getRawLang() {
18        return $this->lang;
19    }
20}
21
22class refnotes_localization {
23
24    private static $instance = NULL;
25
26    private $plugin;
27
28    /**
29     *
30     */
31    public static function initialize($plugin) {
32        if (self::$instance == NULL) {
33            self::$instance = new refnotes_localization($plugin);
34        }
35    }
36
37    /**
38     *
39     */
40    public static function getInstance() {
41        if (self::$instance == NULL) {
42            throw new Exception('Shared refnotes_localization instance is not properly initialized.');
43        }
44
45        return self::$instance;
46    }
47
48    /**
49     * Constructor
50     */
51    private function __construct($plugin) {
52        $this->plugin = $plugin;
53    }
54
55    /**
56     *
57     */
58    public function getLang($id) {
59        return $this->plugin->getLang($id);
60    }
61
62    /**
63     *
64     */
65    public function getFileName($id) {
66        return $this->plugin->localFN($id);
67    }
68
69    /**
70     *
71     */
72    public function getByPrefix($prefix, $strip = true) {
73        $this->plugin->setupLocale();
74
75        if ($strip) {
76            $pattern = '/^' . $prefix . '_(.+)$/';
77        }
78        else {
79            $pattern = '/^(' . $prefix . '_.+)$/';
80        }
81
82        $result = array();
83
84        foreach ($this->plugin->getRawLang() as $key => $value) {
85            if (preg_match($pattern, $key, $match) == 1) {
86                $result[$match[1]] = $value;
87            }
88        }
89
90        return $result;
91    }
92}
93