1 | <?php |
||
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() { |
||
56 | |||
57 | /** |
||
58 | * @since 2.7 |
||
59 | */ |
||
60 | public static function enqueue_scripts() { |
||
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() { |
||
198 | |||
199 | } |
||
200 |