Skip to content
Newer
Older
100644 105 lines (96 sloc) 4.03 KB
c9ed2c9 @svenfuchs add a translation helper
svenfuchs authored
1 require 'action_view/helpers/tag_helper'
896e25e @svenfuchs Make TranslationHelper#translate use the :rescue_format option in I18…
svenfuchs authored
2 require 'i18n/exceptions'
3
4 module I18n
5 class ExceptionHandler
6 include Module.new {
7 def call(exception, locale, key, options)
0438e92 @dmathieu simplify to only one condition
dmathieu authored
8 exception.is_a?(MissingTranslation) && options[:rescue_format] == :html ? super.html_safe : super
896e25e @svenfuchs Make TranslationHelper#translate use the :rescue_format option in I18…
svenfuchs authored
9 end
10 }
11 end
12 end
c9ed2c9 @svenfuchs add a translation helper
svenfuchs authored
13
14 module ActionView
324de19 @rizwanreza Added titles and description.
rizwanreza authored
15 # = Action View Translation Helpers
c9ed2c9 @svenfuchs add a translation helper
svenfuchs authored
16 module Helpers
17 module TranslationHelper
350b977 @smartinez87 Fix styiling issue on TranslationHelper docs
smartinez87 authored
18 # Delegates to <tt>I18n#translate</tt> but also performs three additional functions.
896e25e @svenfuchs Make TranslationHelper#translate use the :rescue_format option in I18…
svenfuchs authored
19 #
350b977 @smartinez87 Fix styiling issue on TranslationHelper docs
smartinez87 authored
20 # First, it'll pass the <tt>:rescue_format => :html</tt> option to I18n so that any
21 # thrown +MissingTranslation+ messages will be turned into inline spans that
896e25e @svenfuchs Make TranslationHelper#translate use the :rescue_format option in I18…
svenfuchs authored
22 #
23 # * have a "translation-missing" class set,
24 # * contain the missing key as a title attribute and
25 # * a titleized version of the last key segment as a text.
26 #
27 # E.g. the value returned for a missing translation key :"blog.post.title" will be
72df64b @gnagno now the documentation reflects the actual returned value for a missin…
gnagno authored
28 # <span class="translation_missing" title="translation missing: en.blog.post.title">Title</span>.
87e9e3f @rtlechow Action Pack typos.
rtlechow authored
29 # This way your views will display rather reasonable strings but it will still
896e25e @svenfuchs Make TranslationHelper#translate use the :rescue_format option in I18…
svenfuchs authored
30 # be easy to spot missing translations.
7527cdf @dhh Added partial scoping to TranslationHelper#translate, so if you call …
dhh authored
31 #
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
32 # Second, it'll scope the key by the current partial if the key starts
33 # with a period. So if you call <tt>translate(".foo")</tt> from the
34 # <tt>people/index.html.erb</tt> template, you'll actually be calling
324de19 @rizwanreza Added titles and description.
rizwanreza authored
35 # <tt>I18n.translate("people.index.foo")</tt>. This makes it less repetitive
36 # to translate many keys within the same partials and gives you a simple framework
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
37 # for scoping them consistently. If you don't prepend the key with a period,
324de19 @rizwanreza Added titles and description.
rizwanreza authored
38 # nothing is converted.
5208cc3 Changed translate helper so that it doesn’t mark every translation as…
Craig Davey authored
39 #
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
40 # Third, it'll mark the translation as safe HTML if the key has the suffix
41 # "_html" or the last element of the key is the word "html". For example,
42 # calling translate("footer_html") or translate("footer.html") will return
324de19 @rizwanreza Added titles and description.
rizwanreza authored
43 # a safe HTML string that won't be escaped by other HTML helper methods. This
44 # naming convention helps to identify translations that include HTML tags so that
5208cc3 Changed translate helper so that it doesn’t mark every translation as…
Craig Davey authored
45 # you know what kind of output to expect when you call translate in a template.
2b8eb54 @spastorino Revert "translation method for arrays on TranslationHelper module ret…
spastorino authored
46 def translate(key, options = {})
b696dd7 @spastorino Allow back passing :rescue_format => nil and test added
spastorino authored
47 options.merge!(:rescue_format => :html) unless options.key?(:rescue_format)
c9f528d @lest wrap translate defaults to use translate helper features, closes #1102
lest authored
48 options[:default] = wrap_translate_defaults(options[:default]) if options[:default]
e8d57f3 @lest _html translation should escape interpolated arguments
lest authored
49 if html_safe_translation_key?(key)
50 html_safe_options = options.dup
51 options.except(*I18n::RESERVED_KEYS).each do |name, value|
603a679 @jonleighton Don't html-escape the :count option to translate if it's a Numeric. F…
jonleighton authored
52 unless name == :count && value.is_a?(Numeric)
53 html_safe_options[name] = ERB::Util.html_escape(value.to_s)
54 end
e8d57f3 @lest _html translation should escape interpolated arguments
lest authored
55 end
56 translation = I18n.translate(scope_key_by_partial(key), html_safe_options)
57
58 translation.respond_to?(:html_safe) ? translation.html_safe : translation
f53a6d8 @jeremy i18n: t() handles single keys returning an Array, also
jeremy authored
59 else
e8d57f3 @lest _html translation should escape interpolated arguments
lest authored
60 I18n.translate(scope_key_by_partial(key), options)
f53a6d8 @jeremy i18n: t() handles single keys returning an Array, also
jeremy authored
61 end
c9ed2c9 @svenfuchs add a translation helper
svenfuchs authored
62 end
90a9637 @svenfuchs I18n: add aliases #t and #l to TranslationHelper#translate and #localize
svenfuchs authored
63 alias :t :translate
cf5d6ab Added localize helper method
Luca Guidi authored
64
350b977 @smartinez87 Fix styiling issue on TranslationHelper docs
smartinez87 authored
65 # Delegates to <tt>I18n.localize</tt> with no additional functionality.
cf5d6ab Added localize helper method
Luca Guidi authored
66 def localize(*args)
afc129e @brynary Fix some Ruby warnings: `*' interpreted as argument prefix
brynary authored
67 I18n.localize(*args)
cf5d6ab Added localize helper method
Luca Guidi authored
68 end
90a9637 @svenfuchs I18n: add aliases #t and #l to TranslationHelper#translate and #localize
svenfuchs authored
69 alias :l :localize
7527cdf @dhh Added partial scoping to TranslationHelper#translate, so if you call …
dhh authored
70
71 private
2b8eb54 @spastorino Revert "translation method for arrays on TranslationHelper module ret…
spastorino authored
72 def scope_key_by_partial(key)
b5f9c88 @spastorino translate method doesn't support arrays anymore and is optimized to b…
spastorino authored
73 if key.to_s.first == "."
bebaccd @josevalim Remove dependency from _template.
josevalim authored
74 if @virtual_path
75 @virtual_path.gsub(%r{/_?}, ".") + key.to_s
dc57d54 @josevalim Fix t('.helper').
josevalim authored
76 else
2b8eb54 @spastorino Revert "translation method for arrays on TranslationHelper module ret…
spastorino authored
77 raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
dc57d54 @josevalim Fix t('.helper').
josevalim authored
78 end
2b8eb54 @spastorino Revert "translation method for arrays on TranslationHelper module ret…
spastorino authored
79 else
80 key
7527cdf @dhh Added partial scoping to TranslationHelper#translate, so if you call …
dhh authored
81 end
82 end
5208cc3 Changed translate helper so that it doesn’t mark every translation as…
Craig Davey authored
83
2b8eb54 @spastorino Revert "translation method for arrays on TranslationHelper module ret…
spastorino authored
84 def html_safe_translation_key?(key)
b5f9c88 @spastorino translate method doesn't support arrays anymore and is optimized to b…
spastorino authored
85 key.to_s =~ /(\b|_|\.)html$/
5208cc3 Changed translate helper so that it doesn’t mark every translation as…
Craig Davey authored
86 end
c9f528d @lest wrap translate defaults to use translate helper features, closes #1102
lest authored
87
88 def wrap_translate_defaults(defaults)
89 new_defaults = []
90 defaults = Array(defaults)
91 while key = defaults.shift
92 if key.is_a?(Symbol)
93 new_defaults << lambda { |_, options| translate key, options.merge(:default => defaults) }
94 break
95 else
96 new_defautls << key
97 end
98 end
99
100 new_defaults
101 end
c9ed2c9 @svenfuchs add a translation helper
svenfuchs authored
102 end
103 end
af05420 @spastorino i18n translate with arrays issue solved
spastorino authored
104 end
Something went wrong with that request. Please try again.