Skip to content

Commit 7f50d63

Browse files
author
Zachary Scott
committed
Merge pull request rails#19892 from derekprior/dp-translate-helper-docs
Correct translate helper docs [ci skip]
2 parents 0cb047a + 6ad5e7e commit 7f50d63

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

actionview/lib/action_view/helpers/translation_helper.rb

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,47 @@ module ActionView
77
module Helpers
88
module TranslationHelper
99
include TagHelper
10-
# Delegates to <tt>I18n#translate</tt> but also performs three additional functions.
10+
# Delegates to <tt>I18n#translate</tt> but also performs three additional
11+
# functions.
1112
#
12-
# First, it will ensure that any thrown +MissingTranslation+ messages will be turned
13-
# into inline spans that:
13+
# First, it will ensure that any thrown +MissingTranslation+ messages will
14+
# be rendered as inline spans that:
1415
#
15-
# * have a "translation-missing" class set,
16-
# * contain the missing key as a title attribute and
17-
# * a titleized version of the last key segment as a text.
16+
# * Have a <tt>translation-missing</tt> class applied
17+
# * Contain the missing key as the value of the +title+ attribute
18+
# * Have a titleized version of the last key segment as text
1819
#
19-
# E.g. the value returned for a missing translation key :"blog.post.title" will be
20-
# <span class="translation_missing" title="translation missing: en.blog.post.title">Title</span>.
21-
# This way your views will display rather reasonable strings but it will still
22-
# be easy to spot missing translations.
20+
# For example, the value returned for the missing translation key
21+
# <tt>"blog.post.title"</tt> will be:
2322
#
24-
# Second, it'll scope the key by the current partial if the key starts
25-
# with a period. So if you call <tt>translate(".foo")</tt> from the
26-
# <tt>people/index.html.erb</tt> template, you'll actually be calling
27-
# <tt>I18n.translate("people.index.foo")</tt>. This makes it less repetitive
28-
# to translate many keys within the same partials and gives you a simple framework
29-
# for scoping them consistently. If you don't prepend the key with a period,
30-
# nothing is converted.
23+
# <span
24+
# class="translation_missing"
25+
# title="translation missing: en.blog.post.title">Title</span>
3126
#
32-
# Third, it'll mark the translation as safe HTML if the key has the suffix
33-
# "_html" or the last element of the key is the word "html". For example,
34-
# calling translate("footer_html") or translate("footer.html") will return
35-
# a safe HTML string that won't be escaped by other HTML helper methods. This
36-
# naming convention helps to identify translations that include HTML tags so that
37-
# you know what kind of output to expect when you call translate in a template.
27+
# This allows for views to display rather reasonable strings while still
28+
# giving developers a way to find missing translations.
29+
#
30+
# If you would prefer missing translations to raise an error, you can
31+
# opt out of span-wrapping behavior globally by setting
32+
# <tt>ActionView::Base.raise_on_missing_translations = true</tt> or
33+
# individually by passing <tt>raise: true</tt> as an option to
34+
# <tt>translate</tt>.
35+
#
36+
# Second, if the key starts with a period <tt>translate</tt> will scope
37+
# the key by the current partial. Calling <tt>translate(".foo")</tt> from
38+
# the <tt>people/index.html.erb</tt> template is equivalent to calling
39+
# <tt>translate("people.index.foo")</tt>. This makes it less
40+
# repetitive to translate many keys within the same partial and provides
41+
# a convention to scope keys consistently.
42+
#
43+
# Third, the translation will be marked as <tt>html_safe</tt> if the key
44+
# has the suffix "_html" or the last element of the key is "html". Calling
45+
# <tt>translate("footer_html")</tt> or <tt>translate("footer.html")</tt>
46+
# will return an HTML safe string that won't be escaped by other HTML
47+
# helper methods. This naming convention helps to identify translations
48+
# that include HTML tags so that you know what kind of output to expect
49+
# when you call translate in a template and translators know which keys
50+
# they can provide HTML values for.
3851
def translate(key, options = {})
3952
options = options.dup
4053
has_default = options.has_key?(:default)

0 commit comments

Comments
 (0)