PluginProbe
Flexible Map / trunk
Flexible Map vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4a 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.2.0 All 52 releases
wp-flexible-map / includes / class.FlxMapLocalisation.php

class.FlxMapLocalisation.php in Flexible Map trunk, at includes/class.FlxMapLocalisation.php

179 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * class for managing the plugin
9 */
10 class FlxMapLocalisation {
11
12 const TEXT_DOMAIN = 'wp-flexible-map';
13 const TRANSIENT_LANGPACKS = 'flexible_map_langpacks';
14
15 protected $script_strings;
16
17 /**
18 * initialise the class if it hasn't been
19 */
20 public function __construct() {
21 // declare l10n strings used in JavaScript so a gettext scanner can find them
22 // and record for use in filtering localisations
23 $this->script_strings = array(
24 'Click for details' => __('Click for details', 'wp-flexible-map'),
25 'Directions' => __('Directions', 'wp-flexible-map'),
26 'From' => __('From', 'wp-flexible-map'),
27 'Get directions' => __('Get directions', 'wp-flexible-map'),
28 );
29 }
30
31 /**
32 * get localisations for map scripts, ignoring admin localisations
33 * @param array $locales
34 * @return array
35 */
36 public function getLocalisations($locales) {
37 $i18n = array();
38
39 $langPacks = array(); // record list of locales for language pack update fetches
40
41 foreach (array_keys($locales) as $locale) {
42 $moLocale = self::getMoLocale($locale);
43 $mofile = self::getMofilePath($moLocale);
44
45 $langPacks[] = $moLocale;
46
47 // check for specific locale first, e.g. 'zh-CN', then for generic locale, e.g. 'zh'
48 // (backwards compatibility)
49 if (!$mofile && strlen($locale) > 2) {
50 $locale = substr($locale, 0, 2);
51 $moLocale = self::getMoLocale($locale);
52 $mofile = self::getMofilePath($moLocale);
53 }
54
55 if ($mofile) {
56 $mo = new MO();
57 if ($mo->import_from_file($mofile)) {
58 $strings = self::getMoStrings($mofile);
59 if (!empty($strings)) {
60 // filter to only contain strings we need in JavaScript
61 $strings = array_intersect_key($strings, $this->script_strings);
62 $i18n[$locale] = $strings;
63 }
64 }
65 }
66 }
67
68 if (!empty($langPacks)) {
69 $this->updateGlobalMapLocales($langPacks);
70 }
71
72 return $i18n;
73 }
74
75 /**
76 * translate locale code to .mo file code, for backwards compatibility with earlier versions of plugin
77 * @param string $locale
78 * @return string
79 */
80 protected static function getMoLocale($locale) {
81 // map old two-character language-only locales that now need to target language_country translations
82 static $upgradeMap = array(
83 'bg' => 'bg_BG', 'cs' => 'cs_CZ', 'da' => 'da_DK', 'de' => 'de_DE',
84 'es' => 'es_ES', 'fa' => 'fa_IR', 'fr' => 'fr_FR', 'gl' => 'gl_ES',
85 'he' => 'he_IL', 'hi' => 'hi_IN', 'hu' => 'hu_HU', 'id' => 'id_ID',
86 'is' => 'is_IS', 'it' => 'it_IT', 'ko' => 'ko_KR', 'lt' => 'lt_LT',
87 'mk' => 'mk_MK', 'ms' => 'ms_MY', 'mt' => 'mt_MT', 'nb' => 'nb_NO',
88 'nl' => 'nl_NL', 'pl' => 'pl_PL', 'pt' => 'pt_PT', 'ro' => 'ro_RO',
89 'ru' => 'ru_RU', 'sk' => 'sk_SK', 'sl' => 'sl_SL', 'sr' => 'sr_RS',
90 'sv' => 'sv_SE', 'ta' => 'ta_IN', 'tr' => 'tr_TR', 'zh' => 'zh_CN',
91 );
92
93 if (isset($upgradeMap[$locale])) {
94 // upgrade old two-character language-only locales
95 return $upgradeMap[$locale];
96 }
97
98 // revert locale name to WordPress locale name as used in .mo files
99 return strtr($locale, '-', '_');
100 }
101
102 /**
103 * get full path to .mo file
104 * @param string $moLocale
105 * @return string|bool returns false if it doesn't exist
106 */
107 protected static function getMofilePath($moLocale) {
108 // try translation packs downloaded from translate.wordpress.org first
109 $path = sprintf('%s/plugins/%s-%s.mo', WP_LANG_DIR, self::TEXT_DOMAIN, $moLocale);
110 if (is_readable($path)) {
111 return $path;
112 }
113
114 // try local packs delivered with the plugin
115 $path = sprintf('%slanguages/%s-%s.mo', FLXMAP_PLUGIN_ROOT, self::TEXT_DOMAIN, $moLocale);
116 if (is_readable($path)) {
117 return $path;
118 }
119
120 return false;
121 }
122
123 /**
124 * get strings from .mo file
125 * @param string $mofile
126 * @return array
127 */
128 protected static function getMoStrings($mofile) {
129 $strings = array();
130
131 $mo = new MO();
132 if ($mo->import_from_file($mofile)) {
133 // pull all translation strings into a simplified format for our script
134 // TODO: handle plurals (not yet needed, don't have any)
135 foreach ($mo->entries as $original => $translation) {
136 // skip admin-side strings, identified by context
137 if ($translation->context === 'plugin details links') {
138 continue;
139 }
140
141 $strings[$original] = $translation->translations[0];
142 }
143 }
144
145 return $strings;
146 }
147
148 /**
149 * get locales used by maps across site (or multisite network)
150 * for global language updates from translate.wordpress.org
151 * @return array
152 */
153 public static function getGlobalMapLocales() {
154 $langPacks = get_site_transient(self::TRANSIENT_LANGPACKS);
155
156 if (!is_array($langPacks)) {
157 $langPacks = array();
158 }
159
160 return $langPacks;
161 }
162
163 /**
164 * add locales to history of locales used by maps across site (or multisite network)
165 * for global language updates from translate.wordpress.org
166 * @param array $locales
167 */
168 protected function updateGlobalMapLocales($locales) {
169 $langPacks = self::getGlobalMapLocales();
170 $diff = array_diff($locales, $langPacks);
171
172 if (!empty($diff)) {
173 $langPacks = array_merge($langPacks, $diff);
174 set_site_transient(self::TRANSIENT_LANGPACKS, $langPacks, WEEK_IN_SECONDS);
175 }
176 }
177
178 }
179