-
-
Notifications
You must be signed in to change notification settings - Fork 390
feat (translation): add ability to ignore keys in translations #199
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
Conversation
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.
Thanks for your work! 🙌🏼 Couple of changes:
- For easier understanding, can you rename methods
extract
->extractParameters
,inject
->injectParameters
,getReplacements
->getParameters
. - Make above mentioned methods
protected
. Also, no need of testing non-public methods. - Make this feature optional. Would be better to get rid of the method
setPattern
and addpreserveParameters(?string $pattern = null)
that will enable this feature and set custom pattern if specified.
I've gone ahead and responded to your feedback. I have it so it always runs through the key extraction but it's ignored if you don't set it explicitly as requested. Tests are expanded to show a couple examples of this and one for custom key matching (like if you wanna do {{key}} for example). You can also set this off of the static method or constructor too for ease of use... // like this...
$translator = new GoogleTranslate(pattern: '/:(\w+/');
// or this...
GoogleTranslate::trans(pattern: '/:(\w+/');
// or this
$translator->preserveParameters('/:(\w+/'); If patterns like |
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.
Great! Thank you very much! 🥳
What does this change?
Allows us to provide keys to translate without eating them up, for example,
Hello :name
was becomingBonjour :nom
. Now this will stored asBonjour :name
still.How does this work?
We do a couple preg_replace commands to extract replacements, modify the string before sending to Google, and then swap back in our replacements. In a string such as
Hello :name are you :type_of_greeting?
for example this replaces this string withHello ${0} are you ${1}?
with the ${\d} values being ignored by Google, everything around it gets translated. When we receive this translated string back we then swap these values out for the matching keys that should be there so it changes fromBonjour ${0} are you ${1}
toBonjour :name are you :type_of_greeting?
where our system can then translate it as normal.Notes
Resolves #198
Also introduces ambiguous space characters being introduced by Google Translate (see screenshot).