Permalink
Browse files

Merge pull request #905 from GlotPress/903-strings-encoding

903: Fix strings file encoding
  • Loading branch information...
toolstack committed Jul 11, 2018
2 parents 2dbe9b5 + 77012cb commit a824d61617d32b50bb0bb719a009416705033da5
@@ -1,15 +1,52 @@
<?php
/**
* GlotPress Format Mac OSX / iOS Strings Translate class
*
* @since 1.0.0
*
* @package GlotPress
*/
/**
* Format class used to support Mac OS X / iOS Translate strings file format.
*
* @since 1.0.0
*/
class GP_Format_Strings extends GP_Format {
/**
* Name of file format, used in file format dropdowns.
*
* @since 1.0.0
*
* @var string
*/
public $name = 'Mac OS X / iOS Strings File (.strings)';
public $extension = 'strings';
public $exported = '';
/**
* File extension of the file format, used to autodetect formats and when creating the output file names.
*
* @since 1.0.0
*
* @var string
*/
public $extension = 'strings';
/**
* Generates a string the contains the $entries to export in the strings file format.
*
* @since 1.0.0
*
* @param GP_Project $project The project the strings are being exported for, not used
* in this format but part of the scaffold of the parent object.
* @param GP_Locale $locale The locale object the strings are being exported for. not used
* in this format but part of the scaffold of the parent object.
* @param GP_Translation_Set $translation_set The locale object the strings are being
* exported for. not used in this format but part
* of the scaffold of the parent object.
* @param GP_Translation $entries The entries to export.
* @return string The exported strings string.
*/
public function print_exported_file( $project, $locale, $translation_set, $entries ) {
$prefix = pack( 'CC', 0xff, 0xfe ); // Add BOM
$result = '';
$result .= '/* Translation-Revision-Date: ' . GP::$translation->last_modified( $translation_set ) . "+0000 */\n";
@@ -41,9 +78,17 @@ public function print_exported_file( $project, $locale, $translation_set, $entri
$result .= "/* $comment */\n\"$original\" = \"$translation\";\n\n";
}
return $prefix . mb_convert_encoding( $result, 'UTF-16LE' );
return $result;
}
/**
* Reads a set of original strings from a strings file.
*
* @since 1.0.0
*
* @param string $file_name The name of the uploaded strings file.
* @return Translations|bool The extracted originals on success, false on failure.
*/
public function read_originals_from_file( $file_name ) {
$entries = new Translations;
$file = file_get_contents( $file_name );
@@ -52,7 +97,16 @@ public function read_originals_from_file( $file_name ) {
return false;
}
$file = mb_convert_encoding( $file, 'UTF-8', 'UTF-16LE' );
/**
* Check to see if the input file is UTF-16LE encoded, if so convert it to UTF-8.
*
* Note, Apple recommends UTF-8 but some of their tools (like genstrings) export
* UTF-16LE (or BE, but GP has never supported that) so to remain backwards
* compatible we support both for importing, but we only export UTF-8.
*/
if ( mb_check_encoding( $file, 'UTF-16LE' ) ) {
$file = mb_convert_encoding( $file, 'UTF-8', 'UTF-16LE' );
}
$context = $comment = null;
$lines = explode( "\n", $file );
@@ -86,7 +140,15 @@ public function read_originals_from_file( $file_name ) {
return $entries;
}
/**
* Sorts the translation entries based on the context attribute.
*
* @since 1.0.0
*
* @param string $a First string to compare.
* @param string $b Second string to compare.
* @return int +1 or -1 based on the order to sort.
*/
private function sort_entries( $a, $b ) {
if ( $a->context == $b->context ) {
return 0;
@@ -95,10 +157,26 @@ private function sort_entries( $a, $b ) {
return ( $a->context > $b->context ) ? +1 : -1;
}
/**
* Strips any escaping from a string.
*
* @since 1.0.0
*
* @param string $string The string to strip escapes from.
* @return string The unescaped string.
*/
private function unescape( $string ) {
return stripcslashes( $string );
}
/**
* Adds escaping to a string.
*
* @since 1.0.0
*
* @param string $string The string to add escapes to.
* @return string The escaped string.
*/
private function escape( $string ) {
return addcslashes( $string, '"\\/' );
}
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
@@ -35,11 +35,9 @@ function test_export() {
}
$file_contents = file_get_contents( GP_DIR_TESTDATA . '/translation.strings' );
$file_contents = mb_convert_encoding( $file_contents, 'UTF-8', 'UTF-16LE' );
$file_contents = str_replace( '[GP VERSION]', GP_VERSION, $file_contents );
$exported = $this->strings->print_exported_file( $project, $locale, $set, $entries_for_export );
$exported = mb_convert_encoding( $exported, 'UTF-8', 'UTF-16' );
$this->assertEquals( $file_contents, $exported );
}
@@ -58,6 +56,20 @@ function test_read_originals() {
}
}
function test_read_originals_from_UTF16LE() {
$translations = $this->strings->read_originals_from_file( GP_DIR_TESTDATA . '/originals.16le.strings' );
$this->assertEquals( count( $this->entries ), count( $translations->entries ), 'number of read originals is different from the expected' );
foreach( $this->entries as $sample ) {
list( $context, $original, $translation, $comment ) = $sample;
$translatable_entry = new Translation_Entry( array('singular' => $original, 'context' => $context, 'extracted_comments' => $comment ) );
$entry = $translations->translate_entry( $translatable_entry );
$this->assertEquals( $original, $entry->singular );
$this->assertEquals( $context, $entry->context );
$this->assertEquals( $comment, $entry->extracted_comments );
}
}
function test_read_translations() {
$stubbed_originals = array();

0 comments on commit a824d61

Please sign in to comment.