-
Notifications
You must be signed in to change notification settings - Fork 529
Gettext replacement, attempt 3? Simple integrated JSON (For conceptual review, not merge) #1925
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
|
Example language file: https://github.com/emoncms/emoncms/blob/json_i18n/Modules/feed/locale/fr_FR.json |
|
Crude core.php implementation so far (initial concept thanks to copilot), need to specify context here, e.g which module, dgettext replacement etc. https://github.com/emoncms/emoncms/blob/json_i18n/core.php#L261 |
|
This now includes a script that generates the language files, just run e.g for welsh :) Then copy and paste the json into ChatGPT for a quick translation, it pretty good in welsh to be fair (as a welsh speaker) ;) |
|
Im actually quite happy with this now, i've got the context translations working, @reedy @chaveiro @gablau @alexandrecuer, could one of you test if this breaks anything, there are associated changes to the other non core modules that I can push up if this is a workable direction. |
|
hi @TrystanLea, However, the idea of switching to json as a structure for translations seems good to me, and the idea of abandoning gettext is even better. Just a couple more things:
If I find some time I will try to run this new version of emoncms. Thanks for your work |
|
Thanks @gablau great that you think it could be a good approach! I do notice a few things I've missed that the gettext version is translating but this new version is not. I will hopefully get those sorted shortly.
|
|
created a docker image with your version from the json_i18n branch docker pull alexjunk/emoncms:alpine3.20_emoncms11.8.6Did a quick test, and there were no errors during the built. There are some when you launch the container, probably more because of mosquitto PHP. I switched from English to French without any problem in the admin module. graph module stays in english but I guess it is still the gettext code (?) @TrystanLea : the docker build does not install the symlinked modules as it searches in each module repo for a branch with the same name (json_i18n) which dont yet exists...when working on those modules, should be no problem if you create a branch with exactly the same name : json_i18n |
|
Hmm, perhaps a script to automatically translate keys is not the best approach. Using the chat window in vs code and adding context there works pretty well and allows for more careful review of changes.. |
|
Thanks @reedy, that's a useful reference, I don't really see the benefit tbh:
|
|
A quick look at optimisation potential, it looks like loading the json translation files for every module that has a menu item adds about 3-3.5 ms on my laptop. This takes the time it takes to load the menu system from around 5-5.5 ms up to ~8.5 ms. Loading a text heavy page such as the input/api page goes from around 21ms up to 25ms approx ~4ms additional in welsh vs the base english. Suggests if we do optimise something it's probably the way the menu system is translated. The easiest approach is probably to translate the menu system via the theme translation files.. this would be non perfect from a modularity perspective but could be a way to shave some milliseconds - if it is a problem which Im not sure that it is? Does 3 milliseconds matter? |
Sure there is a price to pay (more time for the migration) but it is more clean. Maybe something in between could be a solution ?
|
Thanks @alexandrecuer, Yes, I do prefer to fall back on the english words. With the logging of the old translation for reference we at least have a way to recover and edit if needed the translations that need fixing. How about we use the current approach to start with, we could always review this in a year or so time, if small changes keep annoyingly breaking existing translations? |
|
Keen to merge this so that we can continue with other items without having to worry about merge conflicts :) Rough plan for future development in a separate pull request / development effort at a later date:
|
I will review the french translations for the input process descriptions after the merge |



Proposal: Replace gettext with JSON-based i18n system
Reviewing pull requests I am reminded that I've left this topic of multilingual support hanging for far too long.
This is a proposal for simple integrated multilingual support using JSON language files, it's not complete yet, just an initial crude implementation. The batch conversion of all .po files and all "_(" functions to "tr(" was relatively painless.
Convert all .po files:
Convert all "_(" functions:
Still need to handle: dgettext functions
Why make this change?
What we're giving up
Proposed approach
Questions for discussion
I think there's a case for making this change even if we loose perfect translation. All languages will be available out of the box as standard without system wide locale generation and that should encourage further translation as it will be more evident that locales are in use. I envisage adding both CLI tools for template language file generation and perhaps an integrated UI tool modifying the JSON files. I wonder if it's possible to use translation API's for first pass translations?
@reedy @chaveiro @gablau @alexandrecuer