Class: Rosette::Core::Commands::ExportCommand

Inherits:
GitCommand show all
Includes:
WithLocale, WithRef, WithRepoName, WithSnapshots
Defined in:
lib/rosette/core/commands/translations/export_command.rb

Overview

Finds, encodes, and serializes the translations identified by a snapshot of the given git ref or commit id. In other words, this command exports the translations for a git branch or commit. This command also applies any configured pre-processors to the translations before serializing them. As a better visualization, here's the pipeline translations go through when exported:

preprocessed -> serialized/encoded -> base 64 encoded (if requested) -> returned

Examples:

cmd = ExportCommand.new(configuration)
  .set_repo_name('my_repo')
  .set_ref('master')
  .set_locale('pt-BR')
  .set_serializer('json/key-value')
  .set_base_64_encode(true)
  .set_encoding(Encoding::UTF_8)
  .set_include_snapshot(false)

cmd.execute
# =>
# {
#   payload: "<base 64 encoded string>",
#   encoding: "UTF_8"
#   translation_count: 105,
#   base_64_encoded: true
#   locale: "pt-BR"
# }

Instance Attribute Summary collapse

Attributes included from WithRef

#commit_str

Attributes included from WithRepoName

#repo_name

Attributes inherited from Command

#configuration

Instance Method Summary collapse

Methods included from WithSnapshots

#take_snapshot

Methods included from WithLocale

#set_locale

Methods included from WithRef

#commit_id, #set_commit_id, #set_ref

Methods included from WithRepoName

#set_repo_name

Methods inherited from GitCommand

#valid?

Methods inherited from Command

#messages, #valid?, validate, validators

Constructor Details

#initialize(*args) ⇒ ExportCommand

Returns a new instance of ExportCommand.


78
79
80
81
82
83
84
85
# File 'lib/rosette/core/commands/translations/export_command.rb', line 78

def initialize(*args)
  super
  @paths = []
  @encoding = Encoding::UTF_8
  @base_64_encode = false
  @include_snapshot = false
  @include_checksum = false
end

Instance Attribute Details

#base_64_encodeBoolean (readonly)

Returns whether or not the serialized translations should be returned encoded in base 64.

Returns:

  • (Boolean)

    whether or not the serialized translations should be returned encoded in base 64.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rosette/core/commands/translations/export_command.rb', line 64

class ExportCommand < GitCommand
  attr_reader :locale, :serializer, :base_64_encode
  attr_reader :encoding, :include_snapshot, :include_checksum
  attr_reader :paths

  include WithRepoName
  include WithRef
  include WithLocale

  include WithSnapshots

  validate :serializer, type: :serializer
  validate :encoding, type: :encoding

  def initialize(*args)
    super
    @paths = []
    @encoding = Encoding::UTF_8
    @base_64_encode = false
    @include_snapshot = false
    @include_checksum = false
  end

  # Sets the serializer used to export translations. Must be recognizable
  # as a serializer id, eg. 'yaml/rails' or 'json/key-value'.
  #
  # @param [String] serializer The serializer to use.
  # @return [self]
  def set_serializer(serializer)
    @serializer = serializer
    self
  end

  # Sets whether or not the serialized translations should be returned
  # encoded in base 64.
  #
  # @param [Boolean] should_encode To encode or not encode, that is
  #   the question.
  # @return [self]
  def set_base_64_encode(should_encode)
    @base_64_encode = should_encode
    self
  end

  # Sets the encoding translations are expected to be in. Not to be
  # confused with base 64 encoding.
  #
  # @param [String, Encoding] encoding The encoding to use. Can be
  #   either a +String+ or a Ruby +Encoding+, eg. +Encoding::UTF_8+.
  # @return [self]
  def set_encoding(encoding)
    @encoding = encoding
    self
  end

  # Sets whether or not to include the snapshot in the return value.
  #
  # @param [Boolean] should_include_snapshot whether or not to
  #   return the snapshot.
  # @return [self]
  def set_include_snapshot(should_include_snapshot)
    @include_snapshot = should_include_snapshot
    self
  end

  # Sets whether or not to include a checksum of the phrases in the
  # return value.
  #
  # @param [Boolean] should_include_checksum whether or not to include
  #   the checksum.
  # @return [self]
  def set_include_checksum(should_include_checksum)
    @include_checksum = should_include_checksum
    self
  end

  # A list of files or paths to filter translations by. Only translations
  # matching these paths will be included in the export payload.
  def set_paths(paths)
    @paths = Array(paths)
    self
  end

  # Perform the export.
  #
  # @return [Hash] containing the following attributes:
  #   * +payload+: The serialized +String+ blob of all the translations.
  #   * +encoding+: The encoding of the strings in +payload+.
  #   * +translation_count+: The number of translations in +payload+.
  #   * +base_64_encoded+: A boolean indicating if +payload+ is base
  #     64 encoded.
  #   * +locale+: The locale the translations in +payload+ are written in.
  #   * +snapshot+: The snapshot used to identify the translations in
  def execute
    stream = StringIO.new
    snapshot = take_snapshot(repo_config, commit_id, paths)
    translation_count = 0
    checksum_list = []

    serializer_instance = serializer_config.klass.new(
      stream, locale_obj, encoding
    )

    write_translations_for(snapshot, serializer_instance) do |trans|
      translation_count += 1

      if include_checksum
        checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
      end
    end

    params = {
      payload: encode(stream.string),
      encoding: serializer_instance.encoding.to_s,
      translation_count: translation_count,
      base_64_encoded: base_64_encode,
      locale: locale,
      paths: paths
    }

    if include_snapshot
      params.merge!(snapshot: snapshot)
    end

    if include_checksum
      params.merge!(checksum: checksum_for(checksum_list))
    end

    params
  end

  private

  def write_translations_for(snapshot, serializer_instance)
    each_translation(snapshot) do |trans|
      next unless include_trans?(trans)
      trans = apply_preprocessors(trans, serializer_config)
      yield trans if block_given?

      serializer_instance.write_key_value(
        trans.phrase.index_value, trans.translation
      )
    end

    serializer_instance.flush
  end

  def include_trans?(trans)
    paths.size == 0 || paths.include?(trans.phrase.file)
  end

  def checksum_for(list)
    Digest::MD5.hexdigest(list.sort.join)
  end

  def locale_obj
    @locale_obj ||= repo_config.get_locale(locale)
  end

  def apply_preprocessors(translation, serializer_config)
    serializer_config.preprocessors.inject(translation) do |trans, preprocessor|
      preprocessor.process(trans)
    end
  end

  def encode(string)
    if base_64_encode
      Base64.encode64(string)
    else
      string
    end
  end

  def serializer_config
    @serializer_config ||= repo_config.get_serializer_config(serializer)
  end

  def repo_config
    @repo_config ||= get_repo(repo_name)
  end

  def each_translation(snapshot)
    datastore.phrases_by_commits(repo_name, snapshot) do |phrase|
      # only yield if a translation exists
      if text = repo_config.tms.lookup_translation(locale_obj, phrase)
        yield Translation.new(phrase, locale, text)
      end
    end
  end
end

#encodingString, Encoding (readonly)

Returns the encoding translations are expected to be in. This attribute refers to string encoding and is distinct from base 64 encoding.

Returns:

  • (String, Encoding)

    the encoding translations are expected to be in. This attribute refers to string encoding and is distinct from base 64 encoding.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rosette/core/commands/translations/export_command.rb', line 64

class ExportCommand < GitCommand
  attr_reader :locale, :serializer, :base_64_encode
  attr_reader :encoding, :include_snapshot, :include_checksum
  attr_reader :paths

  include WithRepoName
  include WithRef
  include WithLocale

  include WithSnapshots

  validate :serializer, type: :serializer
  validate :encoding, type: :encoding

  def initialize(*args)
    super
    @paths = []
    @encoding = Encoding::UTF_8
    @base_64_encode = false
    @include_snapshot = false
    @include_checksum = false
  end

  # Sets the serializer used to export translations. Must be recognizable
  # as a serializer id, eg. 'yaml/rails' or 'json/key-value'.
  #
  # @param [String] serializer The serializer to use.
  # @return [self]
  def set_serializer(serializer)
    @serializer = serializer
    self
  end

  # Sets whether or not the serialized translations should be returned
  # encoded in base 64.
  #
  # @param [Boolean] should_encode To encode or not encode, that is
  #   the question.
  # @return [self]
  def set_base_64_encode(should_encode)
    @base_64_encode = should_encode
    self
  end

  # Sets the encoding translations are expected to be in. Not to be
  # confused with base 64 encoding.
  #
  # @param [String, Encoding] encoding The encoding to use. Can be
  #   either a +String+ or a Ruby +Encoding+, eg. +Encoding::UTF_8+.
  # @return [self]
  def set_encoding(encoding)
    @encoding = encoding
    self
  end

  # Sets whether or not to include the snapshot in the return value.
  #
  # @param [Boolean] should_include_snapshot whether or not to
  #   return the snapshot.
  # @return [self]
  def set_include_snapshot(should_include_snapshot)
    @include_snapshot = should_include_snapshot
    self
  end

  # Sets whether or not to include a checksum of the phrases in the
  # return value.
  #
  # @param [Boolean] should_include_checksum whether or not to include
  #   the checksum.
  # @return [self]
  def set_include_checksum(should_include_checksum)
    @include_checksum = should_include_checksum
    self
  end

  # A list of files or paths to filter translations by. Only translations
  # matching these paths will be included in the export payload.
  def set_paths(paths)
    @paths = Array(paths)
    self
  end

  # Perform the export.
  #
  # @return [Hash] containing the following attributes:
  #   * +payload+: The serialized +String+ blob of all the translations.
  #   * +encoding+: The encoding of the strings in +payload+.
  #   * +translation_count+: The number of translations in +payload+.
  #   * +base_64_encoded+: A boolean indicating if +payload+ is base
  #     64 encoded.
  #   * +locale+: The locale the translations in +payload+ are written in.
  #   * +snapshot+: The snapshot used to identify the translations in
  def execute
    stream = StringIO.new
    snapshot = take_snapshot(repo_config, commit_id, paths)
    translation_count = 0
    checksum_list = []

    serializer_instance = serializer_config.klass.new(
      stream, locale_obj, encoding
    )

    write_translations_for(snapshot, serializer_instance) do |trans|
      translation_count += 1

      if include_checksum
        checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
      end
    end

    params = {
      payload: encode(stream.string),
      encoding: serializer_instance.encoding.to_s,
      translation_count: translation_count,
      base_64_encoded: base_64_encode,
      locale: locale,
      paths: paths
    }

    if include_snapshot
      params.merge!(snapshot: snapshot)
    end

    if include_checksum
      params.merge!(checksum: checksum_for(checksum_list))
    end

    params
  end

  private

  def write_translations_for(snapshot, serializer_instance)
    each_translation(snapshot) do |trans|
      next unless include_trans?(trans)
      trans = apply_preprocessors(trans, serializer_config)
      yield trans if block_given?

      serializer_instance.write_key_value(
        trans.phrase.index_value, trans.translation
      )
    end

    serializer_instance.flush
  end

  def include_trans?(trans)
    paths.size == 0 || paths.include?(trans.phrase.file)
  end

  def checksum_for(list)
    Digest::MD5.hexdigest(list.sort.join)
  end

  def locale_obj
    @locale_obj ||= repo_config.get_locale(locale)
  end

  def apply_preprocessors(translation, serializer_config)
    serializer_config.preprocessors.inject(translation) do |trans, preprocessor|
      preprocessor.process(trans)
    end
  end

  def encode(string)
    if base_64_encode
      Base64.encode64(string)
    else
      string
    end
  end

  def serializer_config
    @serializer_config ||= repo_config.get_serializer_config(serializer)
  end

  def repo_config
    @repo_config ||= get_repo(repo_name)
  end

  def each_translation(snapshot)
    datastore.phrases_by_commits(repo_name, snapshot) do |phrase|
      # only yield if a translation exists
      if text = repo_config.tms.lookup_translation(locale_obj, phrase)
        yield Translation.new(phrase, locale, text)
      end
    end
  end
end

#include_checksumBoolean (readonly)

Returns whether or not the checksum of translations is returned alongside the serialized phrases.

Returns:

  • (Boolean)

    whether or not the checksum of translations is returned alongside the serialized phrases.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rosette/core/commands/translations/export_command.rb', line 64

class ExportCommand < GitCommand
  attr_reader :locale, :serializer, :base_64_encode
  attr_reader :encoding, :include_snapshot, :include_checksum
  attr_reader :paths

  include WithRepoName
  include WithRef
  include WithLocale

  include WithSnapshots

  validate :serializer, type: :serializer
  validate :encoding, type: :encoding

  def initialize(*args)
    super
    @paths = []
    @encoding = Encoding::UTF_8
    @base_64_encode = false
    @include_snapshot = false
    @include_checksum = false
  end

  # Sets the serializer used to export translations. Must be recognizable
  # as a serializer id, eg. 'yaml/rails' or 'json/key-value'.
  #
  # @param [String] serializer The serializer to use.
  # @return [self]
  def set_serializer(serializer)
    @serializer = serializer
    self
  end

  # Sets whether or not the serialized translations should be returned
  # encoded in base 64.
  #
  # @param [Boolean] should_encode To encode or not encode, that is
  #   the question.
  # @return [self]
  def set_base_64_encode(should_encode)
    @base_64_encode = should_encode
    self
  end

  # Sets the encoding translations are expected to be in. Not to be
  # confused with base 64 encoding.
  #
  # @param [String, Encoding] encoding The encoding to use. Can be
  #   either a +String+ or a Ruby +Encoding+, eg. +Encoding::UTF_8+.
  # @return [self]
  def set_encoding(encoding)
    @encoding = encoding
    self
  end

  # Sets whether or not to include the snapshot in the return value.
  #
  # @param [Boolean] should_include_snapshot whether or not to
  #   return the snapshot.
  # @return [self]
  def set_include_snapshot(should_include_snapshot)
    @include_snapshot = should_include_snapshot
    self
  end

  # Sets whether or not to include a checksum of the phrases in the
  # return value.
  #
  # @param [Boolean] should_include_checksum whether or not to include
  #   the checksum.
  # @return [self]
  def set_include_checksum(should_include_checksum)
    @include_checksum = should_include_checksum
    self
  end

  # A list of files or paths to filter translations by. Only translations
  # matching these paths will be included in the export payload.
  def set_paths(paths)
    @paths = Array(paths)
    self
  end

  # Perform the export.
  #
  # @return [Hash] containing the following attributes:
  #   * +payload+: The serialized +String+ blob of all the translations.
  #   * +encoding+: The encoding of the strings in +payload+.
  #   * +translation_count+: The number of translations in +payload+.
  #   * +base_64_encoded+: A boolean indicating if +payload+ is base
  #     64 encoded.
  #   * +locale+: The locale the translations in +payload+ are written in.
  #   * +snapshot+: The snapshot used to identify the translations in
  def execute
    stream = StringIO.new
    snapshot = take_snapshot(repo_config, commit_id, paths)
    translation_count = 0
    checksum_list = []

    serializer_instance = serializer_config.klass.new(
      stream, locale_obj, encoding
    )

    write_translations_for(snapshot, serializer_instance) do |trans|
      translation_count += 1

      if include_checksum
        checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
      end
    end

    params = {
      payload: encode(stream.string),
      encoding: serializer_instance.encoding.to_s,
      translation_count: translation_count,
      base_64_encoded: base_64_encode,
      locale: locale,
      paths: paths
    }

    if include_snapshot
      params.merge!(snapshot: snapshot)
    end

    if include_checksum
      params.merge!(checksum: checksum_for(checksum_list))
    end

    params
  end

  private

  def write_translations_for(snapshot, serializer_instance)
    each_translation(snapshot) do |trans|
      next unless include_trans?(trans)
      trans = apply_preprocessors(trans, serializer_config)
      yield trans if block_given?

      serializer_instance.write_key_value(
        trans.phrase.index_value, trans.translation
      )
    end

    serializer_instance.flush
  end

  def include_trans?(trans)
    paths.size == 0 || paths.include?(trans.phrase.file)
  end

  def checksum_for(list)
    Digest::MD5.hexdigest(list.sort.join)
  end

  def locale_obj
    @locale_obj ||= repo_config.get_locale(locale)
  end

  def apply_preprocessors(translation, serializer_config)
    serializer_config.preprocessors.inject(translation) do |trans, preprocessor|
      preprocessor.process(trans)
    end
  end

  def encode(string)
    if base_64_encode
      Base64.encode64(string)
    else
      string
    end
  end

  def serializer_config
    @serializer_config ||= repo_config.get_serializer_config(serializer)
  end

  def repo_config
    @repo_config ||= get_repo(repo_name)
  end

  def each_translation(snapshot)
    datastore.phrases_by_commits(repo_name, snapshot) do |phrase|
      # only yield if a translation exists
      if text = repo_config.tms.lookup_translation(locale_obj, phrase)
        yield Translation.new(phrase, locale, text)
      end
    end
  end
end

#include_snapshotBoolean (readonly)

Returns whether or not the snapshot used to identify translations is returned alongside the serialized phrases.

Returns:

  • (Boolean)

    whether or not the snapshot used to identify translations is returned alongside the serialized phrases.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rosette/core/commands/translations/export_command.rb', line 64

class ExportCommand < GitCommand
  attr_reader :locale, :serializer, :base_64_encode
  attr_reader :encoding, :include_snapshot, :include_checksum
  attr_reader :paths

  include WithRepoName
  include WithRef
  include WithLocale

  include WithSnapshots

  validate :serializer, type: :serializer
  validate :encoding, type: :encoding

  def initialize(*args)
    super
    @paths = []
    @encoding = Encoding::UTF_8
    @base_64_encode = false
    @include_snapshot = false
    @include_checksum = false
  end

  # Sets the serializer used to export translations. Must be recognizable
  # as a serializer id, eg. 'yaml/rails' or 'json/key-value'.
  #
  # @param [String] serializer The serializer to use.
  # @return [self]
  def set_serializer(serializer)
    @serializer = serializer
    self
  end

  # Sets whether or not the serialized translations should be returned
  # encoded in base 64.
  #
  # @param [Boolean] should_encode To encode or not encode, that is
  #   the question.
  # @return [self]
  def set_base_64_encode(should_encode)
    @base_64_encode = should_encode
    self
  end

  # Sets the encoding translations are expected to be in. Not to be
  # confused with base 64 encoding.
  #
  # @param [String, Encoding] encoding The encoding to use. Can be
  #   either a +String+ or a Ruby +Encoding+, eg. +Encoding::UTF_8+.
  # @return [self]
  def set_encoding(encoding)
    @encoding = encoding
    self
  end

  # Sets whether or not to include the snapshot in the return value.
  #
  # @param [Boolean] should_include_snapshot whether or not to
  #   return the snapshot.
  # @return [self]
  def set_include_snapshot(should_include_snapshot)
    @include_snapshot = should_include_snapshot
    self
  end

  # Sets whether or not to include a checksum of the phrases in the
  # return value.
  #
  # @param [Boolean] should_include_checksum whether or not to include
  #   the checksum.
  # @return [self]
  def set_include_checksum(should_include_checksum)
    @include_checksum = should_include_checksum
    self
  end

  # A list of files or paths to filter translations by. Only translations
  # matching these paths will be included in the export payload.
  def set_paths(paths)
    @paths = Array(paths)
    self
  end

  # Perform the export.
  #
  # @return [Hash] containing the following attributes:
  #   * +payload+: The serialized +String+ blob of all the translations.
  #   * +encoding+: The encoding of the strings in +payload+.
  #   * +translation_count+: The number of translations in +payload+.
  #   * +base_64_encoded+: A boolean indicating if +payload+ is base
  #     64 encoded.
  #   * +locale+: The locale the translations in +payload+ are written in.
  #   * +snapshot+: The snapshot used to identify the translations in
  def execute
    stream = StringIO.new
    snapshot = take_snapshot(repo_config, commit_id, paths)
    translation_count = 0
    checksum_list = []

    serializer_instance = serializer_config.klass.new(
      stream, locale_obj, encoding
    )

    write_translations_for(snapshot, serializer_instance) do |trans|
      translation_count += 1

      if include_checksum
        checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
      end
    end

    params = {
      payload: encode(stream.string),
      encoding: serializer_instance.encoding.to_s,
      translation_count: translation_count,
      base_64_encoded: base_64_encode,
      locale: locale,
      paths: paths
    }

    if include_snapshot
      params.merge!(snapshot: snapshot)
    end

    if include_checksum
      params.merge!(checksum: checksum_for(checksum_list))
    end

    params
  end

  private

  def write_translations_for(snapshot, serializer_instance)
    each_translation(snapshot) do |trans|
      next unless include_trans?(trans)
      trans = apply_preprocessors(trans, serializer_config)
      yield trans if block_given?

      serializer_instance.write_key_value(
        trans.phrase.index_value, trans.translation
      )
    end

    serializer_instance.flush
  end

  def include_trans?(trans)
    paths.size == 0 || paths.include?(trans.phrase.file)
  end

  def checksum_for(list)
    Digest::MD5.hexdigest(list.sort.join)
  end

  def locale_obj
    @locale_obj ||= repo_config.get_locale(locale)
  end

  def apply_preprocessors(translation, serializer_config)
    serializer_config.preprocessors.inject(translation) do |trans, preprocessor|
      preprocessor.process(trans)
    end
  end

  def encode(string)
    if base_64_encode
      Base64.encode64(string)
    else
      string
    end
  end

  def serializer_config
    @serializer_config ||= repo_config.get_serializer_config(serializer)
  end

  def repo_config
    @repo_config ||= get_repo(repo_name)
  end

  def each_translation(snapshot)
    datastore.phrases_by_commits(repo_name, snapshot) do |phrase|
      # only yield if a translation exists
      if text = repo_config.tms.lookup_translation(locale_obj, phrase)
        yield Translation.new(phrase, locale, text)
      end
    end
  end
end

#localeString (readonly)

Returns the locale to export translations for.

Returns:

  • (String)

    the locale to export translations for.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rosette/core/commands/translations/export_command.rb', line 64

class ExportCommand < GitCommand
  attr_reader :locale, :serializer, :base_64_encode
  attr_reader :encoding, :include_snapshot, :include_checksum
  attr_reader :paths

  include WithRepoName
  include WithRef
  include WithLocale

  include WithSnapshots

  validate :serializer, type: :serializer
  validate :encoding, type: :encoding

  def initialize(*args)
    super
    @paths = []
    @encoding = Encoding::UTF_8
    @base_64_encode = false
    @include_snapshot = false
    @include_checksum = false
  end

  # Sets the serializer used to export translations. Must be recognizable
  # as a serializer id, eg. 'yaml/rails' or 'json/key-value'.
  #
  # @param [String] serializer The serializer to use.
  # @return [self]
  def set_serializer(serializer)
    @serializer = serializer
    self
  end

  # Sets whether or not the serialized translations should be returned
  # encoded in base 64.
  #
  # @param [Boolean] should_encode To encode or not encode, that is
  #   the question.
  # @return [self]
  def set_base_64_encode(should_encode)
    @base_64_encode = should_encode
    self
  end

  # Sets the encoding translations are expected to be in. Not to be
  # confused with base 64 encoding.
  #
  # @param [String, Encoding] encoding The encoding to use. Can be
  #   either a +String+ or a Ruby +Encoding+, eg. +Encoding::UTF_8+.
  # @return [self]
  def set_encoding(encoding)
    @encoding = encoding
    self
  end

  # Sets whether or not to include the snapshot in the return value.
  #
  # @param [Boolean] should_include_snapshot whether or not to
  #   return the snapshot.
  # @return [self]
  def set_include_snapshot(should_include_snapshot)
    @include_snapshot = should_include_snapshot
    self
  end

  # Sets whether or not to include a checksum of the phrases in the
  # return value.
  #
  # @param [Boolean] should_include_checksum whether or not to include
  #   the checksum.
  # @return [self]
  def set_include_checksum(should_include_checksum)
    @include_checksum = should_include_checksum
    self
  end

  # A list of files or paths to filter translations by. Only translations
  # matching these paths will be included in the export payload.
  def set_paths(paths)
    @paths = Array(paths)
    self
  end

  # Perform the export.
  #
  # @return [Hash] containing the following attributes:
  #   * +payload+: The serialized +String+ blob of all the translations.
  #   * +encoding+: The encoding of the strings in +payload+.
  #   * +translation_count+: The number of translations in +payload+.
  #   * +base_64_encoded+: A boolean indicating if +payload+ is base
  #     64 encoded.
  #   * +locale+: The locale the translations in +payload+ are written in.
  #   * +snapshot+: The snapshot used to identify the translations in
  def execute
    stream = StringIO.new
    snapshot = take_snapshot(repo_config, commit_id, paths)
    translation_count = 0
    checksum_list = []

    serializer_instance = serializer_config.klass.new(
      stream, locale_obj, encoding
    )

    write_translations_for(snapshot, serializer_instance) do |trans|
      translation_count += 1

      if include_checksum
        checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
      end
    end

    params = {
      payload: encode(stream.string),
      encoding: serializer_instance.encoding.to_s,
      translation_count: translation_count,
      base_64_encoded: base_64_encode,
      locale: locale,
      paths: paths
    }

    if include_snapshot
      params.merge!(snapshot: snapshot)
    end

    if include_checksum
      params.merge!(checksum: checksum_for(checksum_list))
    end

    params
  end

  private

  def write_translations_for(snapshot, serializer_instance)
    each_translation(snapshot) do |trans|
      next unless include_trans?(trans)
      trans = apply_preprocessors(trans, serializer_config)
      yield trans if block_given?

      serializer_instance.write_key_value(
        trans.phrase.index_value, trans.translation
      )
    end

    serializer_instance.flush
  end

  def include_trans?(trans)
    paths.size == 0 || paths.include?(trans.phrase.file)
  end

  def checksum_for(list)
    Digest::MD5.hexdigest(list.sort.join)
  end

  def locale_obj
    @locale_obj ||= repo_config.get_locale(locale)
  end

  def apply_preprocessors(translation, serializer_config)
    serializer_config.preprocessors.inject(translation) do |trans, preprocessor|
      preprocessor.process(trans)
    end
  end

  def encode(string)
    if base_64_encode
      Base64.encode64(string)
    else
      string
    end
  end

  def serializer_config
    @serializer_config ||= repo_config.get_serializer_config(serializer)
  end

  def repo_config
    @repo_config ||= get_repo(repo_name)
  end

  def each_translation(snapshot)
    datastore.phrases_by_commits(repo_name, snapshot) do |phrase|
      # only yield if a translation exists
      if text = repo_config.tms.lookup_translation(locale_obj, phrase)
        yield Translation.new(phrase, locale, text)
      end
    end
  end
end

#pathsArray<String> (readonly)

Returns the list of paths to export translations for. Any translations that belong to phrases that did not come from a path in this list will not be included in the export.

Returns:

  • (Array<String>)

    the list of paths to export translations for. Any translations that belong to phrases that did not come from a path in this list will not be included in the export.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rosette/core/commands/translations/export_command.rb', line 64

class ExportCommand < GitCommand
  attr_reader :locale, :serializer, :base_64_encode
  attr_reader :encoding, :include_snapshot, :include_checksum
  attr_reader :paths

  include WithRepoName
  include WithRef
  include WithLocale

  include WithSnapshots

  validate :serializer, type: :serializer
  validate :encoding, type: :encoding

  def initialize(*args)
    super
    @paths = []
    @encoding = Encoding::UTF_8
    @base_64_encode = false
    @include_snapshot = false
    @include_checksum = false
  end

  # Sets the serializer used to export translations. Must be recognizable
  # as a serializer id, eg. 'yaml/rails' or 'json/key-value'.
  #
  # @param [String] serializer The serializer to use.
  # @return [self]
  def set_serializer(serializer)
    @serializer = serializer
    self
  end

  # Sets whether or not the serialized translations should be returned
  # encoded in base 64.
  #
  # @param [Boolean] should_encode To encode or not encode, that is
  #   the question.
  # @return [self]
  def set_base_64_encode(should_encode)
    @base_64_encode = should_encode
    self
  end

  # Sets the encoding translations are expected to be in. Not to be
  # confused with base 64 encoding.
  #
  # @param [String, Encoding] encoding The encoding to use. Can be
  #   either a +String+ or a Ruby +Encoding+, eg. +Encoding::UTF_8+.
  # @return [self]
  def set_encoding(encoding)
    @encoding = encoding
    self
  end

  # Sets whether or not to include the snapshot in the return value.
  #
  # @param [Boolean] should_include_snapshot whether or not to
  #   return the snapshot.
  # @return [self]
  def set_include_snapshot(should_include_snapshot)
    @include_snapshot = should_include_snapshot
    self
  end

  # Sets whether or not to include a checksum of the phrases in the
  # return value.
  #
  # @param [Boolean] should_include_checksum whether or not to include
  #   the checksum.
  # @return [self]
  def set_include_checksum(should_include_checksum)
    @include_checksum = should_include_checksum
    self
  end

  # A list of files or paths to filter translations by. Only translations
  # matching these paths will be included in the export payload.
  def set_paths(paths)
    @paths = Array(paths)
    self
  end

  # Perform the export.
  #
  # @return [Hash] containing the following attributes:
  #   * +payload+: The serialized +String+ blob of all the translations.
  #   * +encoding+: The encoding of the strings in +payload+.
  #   * +translation_count+: The number of translations in +payload+.
  #   * +base_64_encoded+: A boolean indicating if +payload+ is base
  #     64 encoded.
  #   * +locale+: The locale the translations in +payload+ are written in.
  #   * +snapshot+: The snapshot used to identify the translations in
  def execute
    stream = StringIO.new
    snapshot = take_snapshot(repo_config, commit_id, paths)
    translation_count = 0
    checksum_list = []

    serializer_instance = serializer_config.klass.new(
      stream, locale_obj, encoding
    )

    write_translations_for(snapshot, serializer_instance) do |trans|
      translation_count += 1

      if include_checksum
        checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
      end
    end

    params = {
      payload: encode(stream.string),
      encoding: serializer_instance.encoding.to_s,
      translation_count: translation_count,
      base_64_encoded: base_64_encode,
      locale: locale,
      paths: paths
    }

    if include_snapshot
      params.merge!(snapshot: snapshot)
    end

    if include_checksum
      params.merge!(checksum: checksum_for(checksum_list))
    end

    params
  end

  private

  def write_translations_for(snapshot, serializer_instance)
    each_translation(snapshot) do |trans|
      next unless include_trans?(trans)
      trans = apply_preprocessors(trans, serializer_config)
      yield trans if block_given?

      serializer_instance.write_key_value(
        trans.phrase.index_value, trans.translation
      )
    end

    serializer_instance.flush
  end

  def include_trans?(trans)
    paths.size == 0 || paths.include?(trans.phrase.file)
  end

  def checksum_for(list)
    Digest::MD5.hexdigest(list.sort.join)
  end

  def locale_obj
    @locale_obj ||= repo_config.get_locale(locale)
  end

  def apply_preprocessors(translation, serializer_config)
    serializer_config.preprocessors.inject(translation) do |trans, preprocessor|
      preprocessor.process(trans)
    end
  end

  def encode(string)
    if base_64_encode
      Base64.encode64(string)
    else
      string
    end
  end

  def serializer_config
    @serializer_config ||= repo_config.get_serializer_config(serializer)
  end

  def repo_config
    @repo_config ||= get_repo(repo_name)
  end

  def each_translation(snapshot)
    datastore.phrases_by_commits(repo_name, snapshot) do |phrase|
      # only yield if a translation exists
      if text = repo_config.tms.lookup_translation(locale_obj, phrase)
        yield Translation.new(phrase, locale, text)
      end
    end
  end
end

#serializerString (readonly)

Returns the serializer to use when exporting the translations. Must be recognizable as a serializer id, eg. 'yaml/rails' or 'json/key-value'.

Returns:

  • (String)

    the serializer to use when exporting the translations. Must be recognizable as a serializer id, eg. 'yaml/rails' or 'json/key-value'.


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rosette/core/commands/translations/export_command.rb', line 64

class ExportCommand < GitCommand
  attr_reader :locale, :serializer, :base_64_encode
  attr_reader :encoding, :include_snapshot, :include_checksum
  attr_reader :paths

  include WithRepoName
  include WithRef
  include WithLocale

  include WithSnapshots

  validate :serializer, type: :serializer
  validate :encoding, type: :encoding

  def initialize(*args)
    super
    @paths = []
    @encoding = Encoding::UTF_8
    @base_64_encode = false
    @include_snapshot = false
    @include_checksum = false
  end

  # Sets the serializer used to export translations. Must be recognizable
  # as a serializer id, eg. 'yaml/rails' or 'json/key-value'.
  #
  # @param [String] serializer The serializer to use.
  # @return [self]
  def set_serializer(serializer)
    @serializer = serializer
    self
  end

  # Sets whether or not the serialized translations should be returned
  # encoded in base 64.
  #
  # @param [Boolean] should_encode To encode or not encode, that is
  #   the question.
  # @return [self]
  def set_base_64_encode(should_encode)
    @base_64_encode = should_encode
    self
  end

  # Sets the encoding translations are expected to be in. Not to be
  # confused with base 64 encoding.
  #
  # @param [String, Encoding] encoding The encoding to use. Can be
  #   either a +String+ or a Ruby +Encoding+, eg. +Encoding::UTF_8+.
  # @return [self]
  def set_encoding(encoding)
    @encoding = encoding
    self
  end

  # Sets whether or not to include the snapshot in the return value.
  #
  # @param [Boolean] should_include_snapshot whether or not to
  #   return the snapshot.
  # @return [self]
  def set_include_snapshot(should_include_snapshot)
    @include_snapshot = should_include_snapshot
    self
  end

  # Sets whether or not to include a checksum of the phrases in the
  # return value.
  #
  # @param [Boolean] should_include_checksum whether or not to include
  #   the checksum.
  # @return [self]
  def set_include_checksum(should_include_checksum)
    @include_checksum = should_include_checksum
    self
  end

  # A list of files or paths to filter translations by. Only translations
  # matching these paths will be included in the export payload.
  def set_paths(paths)
    @paths = Array(paths)
    self
  end

  # Perform the export.
  #
  # @return [Hash] containing the following attributes:
  #   * +payload+: The serialized +String+ blob of all the translations.
  #   * +encoding+: The encoding of the strings in +payload+.
  #   * +translation_count+: The number of translations in +payload+.
  #   * +base_64_encoded+: A boolean indicating if +payload+ is base
  #     64 encoded.
  #   * +locale+: The locale the translations in +payload+ are written in.
  #   * +snapshot+: The snapshot used to identify the translations in
  def execute
    stream = StringIO.new
    snapshot = take_snapshot(repo_config, commit_id, paths)
    translation_count = 0
    checksum_list = []

    serializer_instance = serializer_config.klass.new(
      stream, locale_obj, encoding
    )

    write_translations_for(snapshot, serializer_instance) do |trans|
      translation_count += 1

      if include_checksum
        checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
      end
    end

    params = {
      payload: encode(stream.string),
      encoding: serializer_instance.encoding.to_s,
      translation_count: translation_count,
      base_64_encoded: base_64_encode,
      locale: locale,
      paths: paths
    }

    if include_snapshot
      params.merge!(snapshot: snapshot)
    end

    if include_checksum
      params.merge!(checksum: checksum_for(checksum_list))
    end

    params
  end

  private

  def write_translations_for(snapshot, serializer_instance)
    each_translation(snapshot) do |trans|
      next unless include_trans?(trans)
      trans = apply_preprocessors(trans, serializer_config)
      yield trans if block_given?

      serializer_instance.write_key_value(
        trans.phrase.index_value, trans.translation
      )
    end

    serializer_instance.flush
  end

  def include_trans?(trans)
    paths.size == 0 || paths.include?(trans.phrase.file)
  end

  def checksum_for(list)
    Digest::MD5.hexdigest(list.sort.join)
  end

  def locale_obj
    @locale_obj ||= repo_config.get_locale(locale)
  end

  def apply_preprocessors(translation, serializer_config)
    serializer_config.preprocessors.inject(translation) do |trans, preprocessor|
      preprocessor.process(trans)
    end
  end

  def encode(string)
    if base_64_encode
      Base64.encode64(string)
    else
      string
    end
  end

  def serializer_config
    @serializer_config ||= repo_config.get_serializer_config(serializer)
  end

  def repo_config
    @repo_config ||= get_repo(repo_name)
  end

  def each_translation(snapshot)
    datastore.phrases_by_commits(repo_name, snapshot) do |phrase|
      # only yield if a translation exists
      if text = repo_config.tms.lookup_translation(locale_obj, phrase)
        yield Translation.new(phrase, locale, text)
      end
    end
  end
end

Instance Method Details

#executeHash

Perform the export.

Returns:

  • (Hash)

    containing the following attributes:

    • payload: The serialized String blob of all the translations.

    • encoding: The encoding of the strings in payload.

    • translation_count: The number of translations in payload.

    • base_64_encoded: A boolean indicating if payload is base 64 encoded.

    • locale: The locale the translations in payload are written in.

    • snapshot: The snapshot used to identify the translations in


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rosette/core/commands/translations/export_command.rb', line 157

def execute
  stream = StringIO.new
  snapshot = take_snapshot(repo_config, commit_id, paths)
  translation_count = 0
  checksum_list = []

  serializer_instance = serializer_config.klass.new(
    stream, locale_obj, encoding
  )

  write_translations_for(snapshot, serializer_instance) do |trans|
    translation_count += 1

    if include_checksum
      checksum_list << "#{trans.phrase.index_value}#{trans.translation}"
    end
  end

  params = {
    payload: encode(stream.string),
    encoding: serializer_instance.encoding.to_s,
    translation_count: translation_count,
    base_64_encoded: base_64_encode,
    locale: locale,
    paths: paths
  }

  if include_snapshot
    params.merge!(snapshot: snapshot)
  end

  if include_checksum
    params.merge!(checksum: checksum_for(checksum_list))
  end

  params
end

#set_base_64_encode(should_encode) ⇒ self

Sets whether or not the serialized translations should be returned encoded in base 64.

Parameters:

  • should_encode (Boolean)

    To encode or not encode, that is the question.

Returns:

  • (self)

103
104
105
106
# File 'lib/rosette/core/commands/translations/export_command.rb', line 103

def set_base_64_encode(should_encode)
  @base_64_encode = should_encode
  self
end

#set_encoding(encoding) ⇒ self

Sets the encoding translations are expected to be in. Not to be confused with base 64 encoding.

Parameters:

  • encoding (String, Encoding)

    The encoding to use. Can be either a String or a Ruby Encoding, eg. Encoding::UTF_8.

Returns:

  • (self)

114
115
116
117
# File 'lib/rosette/core/commands/translations/export_command.rb', line 114

def set_encoding(encoding)
  @encoding = encoding
  self
end

#set_include_checksum(should_include_checksum) ⇒ self

Sets whether or not to include a checksum of the phrases in the return value.

Parameters:

  • should_include_checksum (Boolean)

    whether or not to include the checksum.

Returns:

  • (self)

135
136
137
138
# File 'lib/rosette/core/commands/translations/export_command.rb', line 135

def set_include_checksum(should_include_checksum)
  @include_checksum = should_include_checksum
  self
end

#set_include_snapshot(should_include_snapshot) ⇒ self

Sets whether or not to include the snapshot in the return value.

Parameters:

  • should_include_snapshot (Boolean)

    whether or not to return the snapshot.

Returns:

  • (self)

124
125
126
127
# File 'lib/rosette/core/commands/translations/export_command.rb', line 124

def set_include_snapshot(should_include_snapshot)
  @include_snapshot = should_include_snapshot
  self
end

#set_paths(paths) ⇒ Object

A list of files or paths to filter translations by. Only translations matching these paths will be included in the export payload.


142
143
144
145
# File 'lib/rosette/core/commands/translations/export_command.rb', line 142

def set_paths(paths)
  @paths = Array(paths)
  self
end

#set_serializer(serializer) ⇒ self

Sets the serializer used to export translations. Must be recognizable as a serializer id, eg. 'yaml/rails' or 'json/key-value'.

Parameters:

  • serializer (String)

    The serializer to use.

Returns:

  • (self)

92
93
94
95
# File 'lib/rosette/core/commands/translations/export_command.rb', line 92

def set_serializer(serializer)
  @serializer = serializer
  self
end