Skip to content

Commit

Permalink
Issue #7 - Stage 4. Just enough code to test translation in HTML text
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Nov 28, 2020
1 parent be3329c commit b75b8eb
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 168 deletions.
42 changes: 35 additions & 7 deletions class-dom-string-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

class DOM_string_updater extends DOM_Stringer {

private $locale;

function __construct() {
parent::__construct();
}

function set_locale( $locale ) {
$this->locale = $locale;
}

function saveHTML() {
echo 'Writing HTML';
echo PHP_EOL;
Expand All @@ -26,6 +32,11 @@ function translate() {

}

function translate_string( $string ) {
$translated = __( $string, $this->locale );
return $translated;
}

/**
*
* @param $node
Expand Down Expand Up @@ -58,11 +69,21 @@ function add_string( $node, $value ) {
echo PHP_EOL;
print_r( $node );
if ( XML_TEXT_NODE === $node->nodeType ) {
$node->nodeValue="Hlelo Wrold";
$node->nodeValue= $this->translate_string( $node->nodeValue );
}

}

/**
* Updates the attribute string value for the selected locale.
*
* This is for the nodes.
* When dealing with blocks we have a different method.
*/
function add_attribute_string( $attr, $text ) {
gob();
}

/**
* Overrides extract strings to translate them
* @param $node
Expand Down Expand Up @@ -102,12 +123,19 @@ function extract_strings_from_attributes( $node ) {
//print_r( $node );
//print_r( $node->attributes);
echo $node->attributes->length;
$attribute = $node->attributes->item(0);
echo $attribute->name;
echo $attribute->value;
// $attribute->value = "derf";
//print_r( $attribute );
$node->setAttribute ( $attribute->name , "derf" );
echo PHP_EOL;
for ( $item=0; $item < $node->attributes->length; $item ++ ) {
$attribute=$node->attributes->item( $item );
echo 'A#:' . $item;
echo 'AN:' . $attribute->name;
echo 'AV:' . $attribute->value;
echo PHP_EOL;
if ( $this->isAttrTranslatable( $attribute->name ) ) {
$translated=$this->translate_string( $attribute_value );
$node->setAttribute( $attribute->name, $translated );
//$this->add_attribute_string( $attribute->name, $attribute->value );
}
}
}
}

Expand Down
28 changes: 26 additions & 2 deletions class-dom-stringer.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,26 @@ class DOM_Stringer {
public $nodeName = [];

/**
*
* DOM_Stringer constructor.
*/

function __construct() {
$this->dom_doc = new DOMDocument();
$this->setUpNotNeeded();
}

/**
* Sets the source filename for reporting in the .pot file
* @param $filename
*/
function set_source_filename( $filename ) {
$this->source_filename = $filename;
}

/**
* Sets the block name ( eg core/template-part ).
*
* @param $blockName
*/
function set_blockName( $blockName ) {
$this->blockName = $blockName;
}
Expand All @@ -79,10 +87,18 @@ function set_nodeName ( $nodeName ) {
$this->nodeName[] = $nodeName;
}

/**
* Pops a node off the tree.
*/
function pop_nodeName() {
array_pop( $this->nodeName );
}

/**
* Returns a string representation of the node tree.
*
* @return string|string[]
*/
function get_nodeNameTree() {
$tree = implode( '>', $this->nodeName );
$tree = str_replace( '#document>', '', $tree );
Expand Down Expand Up @@ -161,6 +177,14 @@ function get_strings( $blockName, $html ) {
echo PHP_EOL;
}

/**
* Extracts strings from the nodes.
*
* Recursive processing to extract / update strings. *
* Update is performed by the extending class DOM_String_Updater.
*
* @param DOMNode $node
*/
function extract_strings( DOMNode $node) {
static $nested;
$nested++;
Expand Down
212 changes: 212 additions & 0 deletions class-theme-files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php

/**
* Class Theme_Files
* @package oik-i18n
* @copyright (C) Copyright Bobbing Wide 2020
*
* Implements the base class to process all the template and template part theme files for a Full Site Editing theme
* in order to translate the text in the HTML files and blocks into another locale ( la_CY ).
*
* This is extended by Theme_Files_Updater which
* applies the translations when processing:
* - add_string
* - add_attribute_string
*
* After translation the new theme files are written to the theme's languages folder
* in a subdirectory for the chosen locale - la_CY
* where la is the language code eg en
* and CY is the country code eg GB
*
* For the bb_BB test language the folders would be:
*
* - theme/bb_BB/block-templates
* - theme/bb_BB/block-template-parts
*
*/

class Theme_Files {
/**
* @var string $theme
*/
private $theme;

function __construct() {

}

function get_theme_dir( $theme ) {
$dir =get_stylesheet_directory();
$theme_dir=dirname( $dir ) . '/' . $theme;
echo $theme_dir;

return $theme_dir;
}

/**
* Loads the text domain for the FSE theme's HTML files.
*
* @param $theme
* @param string $locale
*/
function load_text_domain( $theme, $locale='bb_BB' ) {
$path = $this->get_theme_dir( $theme );
$path .= '/languages/';
$path .= $theme;
$path .= '-';
$path .= $locale;
$path .= '.mo'; // For the time being I don't need the -FSE suffix.
//$path = oik_path( "languages/$plugin-$locale.mo", $plugin );
$result = load_textdomain( $locale, $path );
echo "Result:" . $result;
if ( false === $result ) {
echo "Failed to load: " . $path;
gob();
}
}

function list_all_templates_and_parts( $theme ) {
$this->theme = $theme;
$theme_dir =get_theme_dir( $theme );
$template_files=glob( $theme_dir . '/block-templates/*.html' );
$template_parts=glob( $theme_dir . '/block-template-parts/*.html' );
//print_r( $template_files );
//print_r( $template_parts );
$files=array_merge( $template_files, $template_parts );

return $files;
}

/**
* Processes all the theme files.
*
* @param $files
* @param $stringer
*/
function process_theme_files( $files, $stringer ) {
echo "Processing:" . count( $files );
echo PHP_EOL;
$count=0;
foreach ( $files as $filename ) {
$count ++;
echo $count;
echo ' ';
echo $filename;
echo PHP_EOL;
$this->process_file( $filename, $stringer );
}
}

/**
* Process a single .html file to extract the translatable strings.
*
* Uses Gutenberg to parse the content into individual blocks.
* PS. I've got a block recreation routine in oik-clone.
*
* @param $filename
*/
function process_file( $filename, $stringer ) {
$html =file_get_contents( $filename );
$parser =new WP_Block_Parser();
$blocks =$parser->parse( $html );
$count =0;
$basename=basename( $filename );
echo $basename;
echo PHP_EOL;

$stringer->set_source_filename( $basename );
$this->process_blocks( $blocks, $stringer );

}

/**
* Recursively process inner blocks.
*
* Assume this doesn't go recursive since we're not loading other files.
*
* @param $block
*/
function process_blocks( $blocks, $stringer ) {
static $count=0;
foreach ( $blocks as $block ) {
//process_block( $block, $stringer );
$count ++;
echo PHP_EOL;
echo "Block: " . $count;
echo $block['blockName'];
echo PHP_EOL;

extract_strings_from_block_attributes( $block, $stringer );

if ( ! empty( $block['innerHTML'] ) ) {
$strings=$stringer->get_strings( $block['blockName'], $block['innerHTML'] );
}
if ( ! empty( $block['innerBlocks'] ) ) {
$this->process_blocks( $block['innerBlocks'], $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'] );
$stringer->set_blockName( $block['blockName'] );
foreach ( $block['attrs'] as $key=>$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'] );
* $isTranslatable = isset( $translatable[ $key ] ) ? true : false;
* if ( isset( $not_translatable[ $key] ) ) {
* $isTranslatable = false;
* }
* return $isTranslatable;
* }
*/

function write_pot_file( $theme, $stringer ) {
$strings=$stringer->get_all_strings();
$potter =new Potter();
$potter->set_pot_filename( $theme . '.pot' );
$potter->set_project( $theme );
$output=$potter->write_header();
$output.=$potter->write_strings( $strings );
//echo $output;
replace_pot_file( $theme, $output );

}

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

30 changes: 28 additions & 2 deletions html2la_CY.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,39 @@
*/
require_once 'class-dom-stringer.php';
require_once 'class-dom-string-updater.php';
require_once 'theme-files.php';
require_once 'class-theme-files.php';

$html = '<p class="fred">index.html<b>!</b></p>';

$theme = 'fizzie';
$locale = 'bb_BB';

$theme_files = new Theme_Files();
$theme_files->load_text_domain( $theme, $locale );
/*
global $l10n;
//print_r( $l10n );
print_r( $l10n[$locale] );
foreach ( [ 'en_US', 'en_GB', 'bb_BB'] as $locale) {
//$switched=switch_to_locale( $locale );
//if ( ! $switched ) {
// echo "Didn't switch";
//}
echo $locale;
$hw=__( 'index.html', $locale );
echo $hw;
echo PHP_EOL;
}
gob();
*/

$html = '<p class="fred">Hello World<b>!</b></p>';


$stringer = new DOM_string_updater();
$stringer->loadHTML( $html );
$stringer->set_locale( $locale );
$stringer->translate();
$html_after = $stringer->saveHTML();
echo 'HTML:';
Expand Down
Loading

0 comments on commit b75b8eb

Please sign in to comment.