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

feat: allow '{n}' as placeholder for translated strings #5581

Merged
merged 2 commits into from
Jun 6, 2024

Conversation

akoreman
Copy link
Contributor

@akoreman akoreman commented Jun 5, 2024

Issue #, if available: na

Description of changes: Currently, we only allow $n as the placeholder when providing translated strings to Ace (e.g. Cursor at row $0). This change allows to also use {n} (e.g. Cursor at row {n}) as the syntax as that's a syntax used by some translation libraries.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Pull Request Checklist:

Copy link

codecov bot commented Jun 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.78%. Comparing base (a3277ea) to head (09175fa).

Current head 09175fa differs from pull request most recent head ba06cd5

Please upload reports for the commit ba06cd5 to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5581   +/-   ##
=======================================
  Coverage   86.78%   86.78%           
=======================================
  Files         594      594           
  Lines       43017    43020    +3     
  Branches     7129     7129           
=======================================
+ Hits        37331    37334    +3     
  Misses       5686     5686           
Flag Coverage Δ
unittests 86.78% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@akoreman akoreman merged commit 2206024 into ajaxorg:master Jun 6, 2024
1 check passed
@akoreman akoreman deleted the loc_placeholders branch June 6, 2024 07:55
@nightwing
Copy link
Member

Wouldn't allowing both syntaxes at once cause subtle incompatibilities with escaping, (e.g. if we add a text explaining the supported syntax of replace box). It may be better to select one of two syntaxes when setting the messages.

@akoreman
Copy link
Contributor Author

akoreman commented Jun 8, 2024

you mean to instead do something like? Yeah that might make sense

var translated = messages[key] || messages[defaultString] || defaultString;
        if (params) {
            // We support both $n or {n} as placeholder indicators in the provided translated strings
            if (nlsPlaceHolderMode = "dollarSigns") {
                // Replace $n with the nth element in params
                translated = translated.replace(/\$(\$|[\d]+)/g, function(_, dollarMatch) {
                    if (dollarMatch == "$") return "$";
                    return params[dollarMatch];
                });
            }
            if (nlsPlaceHolderMode = "curlyBrackets") {
                // Replace {n} with the nth element in params
                translated = translated.replace(/\{([^\}]+)\}/g, function(_, curlyBracketMatch) {
                    return params[curlyBracketMatch];
                });
            }
        }
        return translated;

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.

None yet

3 participants