Automattic\WooCommerce\Internal\Admin

Translations::combine_official_translation_chunks()privateWC 1.0

Combines data from translation chunk files based on officially downloaded file format.

Method of the class: Translations{}

No Hooks.

Return

Array. Combined translation chunk data.

Usage

// private - for code of main (parent) class only
$result = $this->combine_official_translation_chunks( $json_i18n_filenames );
$json_i18n_filenames(array) (required)
List of JSON chunk files.

Translations::combine_official_translation_chunks() code WC 9.0.1

private function combine_official_translation_chunks( $json_i18n_filenames ) {
	// the filesystem object should be hooked up.
	global $wp_filesystem;
	$combined_translation_data = array();

	foreach ( $json_i18n_filenames as $json_filename ) {
		if ( ! $wp_filesystem->is_readable( $json_filename ) ) {
			continue;
		}

		$file_contents = $wp_filesystem->get_contents( $json_filename );
		$chunk_data    = \json_decode( $file_contents, true );

		if ( empty( $chunk_data ) ) {
			continue;
		}

		if ( ! isset( $chunk_data['comment']['reference'] ) ) {
			continue;
		}

		$reference_file = $chunk_data['comment']['reference'];

		// Only combine "app" files (not scripts registered with WP).
		if (
			false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'app/index.js' ) &&
			false === strpos( $reference_file, WC_ADMIN_DIST_JS_FOLDER . 'chunks/' )
		) {
			continue;
		}

		if ( empty( $combined_translation_data ) ) {
			// Use the first translation file as the base structure.
			$combined_translation_data = $chunk_data;
		} else {
			// Combine all messages from all chunk files.
			$combined_translation_data['locale_data']['messages'] = array_merge(
				$combined_translation_data['locale_data']['messages'],
				$chunk_data['locale_data']['messages']
			);
		}
	}

	// Remove inaccurate reference comment.
	unset( $combined_translation_data['comment'] );
	return $combined_translation_data;
}