Skip to content

Commit

Permalink
Issue #7 - Stage 4. Save the updated files the languages/locale folder
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Nov 29, 2020
1 parent 655f82f commit aef4b58
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 11 deletions.
55 changes: 52 additions & 3 deletions class-theme-files-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,66 @@ function process_file( $filename, $stringer ) {
$this->save_file( $filename, $stringer );
}

/**
* Reconstitute the file from the blocks and html
*/
/**
* Reconstitutes the file from the blocks and html.
*/
function save_file( $filename, $stringer) {
echo "Saving file:" . $filename;
echo PHP_EOL;
$blocks_reformer = new Blocks_Reformer();
$output = $blocks_reformer->reform_blocks( $this->blocks, $stringer );
echo $output;
echo PHP_EOL;
$this->write_locale_file( $filename, $output );
}

/**
* Returns the locale directory.
*
* Without a trailing slash.
* @return string
*/
function get_locale_dir() {
$locale_dir = $this->get_theme_dir( $this->theme );
$locale_dir .= '/languages/';
$locale_dir .= $this->locale;
return $locale_dir;
}

function get_locale_filepath( $filename ) {
echo $filename . PHP_EOL;
$filepath = $this->get_locale_dir();

$filepath .= '/';
$filepath .= basename( dirname( $filename ) );

//if ( !file_exists( $filepath )) {
// echo "Creating locale directory: " . $filepath . PHP_EOL;
wp_mkdir_p( $filepath );

$filepath .= '/';
$filepath .= basename( $filename );
echo "Locale file: ";
echo $filepath;
echo PHP_EOL;
return $filepath;
}

/**
* Writes the localized file.
*
* @param $filename
* @param $contents
*/
function write_locale_file( $filename, $contents ) {
$filepath = $this->get_locale_filepath( $filename );
$written = file_put_contents( $filepath, $contents );
if ( $written !== strlen( $contents ) ) {
echo "File was badly written";
echo "Wrote:" . $written;
echo "Expected:" . strlen( $contents );
gob();
}
}

}
17 changes: 10 additions & 7 deletions class-theme-files.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,40 @@ class Theme_Files {
/**
* @var string $theme
*/
private $theme;
protected $theme;

protected $blocks = null;

protected $locale = null;

function __construct() {
}

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

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

echo "Theme dir: " . $theme_dir . PHP_EOL;
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' ) {
function load_text_domain( $theme ) {
$path = $this->get_theme_dir( $theme );
$path .= '/languages/';
$path .= $theme;
$path .= '-';
$path .= $locale;
$path .= $this->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 );
$result = load_textdomain( $this->locale, $path );
echo "Result:" . $result;
if ( false === $result ) {
echo "Failed to load: " . $path;
Expand Down
3 changes: 2 additions & 1 deletion html2la_CY.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
$locale = oik_batch_query_value_from_argv( 2, 'bb_BB' );

$theme_files = new Theme_Files_Updater();
$theme_files->load_text_domain( $theme, $locale );
$theme_files->set_locale( $locale );
$theme_files->load_text_domain( $theme );

$translated = __( '404.html', $locale );
echo $translated;
Expand Down

0 comments on commit aef4b58

Please sign in to comment.