php-i18n icon indicating copy to clipboard operation
php-i18n copied to clipboard

Switch Language

Open nenad007 opened this issue 5 years ago • 5 comments

Is there a solution, how I can switch language during script execution?

I have the case where I send Emails/Notifications to the users by a cron script, depending on the defined user Language every User should got the Email/Notification in the correct language.

nenad007 avatar Mar 12 '19 09:03 nenad007

No, that's not possible. However, I'm open to accepting pull requests that add this feature while keeping API stability.

Philipp15b avatar Apr 02 '19 07:04 Philipp15b

I have implemented this functionality, but I did not found a way to keep the API in full, concretely I lost calls like L::HELLO which must be called by one of: L::{lang}_HELLO, L::HELLO(), L('HELLO') ...

and instead to have one cached file per Language I generate one file for all languages where I set {lang} as prefix to the constants. I'm not sure if this is a problem as the file got cached in Memory anyway.

If you have some idea how I could do it better please let me know and I will go this way and make a pull request.

nenad007 avatar Apr 02 '19 15:04 nenad007

The language compiler could compile each language class with a separate class name (which includes the language code) and then the main L class could use magic methods to delegate into the currently selected language class... but I fear that this will break api compat.

TheJosh avatar Feb 14 '20 03:02 TheJosh

Yep it's not possible, without either (a) changing the api or (b) using runkit.

TheJosh avatar Feb 17 '20 20:02 TheJosh