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 placeholder replacement to translation filter #14

Open
JohnAlbin opened this issue Dec 3, 2021 · 0 comments
Open

Add placeholder replacement to translation filter #14

JohnAlbin opened this issue Dec 3, 2021 · 0 comments
Labels
feature New feature or request
Milestone

Comments

@JohnAlbin
Copy link
Owner

JohnAlbin commented Dec 3, 2021

The Drupal version of trans (t) filter accepts parameters for replacement in the original string. For example:

{{ 'Expected waiting time is @time minutes'|t({ '@time': time }) }}

The trans filter will replace the @time characters from the original string with the value of the time variable.

In the current implementation of this project, trans is implemented as a pass-through; it returns the original string without change. We should update it to handle replacements for:

  • @variable
  • %variable
  • :variable

From the Drupal docs on how to handle the replacements: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Render%21FormattableMarkup.php/function/FormattableMarkup%3A%3AplaceholderFormat/9.2.x

  • @variable: When the placeholder replacement value is:

    • A string, the replaced value in the returned string will be sanitized using \Drupal\Component\Utility\Html::escape().
    • A MarkupInterface object, the replaced value in the returned string will not be sanitized.
    • A MarkupInterface object cast to a string, the replaced value in the returned string be forcibly sanitized using \Drupal\Component\Utility\Html::escape(). Doesn't apply to JS implementations of Twig?

    Use this placeholder as the default choice for anything displayed on the site, but not within HTML attributes, JavaScript, or CSS. Doing so is a security risk.

  • %variable: Use when the replacement value is to be wrapped in <em> tags. A call like:
        {{ '%output_text'|trans({ '%output_text': 'text output here.' }) }}
    makes the following HTML code:
        <em class="placeholder">text output here.</em>

    As with @variable, do not use this within HTML attributes, JavaScript, or CSS. Doing so is a security risk.

  • :variable: Return value is escaped with \Drupal\Component\Utility\Html::escape() and filtered for dangerous protocols using UrlHelper::stripDangerousProtocols().

This issue depends on #15, "Add placeholder filter implementation".

@JohnAlbin JohnAlbin added bug Something isn't working feature New feature or request labels Dec 3, 2021
@JohnAlbin JohnAlbin added this to the 1.0 milestone Dec 3, 2021
@JohnAlbin JohnAlbin removed the bug Something isn't working label Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant