Skip to content

Commit

Permalink
Provide i18n by moving separator attributes to PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
douglas-johnson committed Sep 22, 2023
1 parent c2891b8 commit ad76274
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
16 changes: 0 additions & 16 deletions build/blocks/block-coauthors/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,12 @@
}
},
"attributes": {
"separator": {
"type": "string",
"default": ", "
},
"lastSeparator": {
"type": "string",
"default": " and "
},
"layout": {
"type": "object",
"default": {
"type": "inline"
}
},
"prefix": {
"type": "string",
"default": "By "
},
"suffix": {
"type": "string",
"default": ""
},
"textAlign": {
"type": "string"
}
Expand Down
49 changes: 49 additions & 0 deletions php/blocks/block-coauthors/class-block-coauthors.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Block_CoAuthors {
* @since 3.6.0
*/
public static function register_block(): void {

add_filter( 'block_type_metadata_settings', array( __CLASS__, 'separator_internationalization' ), 10, 2 );

register_block_type(
dirname( COAUTHORS_PLUS_FILE ) . '/build/blocks/block-coauthors',
array(
Expand All @@ -30,6 +33,52 @@ public static function register_block(): void {
);
}

/**
* Separator Internationalization
* Provide i18n for the default prefix, separators and suffix attributes during block registration.
*
* @param array $settings Array of determined settings for registering a block type.
* @param array $metadata Metadata provided for registering a block type.
* @return array Updated settings that include internationalized attributes.
*/
public static function separator_internationalization( array $settings, array $metadata ): array {

if ( ! array_key_exists( 'name', $metadata ) ) {
return $settings;
}

if ( 'cap/coauthors' !== $metadata['name'] ) {
return $settings;
}

return array_merge(
$settings,
array(
'attributes' => array_merge(
$settings['attributes'],
array(
'prefix' => array(
'type' => 'string',
'default' => apply_filters( 'coauthors_default_before', __( 'By ', 'co-authors-plus' ) ),
),
'separator' => array(
'type' => 'string',
'default' => apply_filters( 'coauthors_default_between', ', ' ),
),
'lastSeparator' => array(
'type' => 'string',
'default' => apply_filters( 'coauthors_default_between_last', __( ' and ', 'co-authors-plus' ) ),
),
'suffix' => array(
'type' => 'string',
'default' => apply_filters( 'coauthors_default_after', '' ),
),
)
),
)
);
}

/**
* Render Block
*
Expand Down
16 changes: 0 additions & 16 deletions src/blocks/block-coauthors/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,12 @@
}
},
"attributes": {
"separator": {
"type": "string",
"default": ", "
},
"lastSeparator": {
"type": "string",
"default": " and "
},
"layout": {
"type": "object",
"default": {
"type": "inline"
}
},
"prefix": {
"type": "string",
"default": "By "
},
"suffix": {
"type": "string",
"default": ""
},
"textAlign": {
"type": "string"
}
Expand Down

0 comments on commit ad76274

Please sign in to comment.