Skip to content

Commit

Permalink
Issue #7 - separate add_attribute_string from add_string. Write the o…
Browse files Browse the repository at this point in the history
…utput file to the theme's languages folder
  • Loading branch information
bobbingwide committed Nov 28, 2020
1 parent b271f84 commit 593c12d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 9 additions & 2 deletions class-dom-stringer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function extract_strings_from_attributes( $node ) {
//print_r( $attribute );
//$node->setAttribute( $attribute->name, "derf" );
if ( $this->isAttrTranslatable( $attribute->name )) {
$this->add_string( null, $attribute->value );
$this->add_attribute_string( $attribute->name, $attribute->value );
}
}
}
Expand Down Expand Up @@ -282,9 +282,16 @@ function getNotTranslatableAttrs() {

/**
* Adds a translatable attr.
*
* Attributes are different from Text strings.
* We don't need to check the node type
* It'd be nice to record the attribute name
*
*/
function add_attribute_string( $attr, $text ) {

if ( ! isset( $this->strings[ $text ] ) ) {
$this->strings[ $text ]=$this->source_filename . ' ' . $this->blockName . ' ' . $this->get_nodeNameTree() . ' ' . $attr;
}
}

/**
Expand Down
23 changes: 20 additions & 3 deletions theme-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,25 @@ function process_blocks( $blocks, $stringer ) {
}
}

/**
* Extracts strings for translatable attributes.
*
* @TODO Implement recursion for nested attributes.
* @param $block
* @param $stringer
*/
function extract_strings_from_block_attributes( $block, $stringer ) {
//print_r( $block );
print_r( $block['attrs'] );
//print_r( $block['attrs'] );
$stringer->set_blockName( $block['blockName'] );
foreach ( $block['attrs'] as $key => $value ) {
if ( isAttrTranslatable( $key ) ) {
$stringer->add_string( null, $value );
if ( $stringer->isAttrTranslatable( $key ) ) {
$stringer->add_attribute_string( $key, $value );
}
}
}

/** See the class
function isAttrTranslatable( $key ) {
$translatable = array_flip( [ 'label' ] );
$not_translatable = array_flip( ['class', 'className', 'slug', 'ID', 'ref'] );
Expand All @@ -108,6 +116,7 @@ function isAttrTranslatable( $key ) {
}
return $isTranslatable;
}
*/

function write_pot_file( $theme, $stringer ) {
$strings = $stringer->get_all_strings();
Expand All @@ -125,12 +134,20 @@ function replace_pot_file( $theme, $contents ) {
$filename = get_theme_dir( $theme );
$filename .= '/languages/' ;
$filename .= $theme;
$filename .= '-FSE'; // Append a suffix so we know these are FSE strings
$filename .= '.pot';
echo "Writing: " ;
echo $filename;
echo PHP_EOL;
echo $contents;
echo PHP_EOL;
$written = file_put_contents( $filename, $contents );
if ( $written !== strlen( $contents ) ) {
echo "File was badly written";
echo "Wrote:" . $written;
echo "Expected:" . strlen( $contents );
}


}

0 comments on commit 593c12d

Please sign in to comment.