Class: I18nChecker::Locale::File
- Inherits:
-
Object
- Object
- I18nChecker::Locale::File
- Defined in:
- lib/i18n_checker/locale/file.rb
Overview
Translation file for each language
Instance Attribute Summary collapse
-
#file_name ⇒ String
readonly
Translation file path.
-
#lang ⇒ String
readonly
language.
-
#locale_texts ⇒ Hash<String,String>
readonly
translation texts.
Class Method Summary collapse
-
.load_yaml_file(yaml_file) ⇒ I18nChecker::Locale::File
Read translation file.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#include?(locale_text) ⇒ Boolean
Check for translated text.
-
#initialize(yaml_file, locale_texts = {}) ⇒ File
constructor
Create a translation file.
-
#remove_texts(locale_texts) ⇒ I18nChecker::Locale::File
Deletes the specified translation text.
-
#save ⇒ I18nChecker::Locale::File
Save the translation file.
Constructor Details
#initialize(yaml_file, locale_texts = {}) ⇒ File
Create a translation file
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_name ⇒ String (readonly)
Translation file path
10 11 12 |
# File 'lib/i18n_checker/locale/file.rb', line 10 def file_name @file_name end |
#lang ⇒ String (readonly)
language
10 11 12 |
# File 'lib/i18n_checker/locale/file.rb', line 10 def lang @lang end |
#locale_texts ⇒ Hash<String,String> (readonly)
translation 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
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
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
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
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 |
#save ⇒ I18nChecker::Locale::File
Save the translation file
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 |