Skip to content

Commit

Permalink
Add translate_to_original method using original_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
oliveranthony17 committed Nov 17, 2024
1 parent f2d2c28 commit aad85b6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/assets/stylesheets/moirai/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
*
*= require_tree .
*= require_self
*= require translation_files
*/
22 changes: 22 additions & 0 deletions app/assets/stylesheets/translation_files.css
Original file line number Diff line number Diff line change
@@ -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 */
28 changes: 28 additions & 0 deletions app/views/layouts/moirai/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@
<%= stylesheet_link_tag "moirai/application", media: "all" %>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAC/VBMVEUAAACcnKyEcFGLlc/AuavOyr+tm3zCrIGTgmaDbkypnojJw6rQv5jNv6TX5fLl8v+fiGatmnPFtpKXgWKVgF2Tg2WVfFmhkG+ejG2kjmmrmXqiloGGb0urnH+pnonDupyNdVHSyamempOSe1q5sqSQioqkk2yThmuWeVONeVaPgmeIdFXItIvArYaMdlOSfVm3qIKxo3+unXijjmePgWeXhmmSfFrEs5C8rozQw5zUxqSelIC2o33Nw6Sgi2imlnuZi3SfiGewoYStn4eVhGquopWampXJw66VlZWqqp6He2aYj3q6rJKqo5GWj3+Nf2VmWUXAuaSlnYqhmoidl4aSineThW2OhG2CemiFdFh7ZUXJxbLFv6q5taSjno2wpIusoIaalYakmICik3qWjXqOhXOViG+Kgm5+dGCRfFq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gSrS3KyqOj4CI1HV8KxJoxQDf5zgV4GzKvPHX1Acc9jgTpomdXdUDjnqjaTAcuJvglEmpGxXB9ft61rwRP2ypjBzXqaj1OQYUiGyDtOcLvJJtwPhsFZu6BWgX+40y3OKFcz9YWEqUMjA9W8dH0WhyFzQiBklpbLfgfToYAbBkF3bKcwr2g5w96S/JLpV8nEqDA/19q6qWDg4UTg02Ho1iCCZq6zGN0TEoSIH2bd8PY3vc1SBFAXbIxLMNvU1P9HbpW+A54AlX/7jg5GA0TdaDCYsUMcZ4SrNDS0saNgr9s0M7VcbYuk3kISm2fGXga+YgGQHoOtiW3kWQivsR3MDTCAXaOnb60+qjA6FXwFZHGBCcCKNTU2ViaawfjpBpjM/GxJQJ0uHA7fshYM1MqxKEv9J0JhohNCz8Tc4FabxL5TToQbwrn7FD+5fh5KzzdRubDYw/ohHofkVHZQ7Irs7QuM2VmnIApWSMw5PIpPe7YDPgJvkHBLXOzYW57mmPxm/uWReVhIRdRw7YpsgWs4HXxClEO/LdQzpcV/oxpkhAuzoFksooV9akTSdZN4s1KvvJLe959cl47N7ZpZX4k29a/bzLVuIhMIvmW2Umk55ehMUI0WcwcdC165aOvC+UmCUqkXdah4KejHuLTI80ugKEw3Eoa08h+SBqiuUB+/WWoLN5ehLHlksnOFh6pis8ajfv7kovnzVOfuX3JWCfI/ZiopTVGHTIhfmYNNrIltIWMAAAAASUVORK5CYII=">

<style>
td {
height: 100px; /* Ensure the cell has a consistent height */
width: 200px; /* Set a reasonable width */
vertical-align: top;
}

form {
height: 100%;
display: flex;
align-items: stretch;
}

textarea {
flex: 1; /* Allow the textarea to expand */
width: 100%; /* Ensure it fills the width */
resize: none; /* Disable manual resizing */
box-sizing: border-box; /* Account for padding and borders */
border: 1px solid #ccc; /* Standard input border style */
border-radius: 4px; /* Match the design */
padding: 5px; /* Inner padding for text */
font-family: inherit; /* Use consistent font */
font-size: inherit; /* Match the font size */
overflow: hidden; /* Hide overflow before it resizes */
}
</style>

</head>
<body>

Expand Down
10 changes: 8 additions & 2 deletions app/views/moirai/translation_files/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<tr>
<th>Key</th>
<th>Value</th>
<th>Original Translation</th>
</tr>
</thead>
<tbody>
Expand All @@ -24,13 +25,18 @@
<% end %>
</td>
<td>
<%= 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 %>
</td>
<td>
<%= I18n.translate_to_original(key, @locale) %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
13 changes: 13 additions & 0 deletions lib/i18n/extensions/i18n.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/moirai.rb
Original file line number Diff line number Diff line change
@@ -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...
Expand Down
1 change: 1 addition & 0 deletions lib/moirai/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
14 changes: 14 additions & 0 deletions test/i18n/i18n_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit aad85b6

Please sign in to comment.