Sha256: 71ef0281ef00f1e769585f628038fc72f44b47a1ab5b8ce4337fb7acab0114a2
Contents?: true
Size: 1.66 KB
Versions: 1
Compression:
Stored size: 1.66 KB
Contents
# frozen_string_literal: true require "i18n" require "json" require "yaml" require "glob" require "fileutils" require "optparse" require_relative "i18n-js/schema" require_relative "i18n-js/version" module I18nJS MissingConfigError = Class.new(StandardError) def self.call(config_file: nil, config: nil) if !config_file && !config raise MissingConfigError, "you must set either `config_file` or `config`" end config = Glob::SymbolizeKeys.call(config || YAML.load_file(config_file)) Schema.validate!(config) config[:translations].each do |group| export_group(group) end end def self.export_group(group) filtered_translations = Glob.filter(translations, group[:patterns]) output_file_path = File.expand_path(group[:file]) if output_file_path.include?(":locale") filtered_translations.each_key do |locale| locale_file_path = output_file_path.gsub(/:locale/, locale.to_s) write_file(locale_file_path, locale => filtered_translations[locale]) end else write_file(output_file_path, filtered_translations) end end def self.write_file(file_path, translations) FileUtils.mkdir_p(File.dirname(file_path)) contents = ::JSON.pretty_generate(translations) digest = Digest::MD5.hexdigest(contents) file_path = file_path.gsub(/:digest/, digest) File.open(file_path, "w") do |file| file << contents end end def self.translations ::I18n.backend.instance_eval do has_been_initialized_before = respond_to?(:initialized?, true) && initialized? init_translations unless has_been_initialized_before translations end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
i18n-js-4.0.0.alpha3 | lib/i18n-js.rb |