|
| 1 | +require "erb" |
| 2 | + |
| 3 | +module ReactOnRails |
| 4 | + class LocalesToJs |
| 5 | + def initialize |
| 6 | + return unless obsolete? |
| 7 | + @translations, @defaults = generate_translations |
| 8 | + convert |
| 9 | + end |
| 10 | + |
| 11 | + private |
| 12 | + |
| 13 | + def obsolete? |
| 14 | + @obsolete ||= (latest_file_names - i18n_js_files.map { |f| f.tr("_", ".") }).present? |
| 15 | + end |
| 16 | + |
| 17 | + def latest_file_names |
| 18 | + files = locale_files + i18n_js_files.map { |f| send("path_#{f}").to_s } |
| 19 | + .select { |f| File.exist?(f) } |
| 20 | + files = files.sort_by { |f| File.mtime(f) } |
| 21 | + files.last(2).map { |f| File.basename(f) } |
| 22 | + end |
| 23 | + |
| 24 | + def convert |
| 25 | + i18n_js_files.each do |f| |
| 26 | + template = send("template_#{f}") |
| 27 | + path = send("path_#{f}") |
| 28 | + create_js_file(template, path) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + def i18n_js_files |
| 33 | + %w(translations_js default_js) |
| 34 | + end |
| 35 | + |
| 36 | + def create_js_file(template, path) |
| 37 | + result = ERB.new(template).result() |
| 38 | + File.open(path, "w") do |f| |
| 39 | + f.write(result) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + def generate_translations |
| 44 | + translations = {} |
| 45 | + defaults = {} |
| 46 | + locale_files.each do |f| |
| 47 | + translation = YAML.load(File.open(f)) |
| 48 | + key = translation.keys[0] |
| 49 | + val = flatten(translation[key]) |
| 50 | + translations = translations.deep_merge(key => val) |
| 51 | + defaults = defaults.deep_merge(flatten_defaults(val)) if key == default_locale |
| 52 | + end |
| 53 | + [translations.to_json, defaults.to_json] |
| 54 | + end |
| 55 | + |
| 56 | + def format(input) |
| 57 | + input.to_s.tr(".", "_").camelize(:lower).to_sym |
| 58 | + end |
| 59 | + |
| 60 | + def flatten_defaults(val) |
| 61 | + flatten(val).each_with_object({}) do |(k, v), h| |
| 62 | + key = format(k) |
| 63 | + h[key] = { id: k, defaultMessage: v } |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def flatten(translations) |
| 68 | + translations.each_with_object({}) do |(k, v), h| |
| 69 | + if v.is_a? Hash |
| 70 | + flatten(v).map { |hk, hv| h["#{k}.#{hk}".to_sym] = hv } |
| 71 | + else |
| 72 | + h[k] = v |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def i18n_dir |
| 78 | + @i18n_dir ||= ReactOnRails.configuration.i18n_dir |
| 79 | + end |
| 80 | + |
| 81 | + def locale_files |
| 82 | + @locale_files ||= Rails.application.config.i18n.load_path |
| 83 | + end |
| 84 | + |
| 85 | + def default_locale |
| 86 | + @default_locale ||= I18n.default_locale.to_s || "en" |
| 87 | + end |
| 88 | + |
| 89 | + def path_translations_js |
| 90 | + i18n_dir + "translations.js" |
| 91 | + end |
| 92 | + |
| 93 | + def path_default_js |
| 94 | + i18n_dir + "default.js" |
| 95 | + end |
| 96 | + |
| 97 | + def template_translations_js |
| 98 | + <<-JS |
| 99 | +export const translations = #{@translations}; |
| 100 | + JS |
| 101 | + end |
| 102 | + |
| 103 | + def template_default_js |
| 104 | + <<-JS |
| 105 | +import { defineMessages } from 'react-intl'; |
| 106 | +
|
| 107 | +const defaultLocale = \'#{default_locale}\'; |
| 108 | +
|
| 109 | +const defaultMessages = defineMessages(#{@defaults}); |
| 110 | +
|
| 111 | +export { defaultMessages, defaultLocale }; |
| 112 | + JS |
| 113 | + end |
| 114 | + end |
| 115 | +end |
0 commit comments