Skip to content

Commit

Permalink
Issue #7 - fix problems encountered testing the i18n-test.html templa…
Browse files Browse the repository at this point in the history
…te part
  • Loading branch information
bobbingwide committed Nov 30, 2020
1 parent ecbf1b6 commit d727658
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 47 deletions.
Binary file modified assets/FSE-i18n.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 74 additions & 3 deletions class-dom-string-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,74 @@ function translate() {

}

/**
* Translate the string.
*
* Should we maintain blanks?
*
* @param $string
*
* @return mixed|string|void
*/
function translate_string( $string ) {
echo "Locale: ";
echo $this->locale;
echo PHP_EOL;
$translated = __( $string, $this->locale );
echo "String: '";
echo $string;
echo "'";
echo PHP_EOL;
$trimmed = $this->trim( $string );
echo "Trimmed: " . $trimmed . PHP_EOL;
$translated = __( $trimmed, $this->locale );
if ( strlen( $trimmed ) < strlen( $string ) ) {
$translated = $this->detrim( $translated );
}
echo "Translated: '" . $translated . "'";
echo PHP_EOL;
return $translated;
}

/**
* Trims a string retaining left and right trimmed bits.
*
* @param $string
*
* @return string
*/
function trim( $string ) {
$this->leftness = null;
$this->rightness = null;
$trimmed = trim( $string );
$trimmed_length = strlen( $trimmed );
$string_length = strlen( $string );
$diff = $string_length - $trimmed_length;
if ( $diff ) {
$lpos = strpos( $string, $trimmed);
if ( $lpos ) {
$this->leftness=substr( $string, 0, $lpos );
}
$rlen = $diff - $lpos;
if ( $rlen ) {
$this->rightness=substr( $string, - $rlen );
}
echo "lpos:" . $lpos;
echo "rlen:" . $rlen;
echo "Trimmed left: '" . $this->leftness . "'";
echo "Trimmed right: '" . $this->rightness . "'";
}
return $trimmed;

}
function detrim( $string ) {
$detrimmed = $this->leftness;
$detrimmed .= $string;
$detrimmed .= $this->rightness;
$this->leftness = null;
$this->rightness = null;
return $detrimmed;
}

/**
*
* @param $node
Expand All @@ -66,18 +126,29 @@ function translate_string( $string ) {
[baseURI] =>
[textContent] => Hello World
)
*
* nodeType | Value | Translate
* -------- | ------ | --------
* XML_ELEMENT_NODE | 1
* XML_TEXT_NODE | 3 | Yes
* | 8 | Not necessary
*/
function add_string( $node, $value ) {
echo "Translating string:" . $value;
echo PHP_EOL;
// print_r( $node );
//print_r( $node );
if ( XML_TEXT_NODE === $node->nodeType ) {
$translated = $this->translate_string( $node->nodeValue );
echo $translated;
echo PHP_EOL;
$node->nodeValue= $translated;
//$node->textContent = $translated;
} else {
echo "NO we're not";

echo "NO we're not!";
echo "NV: " . $node->nodeValue;
echo "NT: " . $node->nodeType;
echo PHP_EOL;
}
echo PHP_EOL;

Expand Down
7 changes: 5 additions & 2 deletions class-dom-stringer.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ function extract_strings( DOMNode $node) {
echo 'T:' . $node->nodeType;
echo 'V:' . $node->nodeValue;
$this->extract_strings_from_attributes( $node );
$value = trim( $node->nodeValue);
// Trim the nodeValue. It may have leading or trailing blanks but we don't
// want to include these in the string to be translated.
// Are there languages where we shouldn't maintain leading or trailing blanks?
$value = trim( $node->nodeValue );
if ( !empty( $value ) ) {
echo PHP_EOL;
echo str_repeat( ' ', $nested );
Expand Down Expand Up @@ -324,7 +327,7 @@ function add_attribute_string( $attr, $text ) {
/**
* Add a translatable string.
*
* Only only extract the string if it's part of a text node
* Only extract the string if it's part of a text node
* or the node is null - for Gutenberg blocks.
*
* @param null|DOMNode $node
Expand Down
10 changes: 10 additions & 0 deletions class-potter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ function write_fileline( $filename ) {
return $output;
}

/**
* Writes the string.
*
* Double quotes have to be escaped but not single.
* What about new lines in the middle of the content?
*
* @param $string
* @return string
*/
function write_string( $string ) {
$string = str_replace( '"', '\"', $string );
$output = 'msgid "' . $string . '"';
$output .= PHP_EOL;
$output .= 'msgstr ""';
Expand Down
5 changes: 3 additions & 2 deletions class-theme-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ function load_text_domain( $theme ) {
$path .= '.mo'; // For the time being I don't need the -FSE suffix.
//$path = oik_path( "languages/$plugin-$locale.mo", $plugin );
$result = load_textdomain( $this->locale, $path );
echo "Result:" . $result;
echo "Path: " . $path . PHP_EOL;
echo "Result:" . $result . PHP_EOL;
if ( false === $result ) {
echo "Failed to load: " . $path;
gob();
Expand Down Expand Up @@ -125,7 +126,7 @@ function process_file( $filename, $stringer ) {

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



Expand Down
9 changes: 9 additions & 0 deletions html2la_CY.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,19 @@
$theme_files->set_locale( $locale );
$theme_files->load_text_domain( $theme );

/**
* This is just some test code.
*/
$translated = __( '404.html', $locale );
echo "Translated: ". $translated;
echo PHP_EOL;
$translated = __( 'Color', $locale );
echo $translated;
echo PHP_EOL;

$translated = __( 'core/html', $locale );
echo $translated;


$stringer = new DOM_string_updater();
$stringer->set_locale( $locale );
Expand Down
4 changes: 1 addition & 3 deletions l10n.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ function do_component( $component, $lang="en_GB" ) {
* Then try for theme.
*
* @param $component
*
* @return string
* @return array [ $component_type, $component_path ]
*/
function l10n_locate_component( $component ) {
$path = oik_path( 'languages', $component );
Expand All @@ -222,7 +221,6 @@ function l10n_locate_component( $component ) {
return [null, null];
}


/**
* Makeoik is required when we can't use makepot within npm.
*
Expand Down
82 changes: 45 additions & 37 deletions la_CY.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,33 @@
* @param string $plugin
*
*/
function la_CY( $plugin ) {
function la_CY( $plugin, $lang=null, $component_path ) {
static $c = 0;
echo "Updating $plugin .po files for selected languages" . PHP_EOL;
echo "Updating .po for: " . $plugin . PHP_EOL;
echo "Language: ". $lang . PHP_EOL;
echo "Component path:" . $component_path . PHP_EOL;

$current_locale = get_locale();
echo "Current locale: " . $current_locale . PHP_EOL;
if ( 'en_US' !== $current_locale ) {
switch_to_locale( 'en_US');
}
$current_locale = get_locale();
echo "Current locale: " . $current_locale . PHP_EOL;


$locales = la_CY_get_locales( $plugin );
$content = la_CY_load_pot( $plugin );
//$locales = la_CY_get_locales( $plugin );
$locales = [ $lang ];
$content = la_CY_load_pot( $plugin, $component_path );
la_CY_cd_working();
//print_r ( $locales );
//gob();
//var_dump( debug_backtrace() );
//bw_backtrace();
if ( $c ) {
//gob();
}
$c++;


foreach ( $locales as $new_locale ) {
global $locale;
echo "$plugin $locale $new_locale" . PHP_EOL;
$locale = $new_locale;
la_CY_load_locale( $locale );
la_CY_load_plugin_locale( $plugin, $locale );
la_CY_load_plugin_locale( $plugin, $locale, $component_path );
$outfile = la_CY_prepare_po( $plugin, $locale );

la_CY_translate( $plugin, $locale, $content, $outfile );
Expand All @@ -57,7 +62,7 @@ function la_CY( $plugin ) {


la_CY_msgfmt( $plugin, $locale );
la_CY_copytoplugin( $plugin, $locale );
la_CY_copytoplugin( $plugin, $locale, $component_path );


}
Expand All @@ -76,33 +81,35 @@ function la_CY_cd_working() {
/**
* Load the en_US .pot file
*
* We load the .pot file from the working directory
* having assumed that we've just built it ourselves.
*
* But this isn't where it's built by makepot is it?
*
* We load the .pot file from the component's source directory
*/
function la_CY_load_pot( $plugin ) {
$real_file = oik_path( "languages/$plugin.pot", $plugin );
//$real_file = "$plugin.pot" ;
echo __FUNCTION__ . ": Processing $real_file" . PHP_EOL;
$content = file( $real_file );
return( $content );
function la_CY_load_pot( $component, $component_path ) {
$real_file = $component_path .'/' . $component . '.pot';
echo __FUNCTION__ . ": Processing: $real_file" . PHP_EOL;
$content = file( $real_file );
return $content;
}

/**
* Load the plugin's current language file
* Load the component's current language file
*
* We assume that the textdomain is the same as the plugin
* AND that the languages files are stored in the plugin's languages folder
* We assume that the textdomain is the same as the component
* AND that the languages files are stored in the component's languages folder
*
* Note: We have to unload any previous language version first
*
*/
function la_CY_load_plugin_locale( $plugin, $locale ) {
echo "Loading the translation file for the plugin" . PHP_EOL;
unload_textdomain( $plugin );
$result = load_plugin_textdomain( $plugin, false, "$plugin/languages" );
function la_CY_load_plugin_locale( $component, $locale, $component_path ) {
echo "Loading the translation file for the component:" . PHP_EOL;
unload_textdomain( $component );
$path = $component_path;
$path .= '/languages/';
$path .= $component;
$path .= '-';
$path .= $locale;
$path .= '.mo';

$result = load_textdomain( $component, $path );
print_r( $result );
}

Expand All @@ -114,8 +121,8 @@ function la_CY_trace_l10n() {
/**
* Get the Last translator and language team
* `
* "Last-Translator: Rémy Perona <[email protected]>\n"
* "Language-Team: Rémy Perona <[email protected]>\n"
* "Last-Translator: R�my Perona <[email protected]>\n"
* "Language-Team: R�my Perona <[email protected]>\n"
* `
*
* @TODO Develop logic using get_plugin_data() or whatever the API is
Expand All @@ -138,15 +145,16 @@ function la_CY_msgfmt( $plugin, $locale ) {
do_msgfmt( $plugin, $locale );
} else {
echo "You'll need to run msgfmt manually for $plugin $locale";
gob();
}
}

/**
* Copy the working files to the plugin
*/
function la_CY_copytoplugin( $plugin, $locale ) {
if ( function_exists( "do_copytoplugin" ) ) {
do_copytoplugin( $plugin, $locale );
function la_CY_copytoplugin( $plugin, $locale, $component_path ) {
if ( function_exists( "do_copytocomponent" ) ) {
do_copytocomponent( $plugin, $locale, $component_path );
} else {
echo "You'll need to copy files manually for $plugin $locale";
}
Expand Down

0 comments on commit d727658

Please sign in to comment.