Skip to content

[Translation] Added support for translation files with other filename patterns #27655

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

Merged
merged 1 commit into from
Jun 25, 2018

Conversation

javiereguiluz
Copy link
Member

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #27644
License MIT
Doc PR -

This implements the #27644 feature request in case we accept it.

My vote is 👍 because the changes required are tiny and the resulting code is even more robust thanks to the new preg_match() call.

@javiereguiluz javiereguiluz added this to the next milestone Jun 20, 2018
@nicolas-grekas nicolas-grekas changed the title Added support for translation files with other filename patterns [Translation] Added support for translation files with other filename patterns Jun 20, 2018
$expectedFileExtension = sprintf('%s.xlf', str_replace('-', '_', $targetLanguage));
$realFileExtension = explode('.', basename($file), 2)[1] ?? '';
$normalizedLocale = str_replace('-', '_', $targetLanguage);
$expectedFilenamePattern = sprintf('/^(.*\.%s\.xlf|%s\..*\.xlf)/', $normalizedLocale, $normalizedLocale);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing preg_quote on the 2 variables you inject in the regex

@stof
Copy link
Member

stof commented Jun 20, 2018

In a FrameworkBundle-based project, the file name format must always be <domain>.<locale>.<format>. Anything else is unsupported (because the bundle needs to be able to extract the domain and the locale from the file name). So having the convention configurable in the command (using a constructor argument) would be better to me than supporting having the locale anywhere in the file name (which would not report all issues for Symfony projects)

@javiereguiluz
Copy link
Member Author

@stof I've made some changes. Please tell me if that's what you were proposing. Thanks.

$realFileExtension = explode('.', basename($file), 2)[1] ?? '';
$normalizedLocale = preg_quote(str_replace('-', '_', $targetLanguage));
// when this command is used inside Symfony, require filenames to be '____.locale.xlf'
// when used outside Symfony, allow '____.locale.xlf' and 'locale.____.xlf'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments should be talking about strict vs not strict, not about inside Symfony. Other projects may put the command in strict mode too.


public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null)
public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null, bool $requireStrictFileNames = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we keep using true as default, to keep the same behavior than today by default ?

@@ -115,14 +117,16 @@ private function validate($content, $file = null)
$document->loadXML($content);

if (null !== $targetLanguage = $this->getTargetLanguageFromFile($document)) {
$expectedFileExtension = sprintf('%s.xlf', str_replace('-', '_', $targetLanguage));
$realFileExtension = explode('.', basename($file), 2)[1] ?? '';
$normalizedLocale = preg_quote(str_replace('-', '_', $targetLanguage));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to pass the regex delimiter as second argument to preg_quote as it needs to be escaped too.

Copy link
Member

@fabpot fabpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some more tests , especially when requireStrictFileNames is false?

@fabpot
Copy link
Member

fabpot commented Jun 25, 2018

Thank you @javiereguiluz.

@fabpot fabpot merged commit 0ee912d into symfony:master Jun 25, 2018
fabpot added a commit that referenced this pull request Jun 25, 2018
… other filename patterns (javiereguiluz)

This PR was squashed before being merged into the 4.2-dev branch (closes #27655).

Discussion
----------

[Translation] Added support for translation files with other filename patterns

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27644
| License       | MIT
| Doc PR        | -

This implements the #27644 feature request in case we accept it.

My vote is 👍 because the changes required are tiny and the resulting code is even more robust thanks to the new `preg_match()` call.

Commits
-------

0ee912d [Translation] Added support for translation files with other filename patterns
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants