1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Pods |
5
|
|
|
* @since 2.7 |
6
|
|
|
*/ |
7
|
|
|
final class PodsI18n { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var PodsI18n Singleton instance |
11
|
|
|
*/ |
12
|
|
|
private static $instance = null; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array Key/value pairs with label/translation |
16
|
|
|
*/ |
17
|
|
|
private static $strings = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Singleton handling for a basic pods_i18n() request |
21
|
|
|
* |
22
|
|
|
* @return PodsI18n |
23
|
|
|
* |
24
|
|
|
* @since 2.7 |
25
|
|
|
*/ |
26
|
|
|
public static function init() { |
27
|
|
|
|
28
|
|
|
if ( ! is_object( self::$instance ) ) { |
29
|
|
|
self::$instance = new PodsI18n(); |
30
|
|
|
|
31
|
|
|
// Hook all enqueue scripts actions |
32
|
|
|
add_action( 'wp_enqueue_scripts', array( 'PodsI18n', 'enqueue_scripts' ) ); |
33
|
|
|
add_action( 'admin_enqueue_scripts', array( 'PodsI18n', 'enqueue_scripts' ) ); |
34
|
|
|
add_action( 'login_enqueue_scripts', array( 'PodsI18n', 'enqueue_scripts' ) ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return self::$instance; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Singleton handling for a basic pods_i18n() request |
42
|
|
|
* |
43
|
|
|
* @return \PodsI18n |
44
|
|
|
* |
45
|
|
|
* @since 2.7 |
46
|
|
|
*/ |
47
|
|
|
public static function get_instance() { |
48
|
|
|
|
49
|
|
|
// Initialize if the class hasn't been setup yet for some reason |
50
|
|
|
if ( ! is_object( self::$instance ) ) { |
51
|
|
|
self::init(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return self::$instance; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @since 2.7 |
59
|
|
|
*/ |
60
|
|
|
public static function enqueue_scripts() { |
61
|
|
|
|
62
|
|
|
// Register our i18n script for JS |
63
|
|
|
wp_register_script( 'pods-i18n', PODS_URL . 'ui/js/pods-i18n.js', array(), PODS_VERSION, true ); |
64
|
|
|
|
65
|
|
|
self::localize_assets(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Localize assets: |
70
|
|
|
* * Build localizations strings from the defaults and those provided via filter |
71
|
|
|
* * Provide a global JavaScript object with the assembled localization strings via `wp_localize_script` |
72
|
|
|
* |
73
|
|
|
* @since 2.7 |
74
|
|
|
*/ |
75
|
|
|
private static function localize_assets() { |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add strings to the localization |
79
|
|
|
* Setting the key of your string to the original (non translated) value is mandatory |
80
|
|
|
* Note: Existing keys in this class will overwrite the ones of this filter! |
81
|
|
|
* |
82
|
|
|
* @since 2.7 |
83
|
|
|
* @see default_strings() |
84
|
|
|
* |
85
|
|
|
* @param array |
86
|
|
|
* |
87
|
|
|
* @return array format: 'Untranslated string' => 'Translated string with use of WP translate functions' |
88
|
|
|
*/ |
89
|
|
|
$strings_extra = apply_filters( 'pods_localized_strings', array() ); |
90
|
|
|
|
91
|
|
|
self::$strings = array_merge( $strings_extra, self::default_strings() ); |
92
|
|
|
|
93
|
|
|
foreach ( self::$strings as $key => $str ) { |
94
|
|
|
self::register( $key, $str ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// Some other stuff we need to pass through |
98
|
|
|
$i18n_base = array( |
99
|
|
|
'debug' => ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG == true ) ? true : false, |
100
|
|
|
); |
101
|
|
|
// Add localization to our i18n script |
102
|
|
|
wp_localize_script( 'pods-i18n', 'podsLocalizedStrings', array_merge( self::$strings, $i18n_base ) ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Register function that creates the references and combines these with the translated strings |
107
|
|
|
* |
108
|
|
|
* @param string $string_key |
109
|
|
|
* @param string $translation |
110
|
|
|
* |
111
|
|
|
* @since 2.7 |
112
|
|
|
*/ |
113
|
|
|
private static function register( $string_key, $translation ) { |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Converts string into reference object variable |
117
|
|
|
* Uses the same logic as JS to create the same references |
118
|
|
|
*/ |
119
|
|
|
$ref = '__' . $string_key; |
120
|
|
|
|
121
|
|
|
// Add it to the strings localized |
122
|
|
|
self::$strings[ $ref ] = $translation; |
123
|
|
|
|
124
|
|
|
// Remove the old key |
125
|
|
|
unset( self::$strings[ $string_key ] ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Register our labels to use in JS |
130
|
|
|
* We need to register them as normal string to convert to JS references |
131
|
|
|
* And we need to register the translations to attach to these references, these may not be variables! |
132
|
|
|
* |
133
|
|
|
* @return array Key/value pairs with label/translation |
134
|
|
|
* |
135
|
|
|
* @since 2.7 |
136
|
|
|
*/ |
137
|
|
|
private static function default_strings() { |
138
|
|
|
|
139
|
|
|
return array( |
140
|
|
|
|
141
|
|
|
'%s is required.' => |
142
|
|
|
__( '%s is required.', 'pods' ), |
143
|
|
|
|
144
|
|
|
'This field is required.' => |
145
|
|
|
__( 'This field is required.', 'pods' ), |
146
|
|
|
|
147
|
|
|
'Add' => |
148
|
|
|
__( 'Add', 'pods' ), |
149
|
|
|
|
150
|
|
|
'Add New' => |
151
|
|
|
__( 'Add New', 'pods' ), |
152
|
|
|
|
153
|
|
|
'Add New Record' => |
154
|
|
|
__( 'Add New Record', 'pods' ), |
155
|
|
|
|
156
|
|
|
'Added!' => |
157
|
|
|
__( 'Added!', 'pods' ), |
158
|
|
|
|
159
|
|
|
'Added! Choose another or <a href="#">close this box</a>' => |
160
|
|
|
__( 'Added! Choose another or <a href="#">close this box</a>', 'pods' ), |
161
|
|
|
|
162
|
|
|
'Copy' => |
163
|
|
|
__( 'Copy', 'pods' ), |
164
|
|
|
|
165
|
|
|
'Reorder' => |
166
|
|
|
__( 'Reorder', 'pods' ), |
167
|
|
|
|
168
|
|
|
'Remove' => |
169
|
|
|
__( 'Remove', 'pods' ), |
170
|
|
|
|
171
|
|
|
'Download' => |
172
|
|
|
__( 'Download', 'pods' ), |
173
|
|
|
|
174
|
|
|
'View' => |
175
|
|
|
__( 'View', 'pods' ), |
176
|
|
|
|
177
|
|
|
'Edit' => |
178
|
|
|
__( 'Edit', 'pods' ), |
179
|
|
|
|
180
|
|
|
'Navigating away from this page will discard any changes you have made.' => |
181
|
|
|
__( 'Navigating away from this page will discard any changes you have made.', 'pods' ), |
182
|
|
|
|
183
|
|
|
'Unable to process request, please try again.' => |
184
|
|
|
__( 'Unable to process request, please try again.', 'pods' ), |
185
|
|
|
|
186
|
|
|
'There was an issue with the file upload, please try again.' => |
187
|
|
|
__( 'There was an issue with the file upload, please try again.', 'pods' ), |
188
|
|
|
|
189
|
|
|
'Allowed Files' => |
190
|
|
|
__( 'Allowed Files', 'pods' ), |
191
|
|
|
|
192
|
|
|
'The Title' => |
193
|
|
|
__( 'The Title', 'pods' ), |
194
|
|
|
|
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|