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

Support configurable template parsers #317

Merged
merged 10 commits into from
Jan 31, 2024
Merged

Support configurable template parsers #317

merged 10 commits into from
Jan 31, 2024

Conversation

nicksnyder
Copy link
Owner

@nicksnyder nicksnyder commented Jan 28, 2024

This change introduces a template.Parser interface and related property on LocalizeConfig so that the template parser can be completely customized or disabled.

Resolves #275
Resolves #289
Resolves #315
Resolves #316

Benchmark
goos: darwin
goarch: arm64
pkg: github.com/nicksnyder/go-i18n/v2/i18n
                                                                                            │   old.txt   │               new.txt                │
                                                                                            │   sec/op    │    sec/op     vs base                │
Localizer_Localize/message_id_mismatch-10                                                     21.43n ± 1%    21.33n ± 1%        ~ (p=0.137 n=10)
Localizer_Localize/message_id_not_mismatched-10                                               175.9n ± 1%    170.5n ± 0%   -3.01% (p=0.000 n=10)
Localizer_Localize/missing_translation_from_default_language-10                               178.2n ± 1%    176.5n ± 0%   -0.98% (p=0.000 n=10)
Localizer_Localize/empty_translation_without_fallback-10                                      212.0n ± 0%    207.3n ± 1%   -2.22% (p=0.000 n=10)
Localizer_Localize/missing_translation_from_default_language_with_other_translation-10        180.0n ± 0%    179.2n ± 0%   -0.47% (p=0.006 n=10)
Localizer_Localize/missing_translations_from_not_default_language-10                          131.6n ± 1%    130.0n ± 1%   -1.25% (p=0.000 n=10)
Localizer_Localize/missing_translation_from_not_default_language-10                           193.6n ± 0%    193.5n ± 0%        ~ (p=0.402 n=10)
Localizer_Localize/missing_translation_not_default_language_with_other_translation-10         195.2n ± 1%    194.0n ± 0%   -0.64% (p=0.001 n=10)
Localizer_Localize/accept_default_language,_message_in_bundle-10                              174.7n ± 1%    169.9n ± 0%   -2.72% (p=0.000 n=10)
Localizer_Localize/accept_default_language,_message_in_bundle,_default_message-10             175.8n ± 1%    170.2n ± 1%   -3.16% (p=0.000 n=10)
Localizer_Localize/accept_not_default_language,_message_in_bundle-10                          176.3n ± 1%    172.8n ± 0%   -2.01% (p=0.000 n=10)
Localizer_Localize/accept_not_default_language,_other_message_in_bundle,_default_message-10   130.5n ± 2%    121.8n ± 1%   -6.67% (p=0.000 n=10)
Localizer_Localize/accept_not_default_language,_message_in_bundle,_default_message-10         181.2n ± 2%    173.1n ± 0%   -4.42% (p=0.000 n=10)
Localizer_Localize/accept_default_language,_default_message-10                                299.6n ± 2%    327.6n ± 3%   +9.33% (p=0.000 n=10)
Localizer_Localize/accept_not_default_language,_default_message-10                            250.2n ± 1%    278.5n ± 0%  +11.29% (p=0.000 n=10)
Localizer_Localize/fallback_to_non-default_less_specific_language-10                          448.3n ± 1%    438.8n ± 0%   -2.12% (p=0.000 n=10)
Localizer_Localize/fallback_to_non-default_more_specific_language-10                          172.8n ± 0%    166.8n ± 0%   -3.47% (p=0.000 n=10)
Localizer_Localize/plural_count_one,_bundle_message-10                                        556.1n ± 1%    551.5n ± 1%   -0.85% (p=0.001 n=10)
Localizer_Localize/plural_count_other,_bundle_message-10                                      557.2n ± 0%    550.1n ± 0%   -1.27% (p=0.000 n=10)
Localizer_Localize/plural_count_float,_bundle_message-10                                      641.5n ± 1%    636.2n ± 0%   -0.83% (p=0.003 n=10)
Localizer_Localize/plural_count_one,_default_message-10                                       2.554µ ± 2%    2.540µ ± 0%        ~ (p=0.134 n=10)
Localizer_Localize/plural_count_missing_one,_default_message-10                               2.552µ ± 1%    2.547µ ± 2%        ~ (p=0.644 n=10)
Localizer_Localize/plural_count_missing_other,_default_message-10                             431.3n ± 0%    427.4n ± 0%   -0.92% (p=0.000 n=10)
Localizer_Localize/plural_count_other,_default_message-10                                     2.529µ ± 0%    2.549µ ± 0%   +0.79% (p=0.000 n=10)
Localizer_Localize/plural_count_float,_default_message-10                                     2.628µ ± 1%    2.634µ ± 0%        ~ (p=0.616 n=10)
Localizer_Localize/template_data,_bundle_message-10                                           420.9n ± 1%    408.8n ± 1%   -2.86% (p=0.000 n=10)
Localizer_Localize/template_data,_default_message-10                                          2.226µ ± 0%    2.232µ ± 0%   +0.29% (p=0.007 n=10)
Localizer_Localize/template_data,_custom_delims,_bundle_message-10                            419.3n ± 0%    410.6n ± 1%   -2.09% (p=0.000 n=10)
Localizer_Localize/template_data,_custom_delims,_default_message-10                           2.221µ ± 1%    2.235µ ± 0%   +0.59% (p=0.012 n=10)
Localizer_Localize/template_data,_plural_count_one,_bundle_message-10                         637.9n ± 0%    636.1n ± 1%        ~ (p=0.079 n=10)
Localizer_Localize/template_data,_plural_count_other,_bundle_message-10                       639.5n ± 0%    637.5n ± 1%        ~ (p=0.159 n=10)
Localizer_Localize/template_data,_plural_count_float,_bundle_message-10                       704.6n ± 1%    705.2n ± 0%        ~ (p=0.424 n=10)
Localizer_Localize/template_data,_plural_count_one,_default_message-10                        3.015µ ± 1%    3.020µ ± 0%        ~ (p=0.492 n=10)
Localizer_Localize/template_data,_plural_count_other,_default_message-10                      3.018µ ± 1%    3.025µ ± 1%        ~ (p=0.314 n=10)
Localizer_Localize/template_data,_plural_count_float,_default_message-10                      3.104µ ± 1%    3.095µ ± 1%        ~ (p=0.725 n=10)
Localizer_Localize/no_fallback-10                                                             207.3n ± 1%    206.7n ± 1%        ~ (p=0.059 n=10)
Localizer_Localize/fallback_default_message-10                                                347.6n ± 0%    379.1n ± 0%   +9.05% (p=0.000 n=10)
Localizer_Localize/no_fallback_default_message-10                                             207.2n ± 1%    206.8n ± 4%        ~ (p=0.108 n=10)
Localizer_Localize/empty_default_message-10                                                   202.9n ± 0%    202.4n ± 0%   -0.27% (p=0.004 n=10)
Localizer_Localize/empty_default_message_with_id-10                                           203.8n ± 2%    202.2n ± 0%   -0.81% (p=0.000 n=10)
NewMatcher-10                                                                                 32.43µ ± 3%    33.03µ ± 2%   +1.86% (p=0.029 n=10)
MatchStrings-10                                                                               990.7n ± 1%   1027.0n ± 1%   +3.66% (p=0.000 n=10)
geomean                                                                                       482.2n         481.0n        -0.24%

                                                                                            │    old.txt     │                new.txt                │
                                                                                            │      B/op      │     B/op      vs base                 │
Localizer_Localize/message_id_mismatch-10                                                       32.00 ± 0%       32.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/message_id_not_mismatched-10                                                 0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_from_default_language-10                                 48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/empty_translation_without_fallback-10                                        48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_from_default_language_with_other_translation-10          48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translations_from_not_default_language-10                            48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_from_not_default_language-10                             48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_not_default_language_with_other_translation-10           48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_default_language,_message_in_bundle-10                                0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_default_language,_message_in_bundle,_default_message-10               0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_not_default_language,_message_in_bundle-10                            0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_not_default_language,_other_message_in_bundle,_default_message-10     0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_not_default_language,_message_in_bundle,_default_message-10           0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_default_language,_default_message-10                                  368.0 ± 0%       384.0 ± 0%  +4.35% (p=0.000 n=10)
Localizer_Localize/accept_not_default_language,_default_message-10                              368.0 ± 0%       384.0 ± 0%  +4.35% (p=0.000 n=10)
Localizer_Localize/fallback_to_non-default_less_specific_language-10                            56.00 ± 0%       56.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/fallback_to_non-default_more_specific_language-10                            0.000 ± 0%       0.000 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_one,_bundle_message-10                                          608.0 ± 0%       608.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_other,_bundle_message-10                                        608.0 ± 0%       608.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_float,_bundle_message-10                                        640.0 ± 0%       640.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_one,_default_message-10                                       3.994Ki ± 0%     4.002Ki ± 0%  +0.20% (p=0.000 n=10)
Localizer_Localize/plural_count_missing_one,_default_message-10                               3.932Ki ± 0%     3.939Ki ± 0%  +0.20% (p=0.000 n=10)
Localizer_Localize/plural_count_missing_other,_default_message-10                               800.0 ± 0%       800.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_other,_default_message-10                                     3.994Ki ± 0%     4.002Ki ± 0%  +0.20% (p=0.000 n=10)
Localizer_Localize/plural_count_float,_default_message-10                                     4.025Ki ± 0%     4.033Ki ± 0%  +0.19% (p=0.000 n=10)
Localizer_Localize/template_data,_bundle_message-10                                             208.0 ± 0%       208.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_default_message-10                                          3.393Ki ± 0%     3.400Ki ± 0%  +0.23% (p=0.000 n=10)
Localizer_Localize/template_data,_custom_delims,_bundle_message-10                              208.0 ± 0%       208.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_custom_delims,_default_message-10                           3.393Ki ± 0%     3.400Ki ± 0%  +0.23% (p=0.000 n=10)
Localizer_Localize/template_data,_plural_count_one,_bundle_message-10                           304.0 ± 0%       304.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_plural_count_other,_bundle_message-10                         304.0 ± 0%       304.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_plural_count_float,_bundle_message-10                         344.0 ± 0%       344.0 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_plural_count_one,_default_message-10                        3.971Ki ± 0%     3.979Ki ± 0%  +0.20% (p=0.000 n=10)
Localizer_Localize/template_data,_plural_count_other,_default_message-10                      3.971Ki ± 0%     3.979Ki ± 0%  +0.20% (p=0.000 n=10)
Localizer_Localize/template_data,_plural_count_float,_default_message-10                      4.010Ki ± 0%     4.018Ki ± 0%  +0.19% (p=0.000 n=10)
Localizer_Localize/no_fallback-10                                                               48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/fallback_default_message-10                                                  416.0 ± 0%       432.0 ± 0%  +3.85% (p=0.000 n=10)
Localizer_Localize/no_fallback_default_message-10                                               48.00 ± 0%       48.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/empty_default_message-10                                                     96.00 ± 0%       96.00 ± 0%       ~ (p=1.000 n=10) ¹
Localizer_Localize/empty_default_message_with_id-10                                             96.00 ± 0%       96.00 ± 0%       ~ (p=1.000 n=10) ¹
NewMatcher-10                                                                                 22.35Ki ± 0%     22.35Ki ± 0%       ~ (p=0.837 n=10)
MatchStrings-10                                                                                 584.0 ± 0%       584.0 ± 0%       ~ (p=1.000 n=10) ¹
geomean                                                                                                    ²                 +0.34%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                                                                                            │   old.txt    │               new.txt                │
                                                                                            │  allocs/op   │ allocs/op   vs base                  │
Localizer_Localize/message_id_mismatch-10                                                     1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/message_id_not_mismatched-10                                               0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_from_default_language-10                               1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/empty_translation_without_fallback-10                                      1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_from_default_language_with_other_translation-10        1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translations_from_not_default_language-10                          1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_from_not_default_language-10                           1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/missing_translation_not_default_language_with_other_translation-10         1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_default_language,_message_in_bundle-10                              0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_default_language,_message_in_bundle,_default_message-10             0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_not_default_language,_message_in_bundle-10                          0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_not_default_language,_other_message_in_bundle,_default_message-10   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_not_default_language,_message_in_bundle,_default_message-10         0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/accept_default_language,_default_message-10                                4.000 ± 0%     5.000 ± 0%  +25.00% (p=0.000 n=10)
Localizer_Localize/accept_not_default_language,_default_message-10                            4.000 ± 0%     5.000 ± 0%  +25.00% (p=0.000 n=10)
Localizer_Localize/fallback_to_non-default_less_specific_language-10                          4.000 ± 0%     4.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/fallback_to_non-default_more_specific_language-10                          0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_one,_bundle_message-10                                        9.000 ± 0%     9.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_other,_bundle_message-10                                      9.000 ± 0%     9.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_float,_bundle_message-10                                      10.00 ± 0%     10.00 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_one,_default_message-10                                       43.00 ± 0%     44.00 ± 0%   +2.33% (p=0.000 n=10)
Localizer_Localize/plural_count_missing_one,_default_message-10                               43.00 ± 0%     44.00 ± 0%   +2.33% (p=0.000 n=10)
Localizer_Localize/plural_count_missing_other,_default_message-10                             8.000 ± 0%     8.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/plural_count_other,_default_message-10                                     43.00 ± 0%     44.00 ± 0%   +2.33% (p=0.000 n=10)
Localizer_Localize/plural_count_float,_default_message-10                                     44.00 ± 0%     45.00 ± 0%   +2.27% (p=0.000 n=10)
Localizer_Localize/template_data,_bundle_message-10                                           6.000 ± 0%     6.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_default_message-10                                          36.00 ± 0%     37.00 ± 0%   +2.78% (p=0.000 n=10)
Localizer_Localize/template_data,_custom_delims,_bundle_message-10                            6.000 ± 0%     6.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_custom_delims,_default_message-10                           36.00 ± 0%     37.00 ± 0%   +2.78% (p=0.000 n=10)
Localizer_Localize/template_data,_plural_count_one,_bundle_message-10                         9.000 ± 0%     9.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_plural_count_other,_bundle_message-10                       9.000 ± 0%     9.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_plural_count_float,_bundle_message-10                       10.00 ± 0%     10.00 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/template_data,_plural_count_one,_default_message-10                        50.00 ± 0%     51.00 ± 0%   +2.00% (p=0.000 n=10)
Localizer_Localize/template_data,_plural_count_other,_default_message-10                      50.00 ± 0%     51.00 ± 0%   +2.00% (p=0.000 n=10)
Localizer_Localize/template_data,_plural_count_float,_default_message-10                      51.00 ± 0%     52.00 ± 0%   +1.96% (p=0.000 n=10)
Localizer_Localize/no_fallback-10                                                             1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/fallback_default_message-10                                                5.000 ± 0%     6.000 ± 0%  +20.00% (p=0.000 n=10)
Localizer_Localize/no_fallback_default_message-10                                             1.000 ± 0%     1.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/empty_default_message-10                                                   2.000 ± 0%     2.000 ± 0%        ~ (p=1.000 n=10) ¹
Localizer_Localize/empty_default_message_with_id-10                                           2.000 ± 0%     2.000 ± 0%        ~ (p=1.000 n=10) ¹
NewMatcher-10                                                                                 498.0 ± 0%     498.0 ± 0%        ~ (p=1.000 n=10) ¹
MatchStrings-10                                                                               10.00 ± 0%     10.00 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                                                                                  ²                +2.01%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

Copy link

codecov bot commented Jan 28, 2024

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (5257e26) 82.77% compared to head (499e664) 82.81%.

Files Patch % Lines
v2/i18n/message_template.go 71.42% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #317      +/-   ##
==========================================
+ Coverage   82.77%   82.81%   +0.04%     
==========================================
  Files          15       15              
  Lines        1730     1740      +10     
==========================================
+ Hits         1432     1441       +9     
- Misses        232      234       +2     
+ Partials       66       65       -1     

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

@nicksnyder nicksnyder marked this pull request as ready for review January 30, 2024 06:02
@nicksnyder nicksnyder changed the title Support pluggable template engines Support configurable template parsers Jan 30, 2024
@nicksnyder
Copy link
Owner Author

@nohajc I made some changes to the PR (mostly names of things). Have any comments before I merge it?

@nohajc
Copy link

nohajc commented Jan 30, 2024

Thank you. I like it, you can merge. :)

@nicksnyder nicksnyder merged commit 62e3978 into main Jan 31, 2024
8 checks passed
@nicksnyder nicksnyder deleted the template-engine branch January 31, 2024 04:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants