Class: Translations::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/translations/cli.rb

Instance Method Summary collapse

Instance Method Details

#add(key) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/translations/cli.rb', line 86

def add key
  translations = serializer.translations

  answer = ask "#{translations.master.locale}?"

  if answer.length > 0
    translations.master[key] = answer
  else
    say "You have to provide a translation for master"

    return
  end

  translations.slaves.each do |translation|
    answer = ask "#{translation.locale}?"

    if answer.length > 0
      translation[key] = answer
    end
  end

  serializer.save translations
end

#change(key) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/translations/cli.rb', line 115

def change key
  translations = serializer.translations

  say "#{translations.master.locale}: #{translations.master[key]}"
  answer = ask "#{translations.master.locale}?"

  if answer.length > 0
    translations.master[key] = answer
  else
    say "You have to provide a translation for master"

    return
  end

  translations.slaves.each do |translation|
    if translation.has_key?(key)
      say "#{translation.locale}: #{translation[key]}"
      answer = ask "#{translation.locale}?"

      if answer.length > 0
        translation[key] = answer
      else
        translation.remove key
      end
    end
  end

  serializer.save translations
end

#move(from, to) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/translations/cli.rb', line 74

def move from, to
  translations = serializer.translations

  translations.move from, to

  serializer.save translations
end

#remove(*keys) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/translations/cli.rb', line 63

def remove *keys
  translations = serializer.translations

  keys.each do |key|
    translations.remove key
  end

  serializer.save translations
end

#translate(locale, *keys) ⇒ Object



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
# File 'lib/translations/cli.rb', line 15

def translate locale, *keys
  translations = serializer.translations

  translation = translations.for_locale(locale)

  if keys.length == 0
    keys = translation.missing_keys_from_translation(translations.master)
  end

  master = translations.master

  keys.each do |key|
    say key
    say "#{master.locale}: #{master[key]}"

    translations.slaves.each do |translation|
      answer = ask "#{translation.locale}?"

      if answer.length > 0
        translation[key] = answer
      end

      puts

      serializer.save translations
    end
  end
end

#update(locale, key) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/translations/cli.rb', line 149

def update locale, key
  translations = serializer.translations
  translation = translations.for_locale(locale)

  say "#{locale}: #{translation[key]}"
  answer = ask "#{locale}?"

  translation[key] = answer

  serializer.save translations
end

#validate(locale) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/translations/cli.rb', line 45

def validate locale
  translations = serializer.translations
  translation = translations.for_locale(locale)
  keys = translation.missing_keys_from_translation(translations.master)

  if keys.length > 0
    say "Missing keys in #{locale}"
    say

    keys.each do |key|
      say "- #{key}"
    end

    exit 1
  end
end

#view(key) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/translations/cli.rb', line 162

def view key
  serializer.translations.each do |translation|
    if translation.has_key? key
      say "#{translation.locale}: #{translation[key]}"
    else
      say "#{translation.locale} does not have key #{key}"
    end
  end
end