Class: I18nJS::ExportFilesPlugin

Inherits:
Plugin
  • Object
show all
Defined in:
lib/i18n-js/export_files_plugin.rb

Defined Under Namespace

Classes: Template

Instance Attribute Summary

Attributes inherited from Plugin

#config, #main_config, #schema

Instance Method Summary collapse

Methods inherited from Plugin

#enabled?, #initialize, key, #setup, #transform

Constructor Details

This class inherits a constructor from I18nJS::Plugin

Instance Method Details

#after_export(files:) ⇒ Object



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
# File 'lib/i18n-js/export_files_plugin.rb', line 24

def after_export(files:)
  require "erb"
  require "digest/md5"
  require "json"

  files.each do |file|
    dir = File.dirname(file)
    name = File.basename(file)
    extension = File.extname(name)
    base_name = File.basename(file, extension)

    config[:files].each do |export|
      translations = JSON.load_file(file)
      template = Template.new(
        file:,
        translations:,
        template: export[:template]
      )

      contents = template.render

      output = format(
        export[:output],
        dir:,
        name:,
        extension:,
        digest: Digest::MD5.hexdigest(contents),
        base_name:
      )

      File.open(output, "w") do |io|
        io << contents
      end
    end
  end
end

#validate_schemaObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/i18n-js/export_files_plugin.rb', line 7

def validate_schema
  valid_keys = %i[enabled files]

  schema.expect_required_keys(keys: valid_keys)
  schema.reject_extraneous_keys(keys: valid_keys)
  schema.expect_array_with_items(path: [:files])

  export_keys = %i[template output]

  config[:files].each_with_index do |_exports, index|
    schema.expect_required_keys(keys: export_keys, path: [:files, index])
    schema.reject_extraneous_keys(keys: export_keys, path: [:files, index])
    schema.expect_type(path: [:files, index, :template], types: String)
    schema.expect_type(path: [:files, index, :output], types: String)
  end
end