Class: MissingTranslation::TranslationFile

Inherits:
Object
  • Object
show all
Defined in:
lib/missing_translation/translation_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pathname = nil) ⇒ TranslationFile



9
10
11
12
13
14
15
16
17
# File 'lib/missing_translation/translation_file.rb', line 9

def initialize(pathname = nil)
  return unless pathname

  @pathname = pathname
  @file = File.open(pathname)
  if @file
    @content = (YAML.load(@file.read) || {})
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/missing_translation/translation_file.rb', line 7

def content
  @content
end

#groupObject

Returns the value of attribute group.



7
8
9
# File 'lib/missing_translation/translation_file.rb', line 7

def group
  @group
end

#languageObject

Returns the value of attribute language.



7
8
9
# File 'lib/missing_translation/translation_file.rb', line 7

def language
  @language
end

#locale_prefixObject

Returns the value of attribute locale_prefix.



7
8
9
# File 'lib/missing_translation/translation_file.rb', line 7

def locale_prefix
  @locale_prefix
end

#pathnameObject

Returns the value of attribute pathname.



7
8
9
# File 'lib/missing_translation/translation_file.rb', line 7

def pathname
  @pathname
end

#translationsObject

Returns the value of attribute translations.



7
8
9
# File 'lib/missing_translation/translation_file.rb', line 7

def translations
  @translations
end

Instance Method Details

#to_jsonObject



19
20
21
22
23
24
25
26
# File 'lib/missing_translation/translation_file.rb', line 19

def to_json
  return nil unless @content
  
  hash_content = @content.to_hash
  return hash_content[language] if hash_content.has_key?(language)
  
  hash_content
end

#write(directory) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/missing_translation/translation_file.rb', line 43

def write(directory)
  return nil unless translations

  filename = [group, language, "yml"].compact.reject{|s| s.size <= 0}.join('.')
  filepath = "#{directory}/#{filename}"
  p "Writing file #{filepath}..."
  if locale_prefix && translations.keys.length >= 1 && translations.keys.first != language
    File.write(filepath, {language => translations}.to_yaml)
  else
    File.write(filepath, translations.to_yaml)
  end
  MissingTranslation::Util.sort_file(filepath)
end