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

Gutenlypso: Load translations #29026

Merged
merged 4 commits into from
Dec 4, 2018
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
55 changes: 38 additions & 17 deletions client/gutenberg/editor/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ function getPostID( context ) {
return parseInt( context.params.post, 10 );
}

export const jetpackBlocki18n = ( context, next ) => {
if ( ! isEnabled( 'gutenberg/block/jetpack-preset' ) ) {
return next();
export const loadTranslations = ( context, next ) => {
const domains = [
{
name: 'default',
url: 'gutenberg',
},
];
if ( isEnabled( 'gutenberg/block/jetpack-preset' ) ) {
domains.push( {
name: 'jetpack',
url: 'jetpack-gutenberg-blocks',
} );
}

const state = context.store.getState();
Expand All @@ -55,20 +64,32 @@ export const jetpackBlocki18n = ( context, next ) => {
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' );
}
const query = domains.reduce( ( currentQuery, domain ) => {
const { name, url } = domain;
const languageFileUrl = `https://widgets.wp.com/languages/${ url }/${ localeSlug }.json?t=2`;
return {
...currentQuery,
[ name ]: () => requestFromUrl( languageFileUrl ),
};
}, {} );

waitForData( query ).then( responses => {
Object.entries( responses ).forEach( ( [ domain, { state: requestState, data } ] ) => {
if ( requestState === 'failure' ) {
debug(
`Encountered an error loading locale file for domain ${ domain } and locale ${ localeSlug }. Falling back to English.`
);
} else if ( data ) {
const localeData = {
'': {
domain,
lang: localeSlug,
},
...data.body,
};
setLocaleData( localeData, domain );
}
} );

next();
} );
Expand Down
8 changes: 4 additions & 4 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 { jetpackBlocki18n, post } from './controller';
import { loadTranslations, post } from './controller';
import config from 'config';
import { makeLayout, render as clientRender } from 'controller';

Expand All @@ -20,7 +20,7 @@ export default function() {
page(
'/gutenberg/post/:site/:post?',
siteSelection,
jetpackBlocki18n,
loadTranslations,
post,
makeLayout,
clientRender
Expand All @@ -31,7 +31,7 @@ export default function() {
page(
'/gutenberg/page/:site/:post?',
siteSelection,
jetpackBlocki18n,
loadTranslations,
post,
makeLayout,
clientRender
Expand All @@ -43,7 +43,7 @@ export default function() {
page(
'/gutenberg/edit/:customPostType/:site/:post?',
siteSelection,
jetpackBlocki18n,
loadTranslations,
post,
makeLayout,
clientRender
Expand Down