Class: I18nChecker::Locale::File

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_checker/locale/file.rb

Overview

Translation file for each language

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_file, locale_texts = {}) ⇒ File

Create a translation file

Parameters:

  • yaml_file (String)

    Translation file path

  • locale_texts (Hash<String,String>) (defaults to: {})

    translation text



27
28
29
30
31
32
# File 'lib/i18n_checker/locale/file.rb', line 27

def initialize(yaml_file, locale_texts = {})
  lang = locale_texts.keys.first
  @lang = lang.to_sym
  @locale_texts = compact_of(locale_texts[lang] || {})
  @file_name = yaml_file
end

Instance Attribute Details

#file_nameString (readonly)

Translation file path

Returns:

  • (String)

    the current value of file_name



10
11
12
# File 'lib/i18n_checker/locale/file.rb', line 10

def file_name
  @file_name
end

#langString (readonly)

language

Returns:

  • (String)

    the current value of lang



10
11
12
# File 'lib/i18n_checker/locale/file.rb', line 10

def lang
  @lang
end

#locale_textsHash<String,String> (readonly)

translation texts

Returns:

  • (Hash<String,String>)

    the current value of locale_texts



10
11
12
# File 'lib/i18n_checker/locale/file.rb', line 10

def locale_texts
  @locale_texts
end

Class Method Details

.load_yaml_file(yaml_file) ⇒ I18nChecker::Locale::File

Read translation file

Parameters:

  • yaml_file (String)

    Translation file path

Returns:



18
19
20
# File 'lib/i18n_checker/locale/file.rb', line 18

def load_yaml_file(yaml_file)
  new(yaml_file, YAML.load(::File.open(yaml_file, &:read)))
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/i18n_checker/locale/file.rb', line 70

def empty?
  locale_texts.empty?
end

#include?(locale_text) ⇒ Boolean

Check for translated text

Parameters:

  • locale_text (Stirng)

    Translation text key

Returns:

  • (Boolean)

    Returns true if there is translated text



38
39
40
# File 'lib/i18n_checker/locale/file.rb', line 38

def include?(locale_text)
  @locale_texts.key?(locale_text.text)
end

#remove_texts(locale_texts) ⇒ I18nChecker::Locale::File

Deletes the specified translation text

Parameters:

  • locale_text (Array<Stirng>)

    Translation text key

Returns:



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/i18n_checker/locale/file.rb', line 46

def remove_texts(locale_texts)
  registry = locale_texts.map { |locale_text| [locale_text, true] }.to_h

  current_locale_texts = @locale_texts.dup
  current_locale_texts.delete_if { |locale_text| registry.key?(locale_text) }

  remain_texts = {}
  remain_texts[@lang] = current_locale_texts

  self.class.new(file_name, remain_texts)
end

#saveI18nChecker::Locale::File

Save the translation file

Returns:



61
62
63
64
65
66
67
68
# File 'lib/i18n_checker/locale/file.rb', line 61

def save
  locale = {}
  locale[@lang.to_s] = tree_of(locale_texts)
  yaml_file = ::File.open(file_name, 'w')
  yaml_file.write(YAML.dump(locale))
  yaml_file.close
  self
end