Make WordPress Core

Ticket #45425: wp-php-filters-for-js-l10n.php

File wp-php-filters-for-js-l10n.php, 1.3 KB (added by dimadin, 6 years ago)
Line 
1<?php
2
3if ( ! function_exists( 'wp_php_filters_for_js_l10n' ) ) :
4        function wp_php_filters_for_js_l10n( $json_translations, $file, $handle, $domain ) {
5                $translations = json_decode( $json_translations, true );
6
7                if ( is_array( $translations ) && isset( $translations['locale_data'], $translations['locale_data']['messages'] ) && is_array( $translations['locale_data']['messages'] ) ) {
8                        foreach ( $translations['locale_data']['messages'] as $original_string => &$translated_strings ) {
9                                if ( ! $original_string || ! is_array( $translated_strings ) ) {
10                                        continue;
11                                }
12
13                                $context = '';
14
15                                if ( strpos( $original_string, chr( 4 ) ) ) {
16                                        $with_context    = explode( chr( 4 ), $original_string );
17                                        $context         = $with_context[0];
18                                        $original_string = $with_context[1];
19                                }
20
21                                foreach ( $translated_strings as &$translated_string ) {
22                                        if ( $context ) {
23                                                $translated_string = apply_filters( 'gettext_with_context', $translated_string, $original_string, $context, $domain );
24                                        } else {
25                                                $translated_string = apply_filters( 'gettext', $translated_string, $original_string, $domain );
26                                        }
27                                }
28                        }
29                }
30
31                return wp_json_encode( $translations );
32        }
33        add_filter( 'load_script_translation', 'wp_php_filters_for_js_l10n', 10, 4 );
34endif;