Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New block: "Available translations" to list other translations for a theme #72

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/wp-content/themes/wporg-themes-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_once( __DIR__ . '/src/favorite-button/index.php' );
require_once( __DIR__ . '/src/ratings-bars/index.php' );
require_once( __DIR__ . '/src/ratings-stars/index.php' );
require_once( __DIR__ . '/src/theme-available-translations/index.php' );
require_once( __DIR__ . '/src/theme-downloads/index.php' );
require_once( __DIR__ . '/src/theme-patterns/index.php' );
require_once( __DIR__ . '/src/theme-previewer/index.php' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@
<h2 class="wp-block-heading has-heading-4-font-size"><?php esc_html_e( 'Translations', 'wporg-themes' ); ?></h2>
<!-- /wp:heading -->

<!-- wp:wporg/theme-available-translations /-->

<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"wporg-themes/meta","args":{"key":"translate-link"}}}}} -->
<p>Translate this theme</p>
<!-- /wp:paragraph -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "wporg/theme-available-translations",
"version": "0.1.0",
"title": "Available translations",
"category": "design",
"icon": "",
"description": "A list of the available translations for this theme.",
"textdomain": "wporg",
"supports": {
"html": false
},
"usesContext": [ "postId", "postType" ],
"editorScript": "file:./index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import Edit from '../utils/dynamic-edit';
import metadata from './block.json';

registerBlockType( metadata.name, {
edit: Edit,
save: () => null,
} );
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* Block Name: Available translations
* Description: A list of the available translations for this theme.
*
* @package wporg
*/

namespace WordPressdotorg\Theme\Theme_Directory_2024\Theme_Available_Translations_Block;

use function WordPressdotorg\MU_Plugins\Helpers\Locale\{ get_all_locales_with_subdomain, get_translated_locales };

defined( 'WPINC' ) || die();

add_action( 'init', __NAMESPACE__ . '\init' );

/**
* Register the block.
*/
function init() {
register_block_type(
dirname( dirname( __DIR__ ) ) . '/build/theme-available-translations',
array(
'render_callback' => __NAMESPACE__ . '\render',
)
);
}

/**
* Render the block content.
*
* @param array $attributes Block attributes.
* @param string $content Block default content.
* @param WP_Block $block Block instance.
*
* @return string Returns the block markup.
*/
function render( $attributes, $content, $block ) {
if ( ! $block->context['postId'] || ! defined( 'GLOTPRESS_LOCALES_PATH' ) ) {
return '';
}

$theme_post = get_post( $block->context['postId'] );
$theme = wporg_themes_theme_information( $theme_post->post_name );
if ( isset( $theme->error ) ) {
return '';
}

$languages = get_language_links( $theme->slug );

if ( empty( $languages ) ) {
return '';
}

$wrapper_attributes = get_block_wrapper_attributes();
return sprintf(
'<div %s>%s</div>',
$wrapper_attributes,
sprintf(
// translators: %s is a list of links to the theme in each language.
esc_html__( 'This theme is available in the following languages: %s', 'wporg-themes' ),
wp_sprintf( '%l.', $languages )
)
);
}

/**
* Get an array of links to Rosetta sites where this theme is translated.
*/
function get_language_links( $slug ) {
$locale_subdomain_assoc = get_all_locales_with_subdomain();
$translated_locales = get_translated_locales( 'theme', $slug );

$available_languages = array();
foreach ( $translated_locales as $locale ) {
if ( isset( $locale_subdomain_assoc[ $locale ] ) ) {
$language = \GP_Locales::by_field( 'wp_locale', $locale );

$available_languages[ $locale ] = sprintf(
'<a href="https://%1$s.wordpress.org%2$s" lang="%3$s">%4$s</a>',
$locale_subdomain_assoc[ $locale ]->subdomain,
esc_url( trailingslashit( get_site()->path . $slug ) ),
$language->slug,
$language->native_name
);
}
}

// Add in an "English (US)" link back to the main site.
if ( $available_languages || 'en_US' !== get_locale() ) {
$available_languages['en_US'] = sprintf(
'<a href="%1$s" lang="en-US">%2$s</a>',
esc_url( trailingslashit( 'https://wordpress.org/themes/' . $slug ) ),
'English (US)', // Not translated, locale name is in native language.
);
}

ksort( $available_languages, SORT_NATURAL );

return $available_languages;
}