Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7 updates + DeepL Translation provider #189

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.4
3.1.2
52 changes: 5 additions & 47 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
appraise 'rails-5.2' do
appraise 'rails-7.0' do
source 'https://rubygems.org'
gem 'rails', '~> 5.2.0'
gem 'devise'
gem 'rails', '~> 7.0.4'
gem 'devise', '~> 4.8.1'
gem 'pg', '~> 1.2.3'
gem 'redis'
gem 'capybara'
Expand All @@ -16,47 +16,5 @@ appraise 'rails-5.2' do
gem 'rails-controller-testing'
gem 'minitest'
gem 'm', '~> 1.5.0'
gem 'google-cloud-translate', '~> 1.2.4'
end

appraise 'rails-6.0' do
source 'https://rubygems.org'
gem 'rails', '~> 6.0.3'
gem 'devise'
gem 'pg', '~> 1.2.3'
gem 'redis'
gem 'capybara'
gem 'database_cleaner'
gem 'test_declarative'
gem 'mocha'
gem 'webmock'
gem 'sass-rails'
gem 'jquery-rails'
gem 'coffee-rails'
gem 'pry-rails'
gem 'rails-controller-testing'
gem 'minitest'
gem 'm', '~> 1.5.0'
gem 'google-cloud-translate', '~> 1.2.4'
end

appraise 'rails-6.1' do
source 'https://rubygems.org'
gem 'rails', '~> 6.1.4.1'
gem 'devise'
gem 'pg', '~> 1.2.3'
gem 'redis'
gem 'capybara'
gem 'database_cleaner'
gem 'test_declarative'
gem 'mocha'
gem 'webmock'
gem 'sass-rails'
gem 'jquery-rails'
gem 'coffee-rails'
gem 'pry-rails'
gem 'rails-controller-testing'
gem 'minitest'
gem 'm', '~> 1.5.0'
gem 'google-cloud-translate', '~> 2.1.2'
end
gem "google-cloud-translate", "1.2.4"
end
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [Unreleased]
- Support for Rails 7.0

## [WIP]
- Efforts to use Vanilla JS and remove jQuery [WIP]
Expand Down
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ $ rake lit:import FILE=stuff.csv LOCALES=en,pl SKIP_NIL=1

Additionally, there is the `lit:warm_up_keys` task (temporarily aliased as `lit:raw_import` for compatibility) which serves a different purpose: rather than for actual import of translations, it is intended to pre-load into database translations from a specific locale's YAML file **when the application is first deployed to a server and not all translation keys are present in the database yet**. This task also takes the `SKIP_NIL` option in a similar way as the import task.
```bash
$ rake lit:warm_up_keys FILES=config/locales/en.yml LOCALES=en
$ rake lit:warm_up_keys FILES=en.yml LOCALE=en
```
In this case, when the `config/locales/en.yml` contains a translation for `foo` which doesn't have a key in the DB yet, it will be created, but if it already exists in the DB with a translation, it won't be overridden.

Expand All @@ -122,6 +122,24 @@ Currently, Google and Yandex translation providers are supported, but extending
Configure your translation provider using one of routines described below. When a translation provider is configured, each localization in Lit web UI will have a "Translate using _Provider Name_" button next to it, which by default translates to the localization's language from the localization currently saved for the app's `I18n.default_locale`.
Next to the button, there is a dropdown that allows translating from the key's localization in a language different than the default one.

#### DeepL Cloud Translation API

Insert this into your Lit initializer:
```
require 'lit/cloud_translation/providers/deepl_translator'

Lit::CloudTranslation.provider = Lit::CloudTranslation::Providers::DeeplTranslator
Lit::CloudTranslation.configure do |config|
config.api_key = HashWithIndifferentAccess.new(Rails.application.credentials.config[:deepl_api])
end
```

...and make sure you have this in your Gemfile:

```
gem "deepl-rb", require: "deepl"
```

#### Google Cloud Translation API

Insert this into your Lit initializer:
Expand Down Expand Up @@ -296,7 +314,7 @@ Lit.store_request_keys = true
1. `gem install bundler && bundle install` - ensure Bundler and all required gems are installed
2. `bundle exec appraisal install` - install gems from appraisal's gemfiles
3. `cp test/dummy/config/database.yml.sample test/dummy/config/database.yml` - move a database.yml in place (remember to fill your DB credentials in it)
4. `RAILS_ENV=test bundle exec appraisal rails-5.2 rake db:setup` - setup lit DB (see test/config/database.yml); do it
4. `RAILS_ENV=test bundle exec appraisal rails-7.0 rake db:setup` - setup lit DB (see test/config/database.yml); do it
only once, it does not matter which Rails version you use for `appraisal`
5. `bundle exec appraisal rake` - run the tests!

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Lit
module Api
module V1
class BaseController < ActionController::Base
class BaseLitController < ActionController::Base
layout nil
respond_to :json if ::Rails::VERSION::MAJOR < 5
before_action :authenticate_requests!

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/lit/api/v1/locales_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require_dependency 'lit/api/v1/base_controller'
require_dependency 'lit/api/v1/base_lit_controller'

module Lit
module Api
module V1
class LocalesController < Api::V1::BaseController
class LocalesController < Api::V1::BaseLitController
def index
@locales = Locale.all
render json: @locales.as_json(root: false, only: %i[id locale])
render json: @locales.as_json(root: false, only: %i[id locale]).to_json
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/lit/api/v1/localization_keys_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require_dependency 'lit/api/v1/base_controller'
require_dependency 'lit/api/v1/base_lit_controller'

module Lit
class Api::V1::LocalizationKeysController < Api::V1::BaseController
class Api::V1::LocalizationKeysController < Api::V1::BaseLitController
def index
@localization_keys = fetch_localization_keys
render json: @localization_keys.as_json(
root: false, only: %i[id localization_key is_deleted]
)
).to_json
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/lit/api/v1/localizations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Lit
class Api::V1::LocalizationsController < Api::V1::BaseController
class Api::V1::LocalizationsController < Api::V1::BaseLitController
def index
@localizations = fetch_localizations
render json:
@localizations.as_json(
root: false,
only: %i[id localization_key_id locale_id],
methods: %i[value localization_key_str locale_str localization_key_is_deleted],
)
).to_json
end

def last_change
Expand Down
15 changes: 15 additions & 0 deletions app/models/lit/incomming_localization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def assign_new_localization_key
end

def assign_new_localization
create_other_localizations if localization_key.localizations.blank?

self.localization =
Lit::Localization
.where(localization_key_id: localization_key.id)
Expand All @@ -86,6 +88,19 @@ def assign_new_localization
localization.save!
end

def create_other_localizations
::Lit::Locale.where.not(id: locale.id).pluck(:id).each do |locale_id|
localization =
Lit::Localization
.where(localization_key_id: localization_key.id)
.where(locale_id: locale_id)
.first_or_initialize
localization.translated_value = translated_value
localization.save!
Lit.init.cache.update_cache localization.full_key, localization.translation
end
end

def update_cache
Lit.init.cache.update_cache localization.full_key, localization.translation
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/lit/localization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def locale_str
end

def last_change
updated_at.to_s(:db)
updated_at.to_fs(:db)
end

def update_default_value(value)
Expand Down
6 changes: 4 additions & 2 deletions app/services/synchronize_source_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class SynchronizeSourceService
def initialize(source)
def initialize(source, auto_accept = false)
Copy link
Member

@mlitwiniuk mlitwiniuk Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere? In a sense - I does not seem to be called with second parameter anywhere.

@source = source
@auto_accept = auto_accept
end

def execute
Expand All @@ -11,7 +12,7 @@ def execute
private

def synchronize_localizations
after_date = @source.last_updated_at&.to_s(:db)
after_date = @source.last_updated_at&.to_fs(:db)
result = interactor.send_request Lit::Source::LOCALIZATIONS_PATH, after: after_date
return unless result&.is_a?(Array)
result.each { |loc| synchronize_localization loc }
Expand All @@ -29,6 +30,7 @@ def synchronize_localization(loc)
return if inc_loc.duplicated?(loc['value'])

inc_loc.save!
inc_loc.accept if @auto_accept
end

def find_incomming_localization(localization)
Expand Down
2 changes: 1 addition & 1 deletion app/views/lit/dashboard/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<strong>All localization keys</strong> <%= Lit::LocalizationKey.active.count(:id) %><br/>
<% @locales.each do |l| %>
<strong><%= EmojiFlag.new(l.locale) %> <%= I18n.t("lit.locale_to_languages.#{l.locale}", :default=>l.locale) %>:</strong> <span title="<%= "#{l.changed_localizations_count}/#{l.all_localizations_count}" %>"><%= l.translated_percentage %>%</span><br/>
<strong><%= I18n.t("lit.locale_to_languages.#{l.locale}", :default=>l.locale) %>:</strong> <span title="<%= "#{l.changed_localizations_count}/#{l.all_localizations_count}" %>"><%= l.translated_percentage %>%</span><br/>
<% end %>

2 changes: 1 addition & 1 deletion app/views/lit/incomming_localizations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<% @incomming_localizations.each do |il| %>
<tr data-id="<%= il.id %>">
<td colspan="3">
<%= image_tag EmojiFlag.new(il.locale_str) %>
<%= il.locale_str %>
<strong><%= il.full_key %></strong>
</td>
</tr>
Expand Down
3 changes: 1 addition & 2 deletions app/views/lit/localization_keys/_localizations_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<tr>
<td>
<% if localization %>
<%= draw_icon 'clock-o', title: "Last updated at #{localization.updated_at.to_s(:db)}" %>
<%= draw_icon 'clock-o', title: "Last updated at #{localization.updated_at.to_fs(:db)}" %>
<%= link_to lit.previous_versions_localization_key_localization_path(lk, localization, format: :js), class: "js-show_prev_versions #{'hidden' unless versions?(localization)}", remote: true do %>
<%= draw_icon 'random', title: I18n.t('lit.common.previous_versions', default: 'Previous versions') %>
<% end %>
Expand All @@ -56,7 +56,6 @@
<%= render partial: 'localization_row', locals: {localization: Lit.init.cache["#{locale}.#{lk.localization_key}"]} %>
</td>
<td class="locale_row text-center">
<%= EmojiFlag.new(locale) %>
<%= locale %>
</td>
<% unless lk.is_deleted? %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<% @versions.each do |v| %>
<tr>
<td>
<%= render :partial=>"/lit/localization_keys/localization_row", formats: ['html'], :locals=>{:localization=>v.translated_value} %>
<%= render :partial=>"/lit/localization_keys/localization_row", :locals=>{:localization=>v.translated_value} %>
</td>
<td><%= I18n.l v.created_at, format: :long %></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/lit/localizations/edit.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var $row = $('td.localization_row[data-id="<%= @localization.id %>"]');
$row.data('editing', 1);
$row.html("<%= ejs render(partial: 'form', formats: ['html']) %>");
$row.html("<%= ejs render(partial: 'form') %>");
$row.find('textarea').focus();
<% if @localization.full_key.ends_with?('_html') -%>
$row.find('.wysiwyg_switch').click();
Expand Down
2 changes: 1 addition & 1 deletion app/views/lit/localizations/previous_versions.js.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var row = $('.localization_versions_row[data-id="<%= @localization.id %>"]');
row.removeClass('hidden');
row.children('td').html("<%= j render(:partial=>'previous_versions_rows', formats: ['html'], :locals=>{:localization=>@localization, :versions=>@versions}) %>");
row.children('td').html("<%= j render(:partial=>'previous_versions_rows', :locals=>{:localization=>@localization, :versions=>@versions}) %>");
2 changes: 1 addition & 1 deletion app/views/lit/localizations/update.js.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var $row = $('td.localization_row[data-id="<%= @localization.id %>"]');
$row.data('editing', 0);
$row.html("<%= ejs render(:partial=>"/lit/localization_keys/localization_row", formats: ['html'], :locals=>{:localization=>@localization.translated_value }) %>");
$row.html("<%= ejs render(:partial=>"/lit/localization_keys/localization_row", :locals=>{:localization=>@localization.translated_value }) %>");
$row.siblings().find('.js-show_prev_versions').removeClass('hidden');
$('a.change_completed_<%= @localization.id %> input[type=checkbox]').prop("checked", true);

2 changes: 1 addition & 1 deletion app/views/lit/sources/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<% @sources.each do |source| %>
<tr>
<td><%= link_to source.identifier, lit.source_path(source) %></td>
<td><%= source.last_updated_at.to_s(:db) unless source.last_updated_at.nil? %></td>
<td><%= source.last_updated_at.to_fs(:db) unless source.last_updated_at.nil? %></td>
<td><%= source.last_change %></td>
<td>
<%= link_to lit.source_incomming_localizations_path(source), :title=>t('lit.common.browse_incomming', :default=>"Browse incomming localizations") do %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/lit/sources/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<%= @source.api_key %>
<br/>
<strong>Last updated at:</strong>
<%= @source.last_updated_at.to_s(:db) unless @source.last_updated_at.nil? %>
<%= @source.last_updated_at.to_fs(:db) unless @source.last_updated_at.nil? %>
<%= link_to '(touch!)', touch_source_path(@source), :method => :put, :data => { :confirm => 'Are you sure?' } %>
<br/>

Expand Down
Loading