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

Gutenberg: Load Jetpack block translations in Calypso synchronously #28304

Merged
merged 4 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 39 additions & 0 deletions client/gutenberg/editor/controller.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
/** @format */

/**
* External dependencies
*/
import React from 'react';
import debug from 'debug';
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';
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/' ) ) {
Expand All @@ -35,6 +42,38 @@ function getPostID( context ) {
return parseInt( context.params.post, 10 );
}

export const jetpackBlocki18n = ( context, next ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about calling this function here https://github.com/Automattic/wp-calypso/blob/master/client/gutenberg/editor/init.js#L25 before the extensions are loaded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I think I'll pass because:

  • We need access to the state, in order to fetch the user's current locale.
  • For the suggested approach, we need to pause the flow and continue after translations are loaded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could pass the state (or part of it) to initGutenberg from controller, but I agree that we shouldn't do it here because of your second point.

if ( ! isEnabled( 'gutenberg/block/jetpack-preset' ) ) {
return next();
}

const state = context.store.getState();
const localeSlug = getCurrentLocaleSlug( state );

// We don't need to localize English
if ( localeSlug === 'en' ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to use config( 'i18n_default_locale_slug' ) here

return next();
}

const languageFileUrl = `https://widgets.wp.com/languages/jetpack-gutenberg-blocks/${ localeSlug }.json`;

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.`
);
}

if ( data ) {
setLocaleData( data.body, 'jetpack' );
}

next();
} );
};

function waitForSelectedSiteId( context ) {
return new Promise( resolve => {
const unsubscribe = context.store.subscribe( () => {
Expand Down
21 changes: 18 additions & 3 deletions client/gutenberg/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -17,18 +17,33 @@ 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' ) ) {
page( '/gutenberg/edit/:customPostType', siteSelection, sites, makeLayout, clientRender );
page(
'/gutenberg/edit/:customPostType/:site/:post?',
siteSelection,
jetpackBlocki18n,
post,
makeLayout,
clientRender
Expand Down