Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Handle translations in lazy loaded files by injecting them into the p…
Browse files Browse the repository at this point in the history
…age (#4897)

* Formalise chunk names

* register_chunk_translations()

* Rename handle

* Include fileSuffix

* Remove .chunk prefix- it broke the vendors chunk
  • Loading branch information
mikejolley authored Oct 11, 2021
1 parent 6f97012 commit 7e340cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/BlockTypes/AbstractBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ protected function register_block_type_assets() {
}
}

/**
* Injects Chunk Translations into the page so translations work for lazy loaded components.
*
* The chunk names are defined when creating lazy loaded components using webpackChunkName.
*
* @param string[] $chunks Array of chunk names.
*/
protected function register_chunk_translations( $chunks ) {
foreach ( $chunks as $chunk ) {
$handle = 'wc-blocks-' . $chunk . '-chunk';
$this->asset_api->register_script( $handle, $this->asset_api->get_block_asset_build_path( $chunk ), [], true );
wp_add_inline_script(
$this->get_block_type_script( 'handle' ),
wp_scripts()->print_translations( $handle, false ),
'before'
);
wp_deregister_script( $handle );
}
}

/**
* Registers the block type with WordPress.
*/
Expand Down
25 changes: 25 additions & 0 deletions src/BlockTypes/AllProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,29 @@ protected function enqueue_data( array $attributes = [] ) {
$this->asset_data_registry->add( 'max_rows', wc_get_theme_support( 'product_blocks::max_rows', 6 ), true );
$this->asset_data_registry->add( 'default_rows', wc_get_theme_support( 'product_blocks::default_rows', 3 ), true );
}

/**
* Register script and style assets for the block type before it is registered.
*
* This registers the scripts; it does not enqueue them.
*/
protected function register_block_type_assets() {
parent::register_block_type_assets();
$this->register_chunk_translations(
[
'atomic-block-components/price',
'atomic-block-components/image',
'atomic-block-components/title',
'atomic-block-components/rating',
'atomic-block-components/button',
'atomic-block-components/summary',
'atomic-block-components/sale-badge',
'atomic-block-components/sku',
'atomic-block-components/category-list',
'atomic-block-components/tag-list',
'atomic-block-components/stock-indicator',
'atomic-block-components/add-to-cart',
]
);
}
}

0 comments on commit 7e340cb

Please sign in to comment.