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

#main_config, #schema

Instance Method Summary collapse

Methods inherited from Plugin

#config, #config_key, #enabled?, #initialize, #transform

Constructor Details

This class inherits a constructor from I18nJS::Plugin

Instance Method Details

#after_export(files:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/i18n-js/export_files_plugin.rb', line 43

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: file,
        translations: translations,
        template: export[:template]
      )

      contents = template.render

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

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

#setupObject



7
8
9
# File 'lib/i18n-js/export_files_plugin.rb', line 7

def setup
  I18nJS::Schema.root_keys << config_key
end

#validate_schemaObject



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

def validate_schema
  valid_keys = %i[enabled files]

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

  config[:files].each_with_index do |_exports, index|
    export_keys = %i[template output]

    schema.expect_required_keys(
      keys: export_keys,
      path: [config_key, :files, index]
    )

    schema.reject_extraneous_keys(
      keys: export_keys,
      path: [config_key, :files, index]
    )

    schema.expect_type(
      path: [config_key, :files, index, :template],
      types: String
    )

    schema.expect_type(
      path: [config_key, :files, index, :output],
      types: String
    )
  end
end