question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using inline handlebars helpers within translation strings

See original GitHub issue
  • I am on the latest ember-intl version
  • I have searched the issues of this repo and found issues dating back to a couple of years #102 and #421.

Environment

  • Ember Version: 2.16.4
  • Ember CLI Version: 2.18.2
  • Ember Intl Version: 3.5.0
  • Browser(s): Chrome 70
  • Node Version: 8.11.1

Steps to Reproduce

I’ve run into a rather interesting problem while working with ember-intl regarding usage of {{helpers}} (and some components) within the translation strings - particularly {{action}}, {{link-to}} and {{input}}. I’m working on a localization project for the first time so I’m not really well versed with conventions regarding internationalization, but I believe having inline actions and links would be a very common scenario. For example, consider the following string:

You can <a href="#" {{action 'allow'}}>allow</a> incoming message notifications 
or <a href="#" {{action 'disable'}}>disable</a> them entirely. You can also {{#link-to 
'settings.notifications' user}}manage notifications{{/link-to}} for your own profile.

Another variant of the above string used extensively within the app I’m working on uses inline {{input}} within the text for some controls, and sometimes even custom components.

Reading the discussions in #102 and #421 seem to suggest that there were discussions for a possible solution to this a couple of years ago but seems like nothing concrete really materialized from them. Are there any current guidelines or best practices for tackling these scenarios since I wasn’t able to figure out the best approach from the documentation?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mongoose700commented, Feb 24, 2019

Hello! My team has been using a custom i18n solution, but we’d like to switch to using ember-intl. We have a solution for this called t-split. It can take a translation string like

Text before the link
{{#link}}
  First text in the link
  {{#strong}}text in the strong{{/strong}},
  final text in the link
{{/link}}
text after the link

and be used in the template like this:

{{#t-split "translation.key" as |segment|~}}
  {{~#if (eq segment.key "link")~}}
    <a href="#" {{action "didClickLink"}}>
      {{~#segment.content as |innerSegment|~}}
        {{~#if (eq innerSegment.key "strong")~}}
          <strong>
            {{~innerSegment.content~}}
          </strong>
        {{~/if~}}
      {{~/segment.content~}}
    </a>
  {{~/if~}}
{{~/t-split}}

It finds the special {{#separator}} and {{/separator}} tags in the translation string, converts into the appropriate nested structure, then yields them as nested contextual components. Any text not wrapped in special tags gets rendered automatically, so it only needs to handle the special text.

We could implement the same thing in ember-intl.

2reactions
mongoose700commented, Feb 24, 2019

In cases where there’s only one type of segment that appears in a particular string, you can leave out the ifs.

{{#t-split "translation.key" as |segment|~}}
  <a href="#" {{action "didClickLink"}}>
    {{~#segment.content as |innerSegment|~}}
      <strong>{{innerSegment.content}}</strong>
    {{~/segment.content~}}
  </a>
{{~/t-split}}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handlebars Helpers Reference - BigCommerce Dev Center
Retrieves corresponding values from some or all elements in a collection using specified search key(s). Returns retrieved values in a comma-separated string.
Read more >
Handlebars Helpers for Custom Templates - HelpDocs Support
Inline helpers. The simplest way to use helpers is inline. e.g. · Block helpers. Block helpers can help you do conditional statements and...
Read more >
Expressions | Handlebars
Handlebars expressions are the basic unit of a Handlebars template. You can use them alone in a {{mustache}} , pass them to a...
Read more >
How to use the handlebars.helpers function in handlebars | Snyk
var handlebars = require('handlebars'); if (!handlebars.helpers.inlineLinks) { throw new Error('inlineLinks helper not registered -- might not be the correct ...
Read more >
Handlebars helper for i18n of templates using Fluent
language selection is coded in a less reusable way in mine (but I plan to split this out) · I use {{text}} not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found