Class: MissingT
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Includes:
- Helpers
- Defined in:
- lib/missing_t.rb
Instance Method Summary
collapse
Methods included from Helpers
#blue, #colour, #green, #magenta, #red, #yellow
Constructor Details
attr_reader :translations
58
59
60
|
# File 'lib/missing_t.rb', line 58
def initialize
@translations = Hash.new
end
|
Instance Method Details
#add_translations(trs) ⇒ Object
69
70
71
|
# File 'lib/missing_t.rb', line 69
def add_translations(trs)
translations.deep_safe_merge!(trs)
end
|
#collect_translation_queries ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/missing_t.rb', line 120
def collect_translation_queries
queries = {}
files_with_i18n_queries.each do |file|
queries_in_file = (file)
unless queries_in_file.empty?
queries[file] = queries_in_file
end
end
queries
end
|
#collect_translations ⇒ Object
73
74
75
76
77
78
79
80
|
# File 'lib/missing_t.rb', line 73
def collect_translations
locales_pathes = ["config/locales/**/*.yml", "vendor/plugins/**/config/locales/**/*yml", "vendor/plugins/**/locale/**/*yml"]
locales_pathes.each do |path|
Dir.glob(path) do |file|
add_translations(translations_in_file(file))
end
end
end
|
112
113
114
115
116
117
118
|
# File 'lib/missing_t.rb', line 112
def (file)
i18n_query_pattern = /[^\w]+(?:I18n\.translate|I18n\.t|translate|t)\s*\((.*?)[,\)]/
i18n_query_no_parens_pattern = /[^\w]+(?:I18n\.translate|I18n\.t|translate|t)\s+(['"])(.*?)\1/
file_content = get_content_of_file_with_i18n_queries(file)
file_content.scan(i18n_query_pattern).map { |match| match.first.gsub(/['"\s]/, '') }.
concat(file_content.scan(i18n_query_no_parens_pattern).map { |match| match[1].gsub(/['"\s]/, '') })
end
|
#files_with_i18n_queries ⇒ Object
99
100
101
102
103
|
# File 'lib/missing_t.rb', line 99
def files_with_i18n_queries
[ Dir.glob("app/**/*.erb"),
Dir.glob("app/**/controllers/**/*.rb"),
Dir.glob("app/**/helpers/**/*.rb")].flatten
end
|
#find_missing_translations(lang = nil) ⇒ Object
153
154
155
156
|
# File 'lib/missing_t.rb', line 153
def find_missing_translations(lang=nil)
collect_translations
get_missing_translations(collect_translation_queries, lang)
end
|
#get_content_of_file_with_i18n_queries(file) ⇒ Object
105
106
107
108
109
110
|
# File 'lib/missing_t.rb', line 105
def get_content_of_file_with_i18n_queries(file)
f = open(File.expand_path(file), "r")
content = f.read()
f.close()
content
end
|
#get_missing_translations(queries, lang = nil) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/missing_t.rb', line 141
def get_missing_translations(queries, lang=nil)
missing = {}
languages = lang.nil? ? translations.keys : [lang]
languages.each do |l|
get_missing_translations_for_lang(queries, l).each do |file, qs|
missing[file] ||= []
missing[file].concat(qs).uniq!
end
end
missing
end
|
#has_translation?(lang, query) ⇒ Boolean
132
133
134
135
136
137
138
139
|
# File 'lib/missing_t.rb', line 132
def has_translation?(lang, query)
t = translations
i18n_label(lang, query).split('.').each do |segment|
return false unless (t.respond_to?(:key?) and t.key?(segment))
t = t[segment]
end
true
end
|
#hashify(strings) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/missing_t.rb', line 82
def hashify(strings)
h = Hash.new
strings.map { |s| s.split('.') }.
each do |segmented_string|
root = h
segmented_string.each do |segment|
root[segment] ||= {}
root = root[segment]
end
end
h
end
|
#translations ⇒ Object
NOTE: this method is needed because attr_reader :translations does not seem to be stubbable
65
66
67
|
# File 'lib/missing_t.rb', line 65
def translations
@translations
end
|
#translations_in_file(yaml_file) ⇒ Object
95
96
97
|
# File 'lib/missing_t.rb', line 95
def translations_in_file(yaml_file)
open(yaml_file) { |f| YAML.load(f.read) }
end
|