From 66b97d1875008048c5d2ef971912fe4341e9905f Mon Sep 17 00:00:00 2001 From: Winnie Hellmann Date: Thu, 25 Jan 2018 13:36:04 +0000 Subject: Document example for sprintf without escaping --- doc/development/i18n/externalization.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/doc/development/i18n/externalization.md b/doc/development/i18n/externalization.md index c0a325a83e9..f521cb24fc7 100644 --- a/doc/development/i18n/externalization.md +++ b/doc/development/i18n/externalization.md @@ -207,6 +207,9 @@ There is also and alternative method to [translate messages from validation erro ### Interpolation +Placeholders in translated text should match the code style of the respective source file. +For example use `%{created_at}` in Ruby but `%{createdAt}` in JavaScript. + - In Ruby/HAML: ```ruby @@ -216,12 +219,20 @@ There is also and alternative method to [translate messages from validation erro - In JavaScript: ```js - import { __, sprintf } from '../../../locale'; - sprintf(__('Hello %{username}'), { username: 'Joe' }) => 'Hello Joe' + import { __, sprintf } from '~/locale'; + + sprintf(__('Hello %{username}'), { username: 'Joe' }); // => 'Hello Joe' ``` -The placeholders should match the code style of the respective source file. -For example use `%{created_at}` in Ruby but `%{createdAt}` in JavaScript. + By default, `sprintf` escapes the placeholder values. + If you want to take care of that yourself, you can pass `false` as third argument. + + ```js + import { __, sprintf } from '~/locale'; + + sprintf(__('This is %{value}'), { value: 'bold' }); // => 'This is <strong>bold</strong>' + sprintf(__('This is %{value}'), { value: 'bold' }, false); // => 'This is bold' + ``` ### Plurals -- cgit v1.2.1