1. 16 9月, 2020 1 次提交
    • J
      Improve Action View `translate` helper · d81926fd
      Jonathan Hefner 提交于
      This disentangles the control flow between Action View's `translate` and
      I18n's `translate`.  In doing so, it fixes a handful of corner cases,
      for which tests have now been added.  It also reduces memory
      allocations, and improves speed when using a default:
      
      **Memory**
      
      ```ruby
      require "benchmark/memory"
      
      Benchmark.memory do |x|
        x.report("warmup") { translate(:"translations.foo"); translate(:"translations.html") }
        x.report("text") { translate(:"translations.foo") }
        x.report("html") { translate(:"translations.html") }
        x.report("text 1 default") { translate(:"translations.missing", default: :"translations.foo") }
        x.report("html 1 default") { translate(:"translations.missing", default: :"translations.html") }
        x.report("text 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.foo"]) }
        x.report("html 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.html"]) }
      end
      ```
      
      Before:
      
      ```
                      text     1.240k memsize (     0.000  retained)
                              13.000  objects (     0.000  retained)
                               2.000  strings (     0.000  retained)
                      html     1.600k memsize (     0.000  retained)
                              19.000  objects (     0.000  retained)
                               2.000  strings (     0.000  retained)
            text 1 default     4.728k memsize (     1.200k retained)
                              39.000  objects (     4.000  retained)
                               5.000  strings (     0.000  retained)
            html 1 default     5.056k memsize (     1.160k retained)
                              41.000  objects (     3.000  retained)
                               4.000  strings (     0.000  retained)
           text 2 defaults     7.464k memsize (     2.392k retained)
                              54.000  objects (     6.000  retained)
                               4.000  strings (     0.000  retained)
           html 2 defaults     7.944k memsize (     2.384k retained)
                              60.000  objects (     6.000  retained)
                               4.000  strings (     0.000  retained)
      ```
      
      After:
      
      ```
                      text   952.000  memsize (     0.000  retained)
                               9.000  objects (     0.000  retained)
                               1.000  strings (     0.000  retained)
                      html     1.008k memsize (     0.000  retained)
                              10.000  objects (     0.000  retained)
                               1.000  strings (     0.000  retained)
            text 1 default     2.400k memsize (    40.000  retained)
                              24.000  objects (     1.000  retained)
                               4.000  strings (     0.000  retained)
            html 1 default     2.464k memsize (     0.000  retained)
                              22.000  objects (     0.000  retained)
                               2.000  strings (     0.000  retained)
           text 2 defaults     3.232k memsize (     0.000  retained)
                              30.000  objects (     0.000  retained)
                               2.000  strings (     0.000  retained)
           html 2 defaults     3.456k memsize (     0.000  retained)
                              32.000  objects (     0.000  retained)
                               2.000  strings (     0.000  retained)
      ```
      
      **Speed**
      
      ```ruby
      require "benchmark/ips"
      
      Benchmark.ips do |x|
        x.report("text") { translate(:"translations.foo") }
        x.report("html") { translate(:"translations.html") }
        x.report("text 1 default") { translate(:"translations.missing", default: :"translations.foo") }
        x.report("html 1 default") { translate(:"translations.missing", default: :"translations.html") }
        x.report("text 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.foo"]) }
        x.report("html 2 defaults") { translate(:"translations.missing", default: [:"translations.missing", :"translations.html"]) }
      end
      ```
      
      Before:
      
      ```
                      text     35.685k (± 0.7%) i/s -    179.050k in   5.017773s
                      html     28.569k (± 3.1%) i/s -    143.871k in   5.040128s
            text 1 default     13.953k (± 2.0%) i/s -     70.737k in   5.071651s
            html 1 default     12.507k (± 0.4%) i/s -     63.546k in   5.080908s
           text 2 defaults      9.103k (± 0.3%) i/s -     46.308k in   5.087323s
           html 2 defaults      8.570k (± 4.3%) i/s -     43.071k in   5.034322s
      ```
      
      After:
      
      ```
                      text     36.694k (± 2.0%) i/s -    186.864k in   5.094367s
                      html     30.415k (± 0.5%) i/s -    152.900k in   5.027226s
            text 1 default     18.095k (± 2.7%) i/s -     91.086k in   5.036857s
            html 1 default     15.934k (± 1.7%) i/s -     80.223k in   5.036085s
           text 2 defaults     12.179k (± 0.6%) i/s -     61.659k in   5.062910s
           html 2 defaults     11.193k (± 2.1%) i/s -     56.406k in   5.041433s
      ```
      d81926fd
  2. 12 9月, 2020 1 次提交
    • J
      Refactor Action View `translate` helper · 98a76a50
      Jonathan Hefner 提交于
      This refactor incidentally fixes a corner case when `translate` is
      called with a block, the translation is missing, and
      `debug_missing_translation` is false.
      
      This commit adds a test for the above corner case, and additional tests
      for existing behavior.
      98a76a50
  3. 10 9月, 2020 1 次提交
    • J
      Merge pull request from GHSA-cfjv-5498-mph5 · e663f084
      Jonathan Hefner 提交于
      Prior to this commit, when a translation key indicated that the
      translation text was HTML, the value returned by `I18n.translate` would
      always be marked as `html_safe`.  However, the value returned by
      `I18n.translate` could be an untrusted value directly from
      `options[:default]`.
      
      This commit ensures values directly from `options[:default]` are not
      marked as `html_safe`.
      e663f084
  4. 27 8月, 2020 1 次提交
    • S
      Extend `ActionView::Helpers#translate` to yield · 7cf52ae9
      Sean Doyle 提交于
      This commit extends the `ActionView::Helpers#translate` (and by way of
      alias, `#t`) helper methods to accept blocks.
      
      When invoked with a block, the `translate` call will yield the
      translated text as its first block argument, along with the resolved
      translation key as its second:
      
      ```erb
      <%= translate(".key") do |translation, resolved_key| %>
        <span data-i18n-key="<%= resolved_key %>"><%= translation %></span>
      <% end %>
      ```
      
      In cases where relative translation keys are foregone in lieu of fully
      qualified keys, or if the caller is not interested in the resolved key,
      the second block argument can be omitted:
      
      ```erb
      <%= translate("action.template.key") do |translation| %>
        <p><%= translation %></p>
        <p><%= translation %>, but a second time</p>
      <% end %>
      ```
      
      A benefit of yielding the translation is that it enabled template-local
      variable re-use. Alternatively, [`Object#tap`][tap] could be used.
      
      Prior to this commit, however, the resolution of the translation key was
      internal to `ActionView`, and unavailable to the caller (unless they
      were willing to explicitly determine the resolved key themselves). By
      making it available as a block parameter, it could be used to annotate
      the translated value in the resulting elements.
      
      [tap]: https://ruby-doc.org/core-2.7.0/Object.html#method-i-tap
      7cf52ae9
  5. 10 2月, 2020 1 次提交
    • S
      Fix translate method with default: nil · 8877b5ff
      Stefan Wrobel 提交于
          ```ruby
          I18n.translate('missing.translation', default: nil)
          # => nil
          helper.translate('missing.translation', default: nil)
          # Before
          # => "<span class=\"translation_missing\" title=\"translation missing: en.missing.translation\">Translation</span>"
          # After
          # => nil
          ```
      8877b5ff
  6. 05 2月, 2020 2 次提交
  7. 14 6月, 2019 1 次提交
    • J
      Fix TranslationHelper#translate handling of Hash defaults · 5d4a77d3
      Jean Boussier 提交于
      It is sometimes expected of the `translate` methods to return a Hash,
      for instance it's the case of the `number.format` key.
      
      As such users might need to specify a Hash default, e.g.
      
      `translate(:'some.format', default: { separator: '.', delimiter: ',' })`.
      
      This works as expected with the `I18n.translate` methods,
      however `TranslationHelper#translate` apply `Array()` on the default value.
      
      As a result the default value end up as `[:separator, '.', :delimiter, ',']`.
      5d4a77d3
  8. 22 3月, 2019 1 次提交
    • J
      Prefer render template: in tests · f35631d4
      John Hawthorn 提交于
      Many tests were using `render file:`, but were only testing the
      behaviour of `render template:` (file: just allows more paths/ is less
      secure then template:).
      
      The reason for so many `render file:` is probably that they were the old
      default.
      
      This commit replaces `render file:` with `render template:` anywhere the
      test wasn't specifically interested in using `render file:`.
      f35631d4
  9. 07 2月, 2019 2 次提交
  10. 30 1月, 2019 1 次提交
  11. 03 7月, 2018 1 次提交
  12. 26 1月, 2018 1 次提交
  13. 24 7月, 2017 1 次提交
  14. 02 7月, 2017 1 次提交
  15. 01 7月, 2017 1 次提交
  16. 07 8月, 2016 2 次提交
  17. 30 12月, 2015 1 次提交
  18. 19 12月, 2015 1 次提交
    • S
      debug_missing_translation configuration added to action_view · c1dbb13e
      Sameer Rahmani 提交于
      `I18n.translate` helper will wrap the missing translation keys
      in a <span> tag only if `debug_missing_translation` configuration has
      a truthy value. Default value is `true`. For example in `application.rb`:
      
          # in order to turn off missing key wrapping
          config.action_view.debug_missing_translation = false
      c1dbb13e
  19. 29 10月, 2015 1 次提交
  20. 19 9月, 2015 1 次提交
  21. 24 8月, 2015 1 次提交
  22. 05 5月, 2015 2 次提交
  23. 05 4月, 2015 1 次提交
    • A
      Allow an array to be a default translation value. · 6f3c65f6
      Adam Prescott 提交于
      4.2.1 introduced a change to the way `translate`/`t` works with an
      option of `default: [[]]`. In 4.2.0, this would give a default value of
      `[]`, but in 4.2.1, it leads to a missing translation.
      
      `default: [[]]` is again allowed for cases where a default of `[]` is
      needed.
      
      This addresses GitHub issue 19640.
      6f3c65f6
  24. 21 3月, 2015 1 次提交
  25. 27 2月, 2015 1 次提交
    • U
      Fix regression when passing a value different of String. · 362557eb
      Ulisses Almeida 提交于
      The previous version of rails(4.2.0) you can pass objects
      to the default option of translation helper.
      
      For example:
      
      ```ruby
        t('foo', default: 1)
      ```
      
      But on rails 4.2.1 version this kind of use stopped to work,
      because started only to accept String types.
      
      Now with this fix we can use orther value types on this
      helper again.
      362557eb
  26. 06 1月, 2015 1 次提交
  27. 03 1月, 2015 1 次提交
  28. 19 11月, 2014 1 次提交
  29. 06 9月, 2014 1 次提交
  30. 14 6月, 2014 1 次提交
  31. 13 5月, 2014 2 次提交
  32. 27 1月, 2014 1 次提交
  33. 05 12月, 2013 1 次提交
    • S
      Escalate missing error when :raise is true · c1d5477b
      Shota Fukumori (sora_h) 提交于
      Before ec16ba75,
      ActionView::Helpers::TranslationHelper#translate has raised errors with
      specifying options[:raise] to true.
      
      This should work by this fix:
      
           begin
             t(:"translations.missing", raise: true)
           rescue I18n::MissingTranslationData
             p :hello!
           end
      c1d5477b
  34. 03 12月, 2013 1 次提交
    • M
      Stop using i18n's built in HTML error handling. · 0c7ac34a
      Michael Koziarski 提交于
      i18n doesn't depend on active support which means it can't use our html_safe
      code to do its escaping when generating the spans.  Rather than try to sanitize
      the output from i18n, just revert to our old behaviour of rescuing the error
      and constructing the tag ourselves.
      
      Fixes: CVE-2013-4491
      0c7ac34a
  35. 20 6月, 2013 1 次提交