Stichoza / google-translate-php

🔤 Free Google Translate API PHP Package. Translates totally free of charge.
MIT License
1.79k stars 380 forks source link

Ignore escaped keys on translations #198

Closed kylemilloy closed 8 months ago

kylemilloy commented 8 months ago

Problem

Right now when sending keys to be translated we include serialization keys like:

__('Hello :name', ['name' => $user->name])

And when we send this to get translated we get:

// en.json
"Hello :name": "Hello :name"
// fr.json
"Hello :name": "Hello :nom"

Solution

Provide a way to ignore serialization keys or modify the match rules for identifying these.

Stichoza commented 8 months ago

It's a known issue, similar to #167. Unfortunately Google doesn't understand such tags and it translates everything.

kylemilloy commented 8 months ago

Sure. But surely there's a change we can make here that what gets sent to Google doesn't have to be translated. I was thinking of a solution where we use preg_replace to match keys, for example the string Hello :name and have that transform into Hello ${0} which gets sent to Google. On the return trip, we replace the key back so that we get Bonjour ${0} and then just swap back in our value here with Bonjour :name

If you reopen this, I can try to make the appropriate changes and submit a PR. With SVO/VOS/whatever languages it won't work all the time, but it's better than the alternative right now which still breaks.

Stichoza commented 8 months ago

That is actually a great idea! Laravel and most of PHP frameworks use :variable syntax so it will cover most of use cases.

kylemilloy commented 8 months ago

Okay. I'll focus on using colons as the default delimiter but maybe give the option to provide your own pattern or resolver method.