bugGNU gettext - Bugs: bug #60027, Supporting template interpolation...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #60027: Supporting template interpolation and tags in JavaScript

Submitter:  Evan Welsh <ewlsh>
Submitted:  Tue 09 Feb 2021 06:33:53 AM UTC
   
 
Category:  JavaScript Severity:  3 - Normal
Item Group:  None Status:  Fix Released
Privacy:  Public Assigned to:  haible
Open/Closed:  Closed

Jump to the original submission

Sun 01 Dec 2024 04:34:19 PM UTC, comment #6: 

The fix is contained in gettext 0.23.

Bruno Haible <haible>
Group administrator
Sat 02 Nov 2024 11:27:38 PM UTC, comment #5: 

There is no standard facility for this across the JS ecosystem as far as I'm aware. I think adding a javascript-brace-format syntax that supports only {0}, {1}, ... with no other extensions is probably the best solution.

Philip Chimento <ptomato>
Sat 28 Sep 2024 12:15:52 AM UTC, comment #4: 

Implemented through https://git.savannah.gnu.org/gitweb/?p=gettext.git;a=commitdiff;h=967e76ba4f72d6e427ddd3bdca1480a978c4a5a8 .

Please reply regarding the format string type problem, mentioned in the previous comment.

Bruno Haible <haible>
Group administrator
Fri 27 Sep 2024 12:39:31 AM UTC, comment #3: 

Note that if the function transforms the template literal

_`A dog named ${name}`

to a string (for the POT file)

A dog named {0}

it is a format string of unknown kind. If we don't declare it as a format string, translators are not guaranteed to produce valid translations.

The known syntaxes that support {0}, {1}, etc. as placeholders are java-format, csharp-format, python-brace-format. But all of these have extra extensions. We would need something like 'javascript-brace-format'. Is there a quasi-standard facility for such format strings in JavaScript? https://formatjs.io/ seems to use identifiers instead of the numbers 0,1,2... - so that it requires a JSON object as argument at runtime, not an array.

Bruno Haible <haible>
Group administrator
Fri 27 Sep 2024 12:29:06 AM UTC, comment #2: 

After reading https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals , I understand how this should work: At runtime.
1. The designated function gets the N+1 parts of the template literal and the N values of the expressions as arguments.
2. It construct a format string from the N+1 parts.
3. It looks up the translation of this format string.
4. It applies this format strings to the N values.

This can be done with a syntax such as what you proposed:

_`A dog named ${name}`


But I don't see the point of this syntax:

gettext (msgid`A dog named ${name}`)

because that would mean that the gettext() function must be extended to take an object with a format string and N values argument. This way of doing it is redundant.

Bruno Haible <haible>
Group administrator
Tue 09 Feb 2021 06:14:15 PM UTC, comment #1: 

I'd like to express my support for this — backtick strings with interpolation have been around for long enough in JavaScript that there are now plenty of JS programmers who have never experienced the language without them, and so it's natural for them to want to use these strings with gettext.

I like this proposal because it neatly avoids the problems of code inside backtick strings like `My number is ${Math.random()}`, which would just show up as "My number is ${0}" in the messages file.

Many projects using GJS are written in mixed C and JavaScript so xgettext is the natural (and possibly only) tool that will translate strings from a codebase in both of these languages. In GJS we would be eager to implement support for this proposed template tag, but there is no point for us to add it unless xgettext will support it.

Philip Chimento <ptomato>
Tue 09 Feb 2021 06:33:53 AM UTC, original submission:  

GJS hopes to deprecate its formatting module in favor of template strings. Such a change would require xgettext changes to continue supporting translated strings. We're hoping to discuss whether xgettext would ship support for a potential API if we were to implement it in GJS or alternatively what API xgettext would be interested in shipping.

The GJS proposal is currently at https://gitlab.gnome.org/GNOME/gjs/-/issues/374

The goal of the initial proposal is to make it close enough to the existing syntax that it is not overly challenging to parse or implement.

## The API

The API could potentially look like:


gettext(msgid`A sentence with an ${argument}`)


where `msgid` is a template tag which handles the appropriate conversions before passing it to a gettext function.

The msgid would be the same string with any top-level expressions replaced by their index:


msgid "A sentence with an ${0}"

(or)

msgid "A sentence with a ${0} and a ${1}"


### Shortcuts

A shortcut for gettext could also be included:


_`A dog named ${name}`


To simplify the very common...


_(msgid`A dog named ${name}`)


Evan Welsh <ewlsh>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #50941:  0001-Support-template-strings-with-substitutions.patch added by ewlsh (12KiB - text/x-patch - Initial patch for supporting template strings with substitutions.)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by haible (Posted a comment)
  • -email is unavailable- added by ptomato (Posted a comment)
  • -email is unavailable- added by ewlsh (Submitted the item)
  • -email is unavailable- added by ewlsh
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2024-12-01 haible StatusFixed Fix Released
    2024-09-28 haible Open/ClosedOpen Closed
    2024-09-28 haible StatusNone Fixed
    2024-09-27 haible Assigned toNone haible
    2021-02-28 ewlsh Attached File- Added 0001-Support-template-strings-with-substitutions.patch, #50941
    2021-02-09 ewlsh Carbon-Copy- Added -email is unavailable-

    Back to the top

    Powered by Savane 3.14-9aa3.
    Corresponding source code