Module: Atreides::I18nHelpers

Included in:
ActionView::Helpers::TranslationHelper
Defined in:
lib/atreides/i18n_helpers.rb

Overview

Implement a series of helpers to translate Atreides. These are injected into ActionView::Helpers::TranslationHelper

Class Method Summary collapse

Class Method Details

.included(recipient) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/atreides/i18n_helpers.rb', line 9

def self.included(recipient)
  recipient.class_eval do

    def atreides_translate *args
      options = args.extract_options!
      scope_ = options.fetch(:scope, [])
      # puts "ttt: #{args[0]}, scope_: #{scope_.inspect}"
      if args[0].to_s.starts_with?('.') && scope_.include?('atreides')
        # puts "already correctly scoped"
        args << options
        translate(*args)
      else
        # puts "re-scoping"
        raise "NotImplemented" unless scope_.empty?
        options[:scope] = 'atreides'
        args << options
        translate(*args)
      end
    end

    def capitalize(str)
      str.sub str.first, UnicodeUtils.upcase(str.first, I18n.locale)
    end

    def atreides_translate_capitalize(*args)
      capitalize atreides_translate(*args)
    end

    def atreides_translate_titleize(*args)
      if I18n.locale == :en
        UnicodeUtils.titlecase atreides_translate(*args), I18n.locale
      else
        capitalize atreides_translate(*args)
      end
    end

    alias :tt :atreides_translate
    alias :ttc :atreides_translate_capitalize
    alias :ttt :atreides_translate_titleize

    if recipient.respond_to? :helper_method
      helper_method :atreides_translate, :atreides_translate_capitalize, :atreides_translate_titleize
      helper_method :tt, :ttc, :ttt
    end
  end
end