generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #881 from ita-social-projects/feature/category-nam…
…e-translation Category name translation based on site locale
- Loading branch information
Showing
17 changed files
with
152 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Translatable | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def translates(*attributes) | ||
attributes.each do |attribute| | ||
define_method(attribute) do | ||
translation_for(attribute) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def translation_for(attribute) | ||
locale_attr = "#{I18n.locale}_#{attribute}" | ||
default_locale_attr = "#{I18n.default_locale}_#{attribute}" | ||
|
||
if respond_to?(locale_attr) | ||
public_send(locale_attr) | ||
elsif respond_to?(default_locale_attr) | ||
public_send(default_locale_attr) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
<%= simple_form_for @category, url: { action: 'create' }, html: { novalidate: false } do |f| %> | ||
<%= simple_form_for @category, url: { action: "create" }, html: { novalidate: false } do |f| %> | ||
<div class="form-group row"> | ||
<div class="my-auto col-12 has-float-label"> | ||
<%= f.input :name, class: 'form-control col-sm-11' %> | ||
<%= f.input :uk_name, class: "form-control col-sm-11" %> | ||
<%= f.input :en_name, class: "form-control col-sm-11" %> | ||
<%= f.input :priority, collection: Category::PRIORITY_RANGE, wrapper: :custom_vertical_select %> | ||
</div> | ||
</div> | ||
<div class="button-group d-flex"> | ||
<%= f.submit t('.form.create_category_button'), class: 'btn btn-green me-2' %> | ||
<%= link_to account_categories_path, class: 'btn btn-danger d-flex align-items-center justify-content-center' do %> | ||
<span><%= t('buttons.cancel') %></span> | ||
<%= f.submit t(".form.create_category_button"), class: "btn btn-green me-2" %> | ||
<%= link_to account_categories_path, class: "btn btn-danger d-flex align-items-center justify-content-center" do %> | ||
<span><%= t("buttons.cancel") %></span> | ||
<% end %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
db/migrate/20240515233604_add_en_name_and_rename_name_to_uk_name_in_categories.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class AddEnNameAndRenameNameToUkNameInCategories < ActiveRecord::Migration[7.1] | ||
def up | ||
remove_index :categories, name: "index_categories_on_name" | ||
rename_column :categories, :name, :uk_name | ||
add_index :categories, "lower((uk_name)::text)", name: "index_categories_on_uk_name", unique: true | ||
add_column :categories, :en_name, :string | ||
add_index :categories, "lower((en_name)::text)", name: "index_categories_on_en_name", unique: true | ||
end | ||
|
||
def down | ||
remove_index :categories, name: "index_categories_on_uk_name" | ||
rename_column :categories, :uk_name, :name | ||
add_index :categories, "lower((name)::text)", name: "index_categories_on_name", unique: true | ||
remove_index :categories, name: "index_categories_on_en_name" | ||
remove_column :categories, :en_name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Translatable do | ||
describe "translations" do | ||
context "when en_name and uk_name are exist in model" do | ||
let(:dummy_translatable) { DummyTranslatable.new } | ||
|
||
it "returns the en_name when locale is set to :en" do | ||
allow(I18n).to receive(:locale).and_return(:en) | ||
|
||
expect(dummy_translatable.name).to eq("Diapers") | ||
end | ||
|
||
it "returns the uk_name when locale is set to :uk" do | ||
allow(I18n).to receive(:locale).and_return(:uk) | ||
|
||
expect(dummy_translatable.name).to eq("Підгузки") | ||
end | ||
end | ||
|
||
context "when en_name is not exist and a default locale is uk" do | ||
let(:dummy_translatable) { DummyTranslatable.new(en_name: nil) } | ||
|
||
it "returns the uk_name when locale is set to :en" do | ||
allow(I18n).to receive(:locale).and_return(:en) | ||
allow(I18n).to receive(:default_locale).and_return(:uk) | ||
|
||
expect(dummy_translatable.name).to eq("Підгузки") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class DummyTranslatable | ||
include Translatable | ||
|
||
attr_accessor :uk_name | ||
|
||
translates :name | ||
|
||
def initialize(en_name: "Diapers", uk_name: "Підгузки") | ||
@en_name = en_name | ||
@uk_name = uk_name | ||
|
||
define_en_name if @en_name | ||
end | ||
|
||
private | ||
|
||
# Define en_name only if en_name is present for testing case with uk defaul locale | ||
# when en_name not exist at all | ||
def define_en_name | ||
define_singleton_method(:en_name) { @en_name } | ||
end | ||
end |