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

Create Block: Enable loading translations for created blocks #24125

Merged
merged 7 commits into from
Jul 27, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ The results of the save function is what the editor will insert into the **post_
If you look at the generated `src/index.js` file, the block title and description are wrapped in a function that looks like this:

```js
__( 'Gutenpride', 'create_block' );
__( 'Gutenpride', 'gutenpride' );
```

This is an internationalization wrapper that allows for the string "Gutenpride" to be translated. The second parameter, "create_block" is called the text domain and gives context for where the string is from. The JavaScript internationalization, often abbreviated i18n, matches the core WordPress internationalization process. See the [I18n for WordPress documentation](https://codex.wordpress.org/I18n_for_WordPress_Developers) for more details.
This is an internationalization wrapper that allows for the string "Gutenpride" to be translated. The second parameter, "gutenpride" is called the text domain and gives context for where the string is from. The JavaScript internationalization, often abbreviated i18n, matches the core WordPress internationalization process. See the [I18n for WordPress documentation](https://codex.wordpress.org/I18n_for_WordPress_Developers) for more details.

Next Section: [Block Attributes](/docs/designers-developers/developers/tutorials/create-block/block-attributes.md)
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default function Edit( { attributes, className, setAttributes } ) {
return (
<div className={ className }>
<TextControl
label={ __( 'Message', 'create-block' ) }
label={ __( 'Message', 'gutenpride' ) }
value={ attributes.message }
onChange={ ( val ) => setAttributes( { message: val } ) }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The main plugin file created is the PHP file `gutenpride.php`, at the top of thi
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: create-block
* Text Domain: gutenpride
*
* @package create-block
*/
Expand Down Expand Up @@ -108,6 +108,7 @@ function create_block_gutenpride_block_init() {
$script_asset['dependencies'],
$script_asset['version']
);
wp_set_script_translations( 'create-block-gutenpride-block-editor', 'gutenpride' );

$editor_css = 'editor.css';
wp_register_style(
Expand Down Expand Up @@ -138,6 +139,8 @@ The build process creates a secondary asset file that contains the list of depen

The `wp_register_script` function registers a name, called the handle, and relates that name to the script file. The dependencies are used to specify if the script requires including other libraries. The version is specified so the browser will reload if the file changed.

The `wp_set_script_translations` function tells WordPress to load translations for this script, if they exist. See more about [translations & internationalization.](/docs/designers-developers/developers/internationalization.md)

The `register_block_type` function registers the block we are going to create and specifies the editor_script file handle registered. So now when the editor loads it will load this script.

With the above in place, create a new post to load the editor and check the you can add the block in the inserter. You can use `/` to search, or click the box with the [+] and search for "Gutenpride" to find the block.
Expand Down
2 changes: 1 addition & 1 deletion packages/create-block/lib/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = async (
author,
license,
licenseURI,
textdomain: namespace,
textdomain: slug,
editorScript,
editorStyle,
style,
Expand Down
1 change: 1 addition & 0 deletions packages/create-block/lib/templates/es5/$slug.php.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
),
filemtime( "$dir/$index_js" )
);
wp_set_script_translations( '{{namespace}}-{{slug}}-block-editor', '{{textdomain}}' );

$editor_css = 'editor.css';
wp_register_style(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function {{namespaceSnakeCase}}_{{slugSnakeCase}}_block_init() {
$script_asset['dependencies'],
$script_asset['version']
);
wp_set_script_translations( '{{namespace}}-{{slug}}-block-editor', '{{textdomain}}' );

$editor_css = 'build/index.css';
wp_register_style(
Expand Down