From f73ddb454d13fd2e4803a899ba428c4c7540b0b3 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Tue, 6 Nov 2018 10:18:13 +0200 Subject: [PATCH 1/4] Gutenberg: Load Jetpack block translations in Calypso synchronously --- client/gutenberg/editor/controller.js | 30 +++++++++++++++++++++++++++ client/gutenberg/editor/index.js | 21 ++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/client/gutenberg/editor/controller.js b/client/gutenberg/editor/controller.js index 070ccc6c0e454..3010d54aa5273 100644 --- a/client/gutenberg/editor/controller.js +++ b/client/gutenberg/editor/controller.js @@ -1,13 +1,19 @@ /** @format */ + /** * External dependencies */ import React from 'react'; +import debug from 'debug'; +import request from 'superagent'; +import { isEnabled } from 'config'; import { has, uniqueId } from 'lodash'; +import { setLocaleData } from '@wordpress/i18n'; /** * Internal dependencies */ +import getCurrentLocaleSlug from 'state/selectors/get-current-locale-slug'; import { getCurrentUserId } from 'state/current-user/selectors'; import { setAllSitesSelected } from 'state/ui/actions'; import { getSelectedSiteId, getSelectedSiteSlug } from 'state/ui/selectors'; @@ -35,6 +41,30 @@ function getPostID( context ) { return parseInt( context.params.post, 10 ); } +export const jetpackBlocki18n = ( context, next ) => { + if ( ! isEnabled( 'gutenberg/block/jetpack-preset' ) ) { + return next(); + } + + const state = context.store.getState(); + const localeSlug = getCurrentLocaleSlug( state ); + const languageFileUrl = + 'https://widgets.wp.com/languages/jetpack-gutenberg-blocks/' + localeSlug + '.json'; + + request.get( languageFileUrl ).end( ( error, response ) => { + if ( error ) { + debug( + 'Encountered an error loading locale file for ' + localeSlug + '. Falling back to English.' + ); + return next(); + } + + setLocaleData( response.body, 'jetpack' ); + + next(); + } ); +}; + function waitForSelectedSiteId( context ) { return new Promise( resolve => { const unsubscribe = context.store.subscribe( () => { diff --git a/client/gutenberg/editor/index.js b/client/gutenberg/editor/index.js index 51068618be094..92e2cedea21f9 100644 --- a/client/gutenberg/editor/index.js +++ b/client/gutenberg/editor/index.js @@ -8,7 +8,7 @@ import page from 'page'; * Internal dependencies */ import { siteSelection, sites } from 'my-sites/controller'; -import { post } from './controller'; +import { jetpackBlocki18n, post } from './controller'; import config from 'config'; import { makeLayout, render as clientRender } from 'controller'; @@ -17,11 +17,25 @@ export default function() { page( '/gutenberg', '/gutenberg/post' ); page( '/gutenberg/post', siteSelection, sites, makeLayout, clientRender ); - page( '/gutenberg/post/:site/:post?', siteSelection, post, makeLayout, clientRender ); + page( + '/gutenberg/post/:site/:post?', + siteSelection, + jetpackBlocki18n, + post, + makeLayout, + clientRender + ); page( '/gutenberg/post/:site?', siteSelection, makeLayout, clientRender ); page( '/gutenberg/page', siteSelection, sites, makeLayout, clientRender ); - page( '/gutenberg/page/:site/:post?', siteSelection, post, makeLayout, clientRender ); + page( + '/gutenberg/page/:site/:post?', + siteSelection, + jetpackBlocki18n, + post, + makeLayout, + clientRender + ); page( '/gutenberg/page/:site?', siteSelection, makeLayout, clientRender ); if ( config.isEnabled( 'manage/custom-post-types' ) ) { @@ -29,6 +43,7 @@ export default function() { page( '/gutenberg/edit/:customPostType/:site/:post?', siteSelection, + jetpackBlocki18n, post, makeLayout, clientRender From b692e5521be97caaeff56f507cfbe0b0254fe9d4 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Tue, 6 Nov 2018 14:07:16 +0200 Subject: [PATCH 2/4] Do not localize English --- client/gutenberg/editor/controller.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/client/gutenberg/editor/controller.js b/client/gutenberg/editor/controller.js index 3010d54aa5273..ff6e736e17d89 100644 --- a/client/gutenberg/editor/controller.js +++ b/client/gutenberg/editor/controller.js @@ -48,6 +48,12 @@ export const jetpackBlocki18n = ( context, next ) => { const state = context.store.getState(); const localeSlug = getCurrentLocaleSlug( state ); + + // We don't need to localize English + if ( localeSlug === 'en' ) { + return next(); + } + const languageFileUrl = 'https://widgets.wp.com/languages/jetpack-gutenberg-blocks/' + localeSlug + '.json'; From 685a69bbf4a13ae970fa82717046c51bc5deae05 Mon Sep 17 00:00:00 2001 From: Marin Atanasov Date: Tue, 27 Nov 2018 22:39:59 +0200 Subject: [PATCH 3/4] Replace superagent with waitForData and requestFromUrl --- client/gutenberg/editor/controller.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/client/gutenberg/editor/controller.js b/client/gutenberg/editor/controller.js index ff6e736e17d89..e3802dc357e79 100644 --- a/client/gutenberg/editor/controller.js +++ b/client/gutenberg/editor/controller.js @@ -5,7 +5,6 @@ */ import React from 'react'; import debug from 'debug'; -import request from 'superagent'; import { isEnabled } from 'config'; import { has, uniqueId } from 'lodash'; import { setLocaleData } from '@wordpress/i18n'; @@ -19,6 +18,8 @@ import { setAllSitesSelected } from 'state/ui/actions'; import { getSelectedSiteId, getSelectedSiteSlug } from 'state/ui/selectors'; import { EDITOR_START } from 'state/action-types'; import { initGutenberg } from './init'; +import { requestFromUrl } from 'state/data-getters'; +import { waitForData } from 'state/data-layer/http-data'; function determinePostType( context ) { if ( context.path.startsWith( '/gutenberg/post/' ) ) { @@ -54,18 +55,20 @@ export const jetpackBlocki18n = ( context, next ) => { return next(); } - const languageFileUrl = - 'https://widgets.wp.com/languages/jetpack-gutenberg-blocks/' + localeSlug + '.json'; + const languageFileUrl = `https://widgets.wp.com/languages/jetpack-gutenberg-blocks/${ localeSlug }.json`; - request.get( languageFileUrl ).end( ( error, response ) => { - if ( error ) { + waitForData( { + translations: () => requestFromUrl( languageFileUrl ), + } ).then( ( { translations: { state: requestState, data } } ) => { + if ( requestState === 'failure' ) { debug( - 'Encountered an error loading locale file for ' + localeSlug + '. Falling back to English.' + `Encountered an error loading locale file for ${ localeSlug }. Falling back to English.` ); - return next(); } - setLocaleData( response.body, 'jetpack' ); + if ( data ) { + setLocaleData( data.body, 'jetpack' ); + } next(); } ); From 4f8f4cc19f2ae4b260f3b05da48081354882d540 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 3 Dec 2018 12:02:25 +0100 Subject: [PATCH 4/4] Use config( 'i18n_default_locale_slug' ) instead of 'en' --- client/gutenberg/editor/controller.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/gutenberg/editor/controller.js b/client/gutenberg/editor/controller.js index e3802dc357e79..78bcd4b850188 100644 --- a/client/gutenberg/editor/controller.js +++ b/client/gutenberg/editor/controller.js @@ -5,7 +5,7 @@ */ import React from 'react'; import debug from 'debug'; -import { isEnabled } from 'config'; +import config, { isEnabled } from 'config'; import { has, uniqueId } from 'lodash'; import { setLocaleData } from '@wordpress/i18n'; @@ -51,7 +51,7 @@ export const jetpackBlocki18n = ( context, next ) => { const localeSlug = getCurrentLocaleSlug( state ); // We don't need to localize English - if ( localeSlug === 'en' ) { + if ( localeSlug === config( 'i18n_default_locale_slug' ) ) { return next(); }