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

Blockbase: Adding translations #4135

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 6 additions & 2 deletions blockbase/block-templates/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
<main class="wp-block-group page-content">

<!-- wp:heading {"level":1,"fontSize":"large"} -->
<h1 class="has-large-font-size">Oops! That page can’t be found.</h1>
<h1 class="has-large-font-size">
<!-- wp:blockbase/i18n-strings { "string": "Oops! That page can’t be found." } /-->
</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It looks like nothing was found at this location. Maybe try a search?</p>
<p>
<!-- wp:blockbase/i18n-strings { "string": "It looks like nothing was found at this location. Maybe try a search?" } /-->
</p>
<!-- /wp:paragraph -->

<!-- wp:search {"label":"","buttonText":"Search"} /-->
Expand Down
2 changes: 1 addition & 1 deletion blockbase/block-templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<main class="wp-block-group page-content">

<!-- wp:heading -->
<h2>Results:</h2>
<h2><!-- wp:blockbase/i18n-strings { "string": "Results" } /-->:</h2>
<!-- /wp:heading -->

<!-- wp:query {"queryId":1,"query":{"perPage":10,"pages":0,"offset":0,"postType":"post","categoryIds":[],"tagIds":[],"order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true}} -->
Expand Down
11 changes: 8 additions & 3 deletions blockbase/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ function blockbase_fonts_url() {
}

/**
* Customize Global Styles
* Customize Global Styles.
*/
require get_template_directory() . '/inc/wp-customize-colors.php';
require get_template_directory() . '/inc/wp-customize-color-palettes.php';
require_once get_template_directory() . '/inc/wp-customize-colors.php';
require_once get_template_directory() . '/inc/wp-customize-color-palettes.php';

/**
* Load a block for i18n.
*/
require_once get_template_directory() . '/i18n-strings-block/index.php';
17 changes: 17 additions & 0 deletions blockbase/i18n-strings-block/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"apiVersion": 2,
"name": "blockbase/i18n-strings",
"title": "I18N Strings",
"category": "text",
"icon": "smiley",
"description": "Pass strings to PHP for i18n.",
"supports": {
"html": false
},
"textdomain": "blockbase",
"attributes": {
"string": {
"type": "string"
}
}
}
27 changes: 27 additions & 0 deletions blockbase/i18n-strings-block/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

require_once get_stylesheet_directory() . '/i18n-strings.php';

/**
* Registers the block using the metadata loaded from the `block.json` file.
*
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/
*/
function create_block_i18n_strings_block_init() {
register_block_type_from_metadata(
__DIR__,
array(
'render_callback' => 'render_block_i18n_strings',
)
);
}

function render_block_i18n_strings( $attributes ) {
if ( I18N_STRINGS && ! empty ( I18N_STRINGS[ $attributes['string'] ] ) ) {
return I18N_STRINGS[ $attributes['string'] ];
};

return $attributes['string'];
}

add_action( 'init', 'create_block_i18n_strings_block_init' );
7 changes: 7 additions & 0 deletions blockbase/i18n-strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

define ( 'I18N_STRINGS', array(
'Results' => __( 'Results', 'blockbase' ),
'Oops! That page can’t be found.' => __( 'Oops! That page can’t be found.', 'blockbase' ),
'It looks like nothing was found at this location. Maybe try a search?' => __( 'It looks like nothing was found at this location. Maybe try a search?', 'blockbase' ),
) );