-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] XLIFF 2.0 read support #12853
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/Translation/Loader/XliffVersion/AbstractXliffVersion.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Loader\XliffVersion; | ||
|
||
use Symfony\Component\Translation\MessageCatalogue; | ||
|
||
/** | ||
* Base Xliff version loader class. | ||
* | ||
* @author Berny Cantos <be@rny.cc> | ||
*/ | ||
abstract class AbstractXliffVersion | ||
{ | ||
/** | ||
* Get validation schema source for this version | ||
* | ||
* @return string | ||
*/ | ||
abstract public function getSchema(); | ||
|
||
/** | ||
* Extract messages and metadata from DOMDocument into a MessageCatalogue | ||
* | ||
* @param \DOMDocument $dom Source to extract messages and metadata | ||
* @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata | ||
* @param string $domain The domain | ||
*/ | ||
abstract public function extract(\DOMDocument $dom, MessageCatalogue $catalogue, $domain); | ||
|
||
/** | ||
* Internally changes the URI of a dependent xsd to be loaded locally | ||
* | ||
* @param string $schemaSource Current content of schema file | ||
* @param string $xmlUri External URI of XML to convert to local | ||
* | ||
* @return string | ||
*/ | ||
protected function fixXmlLocation($schemaSource, $xmlUri) | ||
{ | ||
$newPath = str_replace('\\', '/', __DIR__).'/../schema/dic/xliff-core/xml.xsd'; | ||
$parts = explode('/', $newPath); | ||
if (0 === stripos($newPath, 'phar://')) { | ||
$tmpfile = tempnam(sys_get_temp_dir(), 'sf2'); | ||
if ($tmpfile) { | ||
copy($newPath, $tmpfile); | ||
$parts = explode('/', str_replace('\\', '/', $tmpfile)); | ||
} | ||
} | ||
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : ''; | ||
$newPath = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts)); | ||
|
||
return str_replace($xmlUri, $newPath, $schemaSource); | ||
} | ||
|
||
/** | ||
* Convert a UTF8 string to the specified encoding | ||
* | ||
* @param string $content String to decode | ||
* @param string $encoding Target encoding | ||
* | ||
* @throws \RuntimeException | ||
* @return string | ||
*/ | ||
protected function utf8ToCharset($content, $encoding = null) | ||
{ | ||
if (empty($encoding) || 'UTF-8' === $encoding) { | ||
return $content; | ||
} | ||
|
||
if (function_exists('mb_convert_encoding')) { | ||
return mb_convert_encoding($content, $encoding, 'UTF-8'); | ||
} | ||
|
||
if (function_exists('iconv')) { | ||
return iconv('UTF-8', $encoding, $content); | ||
} | ||
|
||
throw new \RuntimeException( | ||
'No suitable convert encoding function (use UTF-8 as your encoding or install the iconv or mbstring extension).' | ||
); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/Translation/Loader/XliffVersion/XliffVersion12.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Translation\Loader\XliffVersion; | ||
|
||
use Symfony\Component\Translation\MessageCatalogue; | ||
|
||
/** | ||
* XliffVersion12 loads XLIFF files identified with version 1.2 | ||
* | ||
* @author Berny Cantos <be@rny.cc> | ||
*/ | ||
class XliffVersion12 extends AbstractXliffVersion | ||
{ | ||
/** | ||
* Get validation schema source for this version | ||
* | ||
* @return string | ||
*/ | ||
public function getSchema() | ||
{ | ||
$source = file_get_contents(__DIR__.'/../schema/dic/xliff-core/xliff-core-1.2-strict.xsd'); | ||
|
||
return $this->fixXmlLocation($source, 'http://www.w3.org/2001/xml.xsd'); | ||
} | ||
|
||
/** | ||
* Extract messages and metadata from DOMDocument into a MessageCatalogue | ||
* | ||
* @param \DOMDocument $dom Source to extract messages and metadata | ||
* @param MessageCatalogue $catalogue Catalogue where we'll collect messages and metadata | ||
* @param string $domain The domain | ||
*/ | ||
public function extract(\DOMDocument $dom, MessageCatalogue $catalogue, $domain) | ||
{ | ||
$xml = simplexml_import_dom($dom); | ||
$encoding = strtoupper($dom->encoding); | ||
|
||
$xml->registerXPathNamespace('xliff', 'urn:oasis:names:tc:xliff:document:1.2'); | ||
|
||
foreach ($xml->xpath('//xliff:trans-unit') as $translation) { | ||
$attributes = $translation->attributes(); | ||
|
||
if (!(isset($attributes['resname']) || isset($translation->source)) || !isset($translation->target)) { | ||
continue; | ||
} | ||
|
||
$source = isset($attributes['resname']) && $attributes['resname'] ? $attributes['resname'] : $translation->source; | ||
// If the xlf file has another encoding specified, try to convert it because | ||
// simple_xml will always return utf-8 encoded values | ||
$target = $this->utf8ToCharset((string) $translation->target, $encoding); | ||
|
||
$catalogue->set((string) $source, $target, $domain); | ||
|
||
if (isset($translation->note)) { | ||
$notes = array(); | ||
foreach ($translation->note as $xmlNote) { | ||
$noteAttributes = $xmlNote->attributes(); | ||
$note = array('content' => $this->utf8ToCharset((string) $xmlNote, $encoding)); | ||
if (isset($noteAttributes['priority'])) { | ||
$note['priority'] = (int) $noteAttributes['priority']; | ||
} | ||
|
||
if (isset($noteAttributes['from'])) { | ||
$note['from'] = (string) $noteAttributes['from']; | ||
} | ||
|
||
$notes[] = $note; | ||
} | ||
|
||
$catalogue->setMetadata((string) $source, array('notes' => $notes), $domain); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't handle an
UnexpectedValueException
that might be thrown ingetVersionNumber()
neither here or in theload()
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, my bad!
This should throw a
InvalidArgumentException
so we can catch onload
method for better message readability.Easy fix. Thank you!