Class: AwesomeTranslations::Translation
- Inherits:
-
Object
- Object
- AwesomeTranslations::Translation
- Defined in:
- app/models/awesome_translations/translation.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#full_path ⇒ Object
readonly
Returns the value of attribute full_path.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#key_show ⇒ Object
readonly
Returns the value of attribute key_show.
-
#line_no ⇒ Object
readonly
Returns the value of attribute line_no.
Instance Method Summary collapse
- #array_key ⇒ Object
- #array_no ⇒ Object
- #array_translation? ⇒ Boolean
- #file_line_content ⇒ Object
- #file_line_content? ⇒ Boolean
- #finished? ⇒ Boolean
-
#initialize(data) ⇒ Translation
constructor
A new instance of Translation.
- #inspect ⇒ Object
- #key_show_with_fallback ⇒ Object
- #last_key ⇒ Object
- #name ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
- #translated_value_for_locale(locale) ⇒ Object
- #translated_values ⇒ Object
- #value(args = {}) ⇒ Object
- #value_for?(locale) ⇒ Boolean
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
#default ⇒ Object (readonly)
Returns the value of attribute default.
2 3 4 |
# File 'app/models/awesome_translations/translation.rb', line 2 def default @default end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
2 3 4 |
# File 'app/models/awesome_translations/translation.rb', line 2 def dir @dir end |
#file_path ⇒ Object (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_path ⇒ Object (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 |
#key ⇒ Object (readonly)
Returns the value of attribute key.
2 3 4 |
# File 'app/models/awesome_translations/translation.rb', line 2 def key @key end |
#key_show ⇒ Object (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_no ⇒ Object (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_key ⇒ Object
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_no ⇒ Object
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
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_content ⇒ Object
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
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
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 |
#inspect ⇒ Object
135 136 137 |
# File 'app/models/awesome_translations/translation.rb', line 135 def inspect to_s end |
#key_show_with_fallback ⇒ Object
21 22 23 |
# File 'app/models/awesome_translations/translation.rb', line 21 def key_show_with_fallback @key_show.presence || last_key end |
#last_key ⇒ Object
17 18 19 |
# File 'app/models/awesome_translations/translation.rb', line 17 def last_key key.to_s.split(".").last end |
#name ⇒ Object
47 48 49 |
# File 'app/models/awesome_translations/translation.rb', line 47 def name key end |
#to_param ⇒ Object
43 44 45 |
# File 'app/models/awesome_translations/translation.rb', line 43 def to_param id end |
#to_s ⇒ Object
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_values ⇒ Object
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
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 |