-
Notifications
You must be signed in to change notification settings - Fork 107
/
Rakefile
62 lines (52 loc) · 2.01 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require 'pathname'
require 'yaml'
require 'rspec/core/rake_task'
task default: [:spec]
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = ENV.fetch('SPEC', 'spec/**/*_spec.rb')
t.ruby_opts = '-Ispec'
t.rspec_opts = '--color --backtrace'
t.rspec_opts << " --seed #{ENV['SEED']}" if ENV['SEED']
end
namespace :i18n do
desc 'Fix locale files after tx pull'
task :fix do
Pathname.glob('config/locales/*.yml').each do |file|
data = YAML.safe_load(file.read)
# Transifex exports data inside the YAML using underscored
# identifiers (e.g. pt_BR), but Redmine needs dashed identifiers
# (pt-bR). We need convert each top-level key to use dashes.
#
# rubocop: We cannot modify a hash when iterating using
# `#each_key`. Therefore, we must us `keys.each` here.
data.keys.each do |key| # rubocop:disable Style/HashEachMethods
if key.include?('_')
data[key.tr('_', '-')] = data.delete(key)
end
end
fix_pluralizations = lambda do |locale_data|
# Redmine does not contain pluralization rules, but many
# languages only need an `other` key according to Unicode. Tools
# like transifex expect proper pluralization rules and do not
# export a `one` key when only an `other` key is needed, such as
# for Japanese.
#
# This method searches the locale data for any nested dicitonary
# that looks like a pluralized section (contains only `one`,
# `few`, `other` `many`, `zero` keys). If found, an `one` key
# will be added with the `other` value.
locale_data.each_value do |value|
next unless value.is_a?(Hash)
if (value.keys - %w[one few other many zero]).empty?
value['one'] ||= value['other'] if value.key?('other')
else
fix_pluralizations.call(value)
end
end
end
fix_pluralizations.call(data)
file.write(YAML.dump(data, line_width: -1))
end
end
end