From bc5cbebe5bf60ddc303d3f7e7429382be46b19da Mon Sep 17 00:00:00 2001 From: Herb Miller Date: Sat, 11 Sep 2021 12:33:48 +0100 Subject: [PATCH] Internationalize #4 --- includes/oik-magnetic-poetry.php | 6 ++-- oik-magnetic-poetry.php | 44 +++++++++++++++--------- src/oik-magnetic-poetry/index.js | 58 ++++---------------------------- 3 files changed, 38 insertions(+), 70 deletions(-) diff --git a/includes/oik-magnetic-poetry.php b/includes/oik-magnetic-poetry.php index 5ffaaa7..54b4825 100644 --- a/includes/oik-magnetic-poetry.php +++ b/includes/oik-magnetic-poetry.php @@ -1,6 +1,6 @@ 'oikmp_dynamic_block_poetry' - , 'editor_script' => 'oik-magnetic-poetry-blocks-js' - , 'editor_style' => 'oik-magnetic-poetry-blocks-css' - , 'style' => 'oik-magnetic-poetry-blocks-css' - , 'attributes' => [ - 'content' => [ 'type' => 'string'] - ] - ] ); - */ $args = [ 'render_callback' => 'oikmp_dynamic_block_poetry']; $registered = register_block_type_from_metadata( __DIR__ . '/src/oik-magnetic-poetry', $args ); //bw_trace2( $registered, "registered", false ); + /** + * Localise the script by loading the required strings for the build/index.js file + * from the locale specific .json file in the languages folder. + */ + $ok = wp_set_script_translations( 'oik-mp-magnetic-poetry-editor-script', 'oik-magnetic-poetry' , __DIR__ .'/languages' ); + add_filter( 'load_script_textdomain_relative_path', 'oikmp_load_script_textdomain_relative_path', 10, 2); +} + +/** + * Filters $relative so that md5's match what's expected. + * + * Depending on how it was built the `build/index.js` may be preceded by `./` or `src/block-name/../../`. + * In either of these situations we want the $relative value to be returned as `build/index.js`. + * This then produces the correct md5 value and the .json file is found. + * + * @param $relative + * @param $src + * + * @return mixed + */ +function oikmp_load_script_textdomain_relative_path( $relative, $src ) { + if ( false !== strrpos( $relative, './build/index.js' )) { + $relative = 'build/index.js'; + } + //bw_trace2( $relative, "relative"); + return $relative; } /** @@ -94,7 +106,6 @@ function oikmp_server_side_wrapper( $attributes, $html ) { return $html; } - /** * Implements 'plugins_loaded' action for oik-magnetic-poetry. * @@ -104,6 +115,7 @@ function oik_magnetic_poetry_plugins_loaded() { oik_magnetic_poetry_boot_libs(); oik_require_lib( "bwtrace" ); oik_require_lib( "bobbfunc" ); + bw_load_plugin_textdomain( "oik-magnetic-poetry"); } /** diff --git a/src/oik-magnetic-poetry/index.js b/src/oik-magnetic-poetry/index.js index 602dcc2..e876aed 100644 --- a/src/oik-magnetic-poetry/index.js +++ b/src/oik-magnetic-poetry/index.js @@ -3,17 +3,13 @@ * * Uses logic from oik-magnetic-poetry plugin * - * @copyright (C) Copyright Bobbing Wide 2019, 2020 + * @copyright (C) Copyright Bobbing Wide 2019, 2020, 2021 * @author Herb Miller @bobbingwide */ import './style.scss'; -//import './editor.scss'; - import { __ } from '@wordpress/i18n'; import classnames from 'classnames'; - -// Get registerBlockType from wp.blocks import { registerBlockType, createBlock } from '@wordpress/blocks'; import {AlignmentControl, BlockControls, InspectorControls, useBlockProps, PlainText} from '@wordpress/block-editor'; import ServerSideRender from '@wordpress/server-side-render'; @@ -27,13 +23,10 @@ import { import { Fragment} from '@wordpress/element'; import { map, partial } from 'lodash'; -import metadata from './block.json'; - /** * Register the WordPress block */ - -export default registerBlockType( metadata, +export default registerBlockType( 'oik-mp/magnetic-poetry', { example: { }, @@ -71,7 +64,6 @@ export default registerBlockType( metadata, ] }, - edit: props => { const { textAlign, label } = props.attributes; @@ -86,30 +78,11 @@ export default registerBlockType( metadata, props.setAttributes( { content: value } ); }; - /** - * Attempt a generic function to apply a change - * using the partial technique - * - * key needs to be in [] otherwise it becomes a literal - * - */ - //onChange={ partial( handleChange, 'someKey' ) } - - function onChangeAttr( key, value ) { - //var nextAttributes = {}; - //nextAttributes[ key ] = value; - //setAttributes( nextAttributes ); - props.setAttributes( { [key] : value } ); - }; - const isSelected = props.isSelected; - - - return ( - + { @@ -117,21 +90,12 @@ export default registerBlockType( metadata, } } /> - - - - - - - - - {!isSelected &&
- + />
} @@ -140,21 +104,13 @@ export default registerBlockType( metadata,
</div> } - - </Fragment> - ); - }, - - - save() { - return null; - }, + } }, );