diff --git a/app/assets/stylesheets/moirai/application.css b/app/assets/stylesheets/moirai/application.css index 0ebd7fe..994acca 100644 --- a/app/assets/stylesheets/moirai/application.css +++ b/app/assets/stylesheets/moirai/application.css @@ -12,4 +12,6 @@ * *= require_tree . *= require_self + *= require translation_files + */ diff --git a/app/assets/stylesheets/translation_files.css b/app/assets/stylesheets/translation_files.css new file mode 100644 index 0000000..9590140 --- /dev/null +++ b/app/assets/stylesheets/translation_files.css @@ -0,0 +1,22 @@ +td { + height: 100px; + width: 200px; + vertical-align: top; +} + +form { + height: 100%; + display: flex; + align-items: stretch; +} + +textarea.translation-textarea { + width: 100%; + height: auto; + resize: vertical; + min-height: 3em; + overflow: hidden; + margin-bottom: 0; +} + +/*# TODO: this isn't coming through */ diff --git a/app/views/layouts/moirai/application.html.erb b/app/views/layouts/moirai/application.html.erb index db31aa4..d206b53 100644 --- a/app/views/layouts/moirai/application.html.erb +++ b/app/views/layouts/moirai/application.html.erb @@ -8,6 +8,34 @@ <%= stylesheet_link_tag "moirai/application", media: "all" %> + + + diff --git a/app/views/moirai/translation_files/show.html.erb b/app/views/moirai/translation_files/show.html.erb index 4b50c78..a9e8651 100644 --- a/app/views/moirai/translation_files/show.html.erb +++ b/app/views/moirai/translation_files/show.html.erb @@ -9,6 +9,7 @@ Key Value + Original Translation @@ -24,13 +25,18 @@ <% end %> - <%= form_for translation&.presence || Moirai::Translation.new(key: key, locale: @locale, value: value), url: moirai_create_or_update_translation_path, method: :post do |f| %> + <%= form_for translation&.presence || Moirai::Translation.new(key: key, locale: @locale, value: value), + url: moirai_create_or_update_translation_path, + method: :post do |f| %> <%= f.hidden_field :key %> <%= f.hidden_field :locale %> - <%= f.text_field :value %> + <%= f.text_area :value, class: 'translation-textarea' %> <%= f.submit 'Update', style: 'display: none;' %> <% end %> + + <%= I18n.translate_to_original(key, @locale) %> + <% end %> diff --git a/lib/i18n/extensions/i18n.rb b/lib/i18n/extensions/i18n.rb new file mode 100644 index 0000000..8e5f79d --- /dev/null +++ b/lib/i18n/extensions/i18n.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module I18n + class << self + attr_accessor :original_backend + end + + def self.translate_to_original(key, locale, **) + raise "Original backend is not set" unless original_backend + + original_backend.translate(locale, key, **) + end +end diff --git a/lib/moirai.rb b/lib/moirai.rb index 513fab5..0ed9837 100644 --- a/lib/moirai.rb +++ b/lib/moirai.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true require "moirai/version" +require "i18n/extensions/i18n" +require "i18n/backend/moirai" require "moirai/engine" require "moirai/pull_request_creator" -require "i18n/backend/moirai" module Moirai # Your code goes here... diff --git a/lib/moirai/engine.rb b/lib/moirai/engine.rb index 9c79d59..ae0bffa 100644 --- a/lib/moirai/engine.rb +++ b/lib/moirai/engine.rb @@ -10,6 +10,7 @@ class Engine < ::Rails::Engine config.after_initialize do if ActiveRecord::Base.connection.data_source_exists?("moirai_translations") + I18n.original_backend = I18n.backend I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Moirai.new, I18n.backend) else Rails.logger.warn("moirai disabled: tables have not been generated yet.") diff --git a/test/i18n/i18n_test.rb b/test/i18n/i18n_test.rb new file mode 100644 index 0000000..938ca30 --- /dev/null +++ b/test/i18n/i18n_test.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +require "test_helper" + +class I18nExtensionsTest < ActiveSupport::TestCase + test "it correctly translates using .translate and .translate_to_original" do + assert_equal "Italienisch", I18n.t("locales.italian", locale: :de) + + Moirai::Translation.create!(locale: "de", key: "locales.italian", value: "Italianese") + + assert_equal "Italianese", I18n.t("locales.italian", locale: :de) + assert_equal "Italienisch", I18n.translate_to_original("locales.italian", :de) + end +end