data( $post ); echo '
'; if ( $data['copyOf'] ) { $copy = $data['copyOf']; ?>

%1$s copy of “%2$s”. Visitors who open that page in %1$s see this one.', 'swiftling' ), esc_html( $copy['language'] ), esc_html( $copy['title'] ) ) /* translators: 1: language name, e.g. Deutsch, 2: title of the original. */ : sprintf( __( 'This is the separate %1$s copy of “%2$s”. Visitors who open that page in %1$s see this one once it is published.', 'swiftling' ), esc_html( $copy['language'] ), esc_html( $copy['title'] ) ), array( 'strong' => array() ) ); ?>

' . esc_html__( 'No languages chosen yet.', 'swiftling' ) . '

'; printf( '%s', esc_url( $data['languagesUrl'] ), esc_html__( 'Add languages', 'swiftling' ) ); } elseif ( 'auto-draft' === $post->post_status ) { // Nothing is saved yet, and the translation starts from what is saved. echo '

' . esc_html__( 'Save it first: the translation starts from what is saved here.', 'swiftling' ) . '

'; } else { ?>

%s.', 'swiftling' ), esc_html( $data['source'] ) ), array( 'strong' => array() ) ); ?>

'; } /** * Returns what a language's row says, as the block editor's panel says it (rowState() in * assets/js/editor-panel.js): the state of the translation, of the separate copy, or that the * language keeps the original, showing the post as it is. * * @param array $language Language, see language(). * @return array{0: string, 1: string} State for the dot, and the words. */ private static function rowState( array $language ): array { if ( 'manual' === $language['mode'] && $language['copy'] && $language['copy']['published'] ) { return 'outdated' === $language['state'] ? array( 'outdated', __( 'Copy · original changed', 'swiftling' ) ) : array( 'reviewed', __( 'Separate copy', 'swiftling' ) ); } if ( 'manual' === $language['mode'] ) { return array( 'draft', __( 'Copy in draft', 'swiftling' ) ); } if ( 'duplicate' === $language['mode'] ) { return array( 'duplicate', __( 'Keeps the original', 'swiftling' ) ); } $labels = array( 'reviewed' => __( 'Reviewed', 'swiftling' ), 'machine' => __( 'Machine translated', 'swiftling' ), 'official' => __( 'Reviewed', 'swiftling' ), 'outdated' => __( 'Original changed', 'swiftling' ), 'partial' => __( 'Partly translated', 'swiftling' ), 'missing' => __( 'Not translated', 'swiftling' ), ); $state = isset( $labels[ $language['state'] ] ) ? $language['state'] : 'missing'; return array( $state, $labels[ $state ] ); } /** * Loads the panel script in the post editor only, not in the site or widget editor. */ public function enqueue(): void { $screen = get_current_screen(); if ( ! $screen || 'post' !== $screen->base ) { return; } wp_enqueue_script( 'swiftling-editor-panel', SWIFTLING_URL . 'assets/js/editor-panel.js', array( 'wp-api-fetch', 'wp-components', 'wp-data', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-plugins' ), SWIFTLING_VERSION, true ); wp_set_script_translations( 'swiftling-editor-panel', 'swiftling' ); wp_enqueue_style( 'swiftling-editor-panel', SWIFTLING_URL . 'assets/css/editor-panel.css', array(), SWIFTLING_VERSION ); wp_add_inline_script( 'swiftling-editor-panel', 'window.swiftlingEditorPanel = ' . wp_json_encode( $this->data( get_post() ) ) . ';', 'before' ); } /** * Returns what the panel shows: each language of the post, with its state, the address of its * translation editor and its separate copy, if it has one; or, for a copy, its original. * * @param \WP_Post|null $post Post being edited. * @return array */ private function data( $post ): array { $languages = swiftling_languages(); $data = array( 'source' => $languages['source']['name'], 'languages' => array(), 'languagesUrl' => admin_url( 'admin.php?page=swiftling-languages' ), 'canCopy' => false, 'canChoose' => false, 'copyOf' => null, ); if ( ! $post ) { return $data; } $original_id = swiftling_copy_original_id( $post ); if ( $original_id ) { $slug = swiftling_locale_slug( (string) get_post_meta( $post->ID, SWIFTLING_COPY_LANG, true ) ); $language = '' === $slug ? null : swiftling_target_language( $slug ); $data['copyOf'] = array( 'title' => get_the_title( $original_id ), 'published' => 'publish' === $post->post_status, 'language' => $language ? $language['name'] : '', 'editorUrl' => swiftling_editor_url( $original_id, $slug ), ); return $data; } foreach ( array_keys( $languages['targets'] ) as $slug ) { $data['languages'][] = self::language( $post, (string) $slug ); } $data['canCopy'] = swiftling_can_make_copy( $post ); $data['canChoose'] = swiftling_can_choose_mode( $post ); return $data; } /** * Returns one language as the panel shows it: its state, how it shows the post (translation, * manual or duplicate), the address of its translation editor and its separate copy, if any. * * @param \WP_Post $post Post. * @param string $slug Address slug of the language. * @return array */ public static function language( $post, string $slug ): array { $language = swiftling_target_language( $slug ); $states = swiftling_language_states( $post->ID ); $copy = swiftling_copy_for( $post->ID, $language['locale'] ); return array( 'slug' => $slug, 'code' => $language['code'], 'name' => $language['name'], 'state' => isset( $states[ $slug ] ) ? $states[ $slug ] : 'missing', 'mode' => swiftling_language_mode( $post->ID, $language['locale'] ), 'url' => swiftling_editor_url( $post->ID, $slug ), 'copy' => $copy ? array( 'published' => 'publish' === $copy->post_status, 'editUrl' => \SwiftLing\Builders\Registry::editUrl( $copy->ID ), ) : null, ); } }