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

Add option to skip template execution #316

Closed
wants to merge 2 commits into from
Closed

Add option to skip template execution #316

wants to merge 2 commits into from

Conversation

nohajc
Copy link

@nohajc nohajc commented Jan 27, 2024

Hi,

I use your library in production and it works great. Except now I've come to a point where I need it to play nicely with a different string parameter syntax.

Just for context, I'm dealing with a shared repository of localized strings which has multiple consumers using different programming languages and libraries. This already involves conversion from xliff to a format suitable for go-i18n and some of the strings contain placeholders with a C#-like (but simplified) format string syntax (e.g. "arg1: {0}, arg2: {1}, {{escaped}}").

The problem is with escaped curly braces which correspond to parameters in go templates. When I try to localize a string like this, I get en error because it's not even a valid go template.

Therefore it would be very useful if I could just disable template execution and process any string parameters with custom syntax myself.

Copy link

codecov bot commented Jan 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (5257e26) 82.77% compared to head (b4ca9f4) 82.91%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #316      +/-   ##
==========================================
+ Coverage   82.77%   82.91%   +0.13%     
==========================================
  Files          15       15              
  Lines        1730     1744      +14     
==========================================
+ Hits         1432     1446      +14     
  Misses        232      232              
  Partials       66       66              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nohajc
Copy link
Author

nohajc commented Jan 27, 2024

Btw, I did figure out it's possible to configure custom parameter delimiters, but that still requires using go-style named parameters. This just doesn't work for numeric indices unless I escape any occurrences of double curly braces which basically means turning every {{ into {{ "{{" }} (and the same with }}). I'd say it's better to make the library more configurable rather than using workarounds like this.

@nicksnyder
Copy link
Owner

nicksnyder commented Jan 27, 2024

Thanks for the clear problem statement and proposed solution. Something I want to consider is making the templating engine itself customizable. Then it would be possible to provide a no-op templating engine (which would solve the problem you have). I am not sure if this can be done in a backward compatible way, but if it can that might be preferable.

@nohajc
Copy link
Author

nohajc commented Jan 27, 2024

@nicksnyder Would you be willing to merge this before going forward with the more general solution? It would really help me right now... Otherwise I'd probably end up forking the library for the time being.

I mean, the change in public API is rather minimal, it's just one more config option. Of course the no-op templating engine would be a different option but I suppose by then you could just deprecate this one.

@nicksnyder
Copy link
Owner

There are a few issues that would be solved by making the templating system more flexible, so if we are going to add to the public API, I would prefer to do it in a way that will resolve multiple issues at once. I think I have a solution coded up in #317 but I still need to add some tests to verify.

With that change, instead of NoTemplateExec = true you would use TemplateEngine: NoTemplateEngine{}.

Would that work for you?

@nohajc
Copy link
Author

nohajc commented Jan 28, 2024

You're fast. :) Definitely, that would work.

@nicksnyder
Copy link
Owner

With 2.4.0 there is now a way to disable template parsing. Check out the release notes for more info.

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