Last active
April 11, 2019 18:43
Star
You must be signed in to star a gist
Get With Gutenberg - How to Internationalize Your Block • https://getwithgutenberg.com/2019/04/how-to-internationalize-your-block/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function gwg_register_block_type() { | |
if ( ! function_exists( 'register_block_type' ) ) { | |
// Gutenberg is not active. | |
return; | |
} | |
wp_register_style( | |
'gwg-style', | |
GWG_ESNEXT_PLUGIN_URL . 'style.css', | |
[], | |
GWG_ESNEXT_VERSION | |
); | |
wp_register_style( | |
'gwg-editor', | |
GWG_ESNEXT_PLUGIN_URL . 'editor.css', | |
[], | |
GWG_ESNEXT_VERSION | |
); | |
wp_register_script( | |
'gwg-block', | |
GWG_ESNEXT_PLUGIN_URL . 'block.build.js', | |
[ 'wp-blocks', 'wp-i18n' ], | |
GWG_ESNEXT_VERSION, | |
true // Enqueue script in the footer. | |
); | |
register_block_type( | |
'gwg/esnext-starter', | |
[ | |
'editor_script' => 'gwg-block', | |
'editor_style' => 'gwg-editor', | |
'style' => 'gwg-style', | |
] | |
); | |
} | |
add_action( 'init', 'gwg_register_block_type' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
wp_register_script( | |
'gwg-block', | |
GWG_ESNEXT_PLUGIN_URL . 'block.build.js', | |
[ 'wp-blocks', 'wp-i18n' ], | |
GWG_ESNEXT_VERSION, | |
true // Enqueue script in the footer. | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { __ } = wp.i18n; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
registerBlockType('gwg/esnext-starter', { | |
title: __('Get With Gutenberg - ESNext Starter', 'gwg'), | |
category: 'common', | |
edit(props) { | |
return <p className={props.className}>{__('Hello editor.', 'gwg')}</p>; | |
}, | |
save(props) { | |
return <p className={props.className}>{__('Hello saved content.', 'gwg')}</p>; | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp i18n make-pot ./ ./languages/gwg.pot --headers='{"Last-Translator":"JR Tashjian <jr@getwithgutenberg.com>","Language-Team":"Get With Gutenberg <info@getwithgutenberg.com>"}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#: block.js:5 | |
msgid "Get With Gutenberg - ESNext Starter" | |
msgstr "" | |
#: block.js:9 | |
msgid "Hello editor." | |
msgstr "" | |
#: block.js:13 | |
msgid "Hello saved content." | |
msgstr "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cp ./languages/gwg.pot ./languages/gwg-en_US.po |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"scripts": { | |
"i18n-pot": "wp i18n make-pot ./ ./languages/gwg.pot --headers='{\"Last-Translator\":\"JR Tashjian <jr@getwithgutenberg.com>\",\"Language-Team\":\"Get With Gutenberg <info@getwithgutenberg.com>\"}' && cp ./languages/gwg.pot ./languages/gwg-en_US.po" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp i18n make-json languages/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mv languages/gwg-en_us-5bd7b3c720e0df736021f834799d5ef3.json languages/gwg-en_US-gwg-block.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wp i18n make-json languages/ && rename 's/(gwg-[a-zA-Z_]+-)[^\.]*(\.json)/$1gwg-block$2/' ./languages/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"scripts": { | |
"i18n-json": "wp i18n make-json languages/ && rename 's/(gwg-[a-zA-Z_]+-)[^\\.]*(\\.json)/$1gwg-block$2/' ./languages/*" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for file in `find . -name "*.po"` ; do msgfmt -o ${file/.po/.mo} $file ; done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"scripts": { | |
"i18n-mo": "for file in `find . -name \"*.po\"` ; do msgfmt -o ${file/.po/.mo} $file ; done" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"scripts": { | |
"i18n": "npm run i18n-pot && npm run i18n-json && npm run i18n-mo" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function gwg_init() { | |
load_plugin_textdomain( 'gwg', false, GWG_ESNEXT_PLUGIN_DIR . '/languages' ); | |
} | |
add_action( 'init', 'gwg_init' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( function_exists( 'wp_set_script_translations' ) ) { | |
wp_set_script_translations( 'gwg-block', 'gwg', GWG_ESNEXT_PLUGIN_DIR . '/languages' ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type='text/javascript'> | |
( function( domain, translations ) { | |
var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; | |
localeData[""].domain = domain; | |
wp.i18n.setLocaleData( localeData, domain ); | |
} )( "gwg", {"translation-revision-date":"YEAR-MO-DA HO:MI+ZONE","generator":"WP-CLI\/2.1.0","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"en","plural-forms":"nplurals=2; plural=(n != 1);"},"Get With Gutenberg - ESNext Starter":[""],"Hello editor.":[""],"Hello saved content.":[""]}}} ); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment