summaryrefslogtreecommitdiffstats
path: root/wp-includes/l10n/class-wp-translation-file-php.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:57:26 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 07:57:26 +0000
commit30883c26bdceb9eaf32c8d4a1b0c1bce223b5226 (patch)
tree39a02e2aeb21ab5b7923c6f5757d66d55b708912 /wp-includes/l10n/class-wp-translation-file-php.php
parentAdding upstream version 6.4.3+dfsg1. (diff)
downloadwordpress-30883c26bdceb9eaf32c8d4a1b0c1bce223b5226.tar.xz
wordpress-30883c26bdceb9eaf32c8d4a1b0c1bce223b5226.zip
Adding upstream version 6.5+dfsg1.upstream/6.5+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--wp-includes/l10n/class-wp-translation-file-php.php79
1 files changed, 79 insertions, 0 deletions
diff --git a/wp-includes/l10n/class-wp-translation-file-php.php b/wp-includes/l10n/class-wp-translation-file-php.php
new file mode 100644
index 0000000..f93dd01
--- /dev/null
+++ b/wp-includes/l10n/class-wp-translation-file-php.php
@@ -0,0 +1,79 @@
+<?php
+/**
+ * I18N: WP_Translation_File_PHP class.
+ *
+ * @package WordPress
+ * @subpackage I18N
+ * @since 6.5.0
+ */
+
+/**
+ * Class WP_Translation_File_PHP.
+ *
+ * @since 6.5.0
+ */
+class WP_Translation_File_PHP extends WP_Translation_File {
+ /**
+ * Parses the file.
+ *
+ * @since 6.5.0
+ */
+ protected function parse_file() {
+ $this->parsed = true;
+
+ $result = include $this->file;
+ if ( ! $result || ! is_array( $result ) ) {
+ $this->error = 'Invalid data';
+ return;
+ }
+
+ if ( isset( $result['messages'] ) && is_array( $result['messages'] ) ) {
+ foreach ( $result['messages'] as $original => $translation ) {
+ $this->entries[ (string) $original ] = $translation;
+ }
+ unset( $result['messages'] );
+ }
+
+ $this->headers = array_change_key_case( $result );
+ }
+
+ /**
+ * Exports translation contents as a string.
+ *
+ * @since 6.5.0
+ *
+ * @return string Translation file contents.
+ */
+ public function export(): string {
+ $data = array_merge( $this->headers, array( 'messages' => $this->entries ) );
+
+ return '<?php' . PHP_EOL . 'return ' . $this->var_export( $data ) . ';' . PHP_EOL;
+ }
+
+ /**
+ * Outputs or returns a parsable string representation of a variable.
+ *
+ * Like {@see var_export()} but "minified", using short array syntax
+ * and no newlines.
+ *
+ * @since 6.5.0
+ *
+ * @param mixed $value The variable you want to export.
+ * @return string The variable representation.
+ */
+ private function var_export( $value ): string {
+ if ( ! is_array( $value ) ) {
+ return var_export( $value, true );
+ }
+
+ $entries = array();
+
+ $is_list = array_is_list( $value );
+
+ foreach ( $value as $key => $val ) {
+ $entries[] = $is_list ? $this->var_export( $val ) : var_export( $key, true ) . '=>' . $this->var_export( $val );
+ }
+
+ return '[' . implode( ',', $entries ) . ']';
+ }
+}