Class: AwesomeTranslations::Translation

Inherits:
Object
  • Object
show all
Defined in:
app/models/awesome_translations/translation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Translation

Returns a new instance of Translation.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/awesome_translations/translation.rb', line 4

def initialize(data)
  @data = data
  @dir = data[:dir]
  @file_path = data[:file_path]
  @full_path = data[:full_path]
  @line_no = data[:line_no]
  @key = data[:key]
  @key_show = data[:key_show]
  @default = data[:default]

  raise "Dir wasn't valid: '#{@dir}'." if @dir.blank?
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



2
3
4
# File 'app/models/awesome_translations/translation.rb', line 2

def default
  @default
end

#dirObject (readonly)

Returns the value of attribute dir.



2
3
4
# File 'app/models/awesome_translations/translation.rb', line 2

def dir
  @dir
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



2
3
4
# File 'app/models/awesome_translations/translation.rb', line 2

def file_path
  @file_path
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



2
3
4
# File 'app/models/awesome_translations/translation.rb', line 2

def full_path
  @full_path
end

#keyObject (readonly)

Returns the value of attribute key.



2
3
4
# File 'app/models/awesome_translations/translation.rb', line 2

def key
  @key
end

#key_showObject (readonly)

Returns the value of attribute key_show.



2
3
4
# File 'app/models/awesome_translations/translation.rb', line 2

def key_show
  @key_show
end

#line_noObject (readonly)

Returns the value of attribute line_no.



2
3
4
# File 'app/models/awesome_translations/translation.rb', line 2

def line_no
  @line_no
end

Instance Method Details

#array_keyObject



31
32
33
34
35
# File 'app/models/awesome_translations/translation.rb', line 31

def array_key
  return unless (match = @key.match(/\A(.+)\[(\d+)\]\Z/))

  match[1]
end

#array_noObject



37
38
39
40
41
# File 'app/models/awesome_translations/translation.rb', line 37

def array_no
  return unless (match = @key.match(/\A(.+)\[(\d+)\]\Z/))

  match[2].to_i
end

#array_translation?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'app/models/awesome_translations/translation.rb', line 25

def array_translation?
  return true if /\[(\d+)\]\Z/.match?(@key)

  false
end

#file_line_contentObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/awesome_translations/translation.rb', line 113

def file_line_content
  count = 0

  File.open(@full_path, "r") do |fp|
    fp.each_line do |line|
      count += 1
      return line if count == @line_no
    end
  end

  nil
end

#file_line_content?Boolean

Returns:

  • (Boolean)


107
108
109
110
111
# File 'app/models/awesome_translations/translation.rb', line 107

def file_line_content?
  return true if @full_path && @line_no && File.exist?(@full_path)

  false
end

#finished?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
# File 'app/models/awesome_translations/translation.rb', line 68

def finished?
  I18n.available_locales.each do |locale|
    next if value_for?(locale)

    return false
  end

  true
end

#inspectObject



135
136
137
# File 'app/models/awesome_translations/translation.rb', line 135

def inspect
  to_s
end

#key_show_with_fallbackObject



21
22
23
# File 'app/models/awesome_translations/translation.rb', line 21

def key_show_with_fallback
  @key_show.presence || last_key
end

#last_keyObject



17
18
19
# File 'app/models/awesome_translations/translation.rb', line 17

def last_key
  key.to_s.split(".").last
end

#nameObject



47
48
49
# File 'app/models/awesome_translations/translation.rb', line 47

def name
  key
end

#to_paramObject



43
44
45
# File 'app/models/awesome_translations/translation.rb', line 43

def to_param
  id
end

#to_sObject



126
127
128
129
130
131
132
133
# File 'app/models/awesome_translations/translation.rb', line 126

def to_s
  "<AwesomeTranslations::Translation " \
    "key=\"#{@key}\" " \
    "dir=\"#{@dir}\" " \
    "array_translation?=\"#{array_translation?}\" " \
    "array_key=\"#{array_key}\" " \
    "array_no=\"#{array_no}\">"
end

#translated_value_for_locale(locale) ⇒ Object



78
79
80
81
82
83
84
85
# File 'app/models/awesome_translations/translation.rb', line 78

def translated_value_for_locale(locale)
  AwesomeTranslations::TranslatedValue.new(
    file: "#{dir}/#{locale}.yml",
    key: @key,
    locale: locale,
    value: value_for?(locale) ? value(locale: locale) : ""
  )
end

#translated_valuesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/awesome_translations/translation.rb', line 51

def translated_values
  result = []

  I18n.available_locales.each do |locale|
    next unless value_for?(locale)

    result << AwesomeTranslations::TranslatedValue.new(
      file: "#{dir}/#{locale}.yml",
      key: @key,
      locale: locale,
      value: value(locale: locale)
    )
  end

  result
end

#value(args = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/awesome_translations/translation.rb', line 95

def value(args = {})
  locale = (args[:locale] || I18n.locale || I18n.default_locale).to_sym

  return nil unless value_for?(locale)

  if array_translation?
    I18n.with_locale(locale) { I18n.t(array_key, default: [])[array_no] }
  else
    I18n.with_locale(locale) { I18n.t(@key, default: nil, fallback: false) }
  end
end

#value_for?(locale) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'app/models/awesome_translations/translation.rb', line 87

def value_for?(locale)
  if array_translation?
    I18n.with_locale(locale) { I18n.exists?(array_key) && I18n.t(array_key)[array_no].present? }
  else
    I18n.with_locale(locale) { I18n.exists?(@key) }
  end
end