Making WordPress.org

Changeset 10064


Ignore:
Timestamp:
07/11/2020 04:04:59 PM (4 years ago)
Author:
ocean90
Message:

Translate: When exporting the JSON files for WordPress core JavaScript translations, check for an existing JSON file and merge translations accordingly.

See #5314.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-customizations/inc/cli/class-export.php

    r8500 r10064  
    149149                    return $file;
    150150                }
     151
    151152                return 'po';
    152153            }, $entry->references );
     
    187188            $json_content_decoded->comment = [ 'reference' => $file ];
    188189
    189             $json_content = wp_json_encode( $json_content_decoded );
    190 
    191190            $hash = md5( $file );
    192191            $dest = "{$base_dest}-{$hash}.json";
    193192
    194             file_put_contents( $dest, $json_content );
     193            /*
     194             * Merge translations into an existing JSON file.
     195             *
     196             * Some strings occur in multiple source files which may be used on the frontend
     197             * or in the admin or both, thus they can be part of different translation
     198             * projects (wp/dev, wp/dev/admin, wp/dev/admin/network).
     199             * Unlike in PHP with gettext, where translations from multiple MO files are merged
     200             * automatically, we have do merge the translations before shipping the
     201             * single JSON file per reference.
     202             */
     203            if ( file_exists( $dest ) ) {
     204                $existing_json_content_decoded = json_decode( file_get_contents( $dest ) );
     205                if ( isset( $existing_json_content_decoded->locale_data->messages ) ) {
     206                    foreach ( $existing_json_content_decoded->locale_data->messages as $key => $translations ) {
     207                        if ( ! isset( $json_content_decoded->locale_data->messages->{ $key } ) ) {
     208                            $json_content_decoded->locale_data->messages->{ $key } = $translations;
     209                        }
     210                    }
     211                }
     212            }
     213
     214            file_put_contents( $dest, wp_json_encode( $json_content_decoded ) );
    195215
    196216            $files[] = $dest;
Note: See TracChangeset for help on using the changeset viewer.