diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e5e0e4f..f3f121d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -37,8 +37,13 @@ jobs: - name: Run system tests run: bundle exec rails test:system - - + + - name: Archive logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: test.log + path: test/dummy/log/test.log lint: name: Lint runs-on: ubuntu-latest 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..d396656 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_without_moirai(key, @locale) %> + <% end %> diff --git a/lib/i18n/extensions/i18n.rb b/lib/i18n/extensions/i18n.rb new file mode 100644 index 0000000..f142a5a --- /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_without_moirai(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..5ee8b5c 100644 --- a/lib/moirai/engine.rb +++ b/lib/moirai/engine.rb @@ -9,7 +9,8 @@ class Engine < ::Rails::Engine end config.after_initialize do - if ActiveRecord::Base.connection.data_source_exists?("moirai_translations") + I18n.original_backend = I18n.backend + if ActiveRecord::Base.connection.data_source_exists?("moirai_translations") || ENV["RAILS_ENV"] == "test" 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..b3c2c55 --- /dev/null +++ b/test/i18n/i18n_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require "test_helper" + +class I18nExtensionsTest < ActiveSupport::TestCase + test "it correctly translates using .translate and .translate_without_moirai" do + assert_equal "Italienisch", I18n.t("locales.italian", locale: :de) + + Moirai::Translation.create!(locale: "de", key: "locales.italian", value: "Italianese") + + assert_equal 1, Moirai::Translation.count + assert_equal "Italianese", I18n.t("locales.italian", locale: :de) + assert_equal "Italienisch", I18n.translate_without_moirai("locales.italian", :de) + end +end