Skip to content
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

function formatter, add basic function formatter #3171

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ecstrema
Copy link
Contributor

@ecstrema ecstrema commented Mar 8, 2023

This is very very rough work on adding a function formatter to tolgee core. Before I dive in deeper (I haven't actually tested the code yet, just wrote a few lines), is this the right path? Or, instead of via a plugin, do you think a function formatter should be included directly in the core formatter?

Any guidance welcome before I proceed.

@stepan662
Copy link
Collaborator

stepan662 commented Mar 8, 2023

Hey, my idea was to include it directly into the core basically - if there is a function instead of translation, just call the function. The approach with the plugin might be more flexible, if it works, it might be better.

My biggest concern was how to solve translation updates (when user updates translation in in-context dialog). As the function for formatting is generated outside Tolgee, it has no chance of knowing how to interpret the translation.

And also in development mode, translations are downloaded directly from Tolgee platform as strings.

@ecstrema
Copy link
Contributor Author

ecstrema commented Mar 8, 2023

Hey, my idea was to include it directly into the core basically - if there is a function instead of translation, just call the function. The approach with the plugin might be more flexible, if it works, it might be better.

The plugin might work, but it will be harder to make it work with your two other points (following)

My biggest concern was how to solve translation updates (when user updates translation in in-context dialog). As the function for formatting is generated outside Tolgee, it has no chance of knowing how to interpret the translation.

That's a good one. So if a translation is updated by the in-context dialog, I guess the in-context dialog should show the function as content. But editing it won't work, since it's a function. I see two approaches: 1) prevent using the in-context dialog for functions or 2) Since we have the translation id, we could redirect to Tolgee platform for that. Which brings to the last point:

And also in development mode, translations are downloaded directly from Tolgee platform as strings.

The thing is that clearly, it is not possible to manage the function from Tolgee platform, as it can only work with strings. But we can add a layer around the tolgee platform to 1) post process the strings 2) pre-process the content sent back.

So in total:

  1. If the translation received by the core is a function, call that function and add the magic zero-width chars going with the key
  2. When downloading translations directly from Tolgee, we need a post-process hook to obtain the function from the strings.
  3. When that translation needs to be modified, same as usual, but get the string that needs to be modified from the tolgee platform instance instead of directly from the result, and apply the post-process hook again.

Does that seem doable/sensible?

@stepan662
Copy link
Collaborator

Yep, the post process hook could basically solve this. But it must be done in a clever way, so it's only included in dev mode. As this will basically include whole icu parser, so that would bloat your bundle unecessarly.

My idea was that the post processor could run as an local server and the tolgee would hit it when necessary. But then it's more complicated setup for the user ...

@ecstrema
Copy link
Contributor Author

Hmm, the local server might be a good idea... It would just require a webpack/vite/etc. plugin, or, put simply an unplugin. There's documentation on adding a server directly to vite's dev server and wepack's dev server (though it only works on development mode).

The plugin might actually be a good idea for more than that:

  • Fetch translations at build time
  • Sync translations automagically
  • Virtual modules for translations, instead of loading them from ./i18n/[language].json
  • (Optimize translations?)

However, the burden of maintaining a plugin might not be worth it, so the post processor hook should be considered, and then translating in production should be accompanied with a message like: This translation will be updated the next time this website is built.

@stepan662
Copy link
Collaborator

Yeah, basically I think we could have a configuration option on tolgee core to postprocess a translation after is downloaded from the platform. That should be pretty universal. And then you can have a vite-plugin which would run the local server. But basically the core has to be universal for all build systems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants