Skip to content

Commit

Permalink
Add i18n support (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
haukot authored Mar 30, 2021
1 parent 23434a6 commit a2be284
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 5 deletions.
27 changes: 26 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GEM
momentjs-rails (~> 2.8)
sassc-rails (~> 2.1)
selectize-rails (~> 0.6)
ast (2.4.2)
autoprefixer-rails (10.2.0.0)
execjs
builder (3.2.4)
Expand All @@ -90,8 +91,19 @@ GEM
ffi (1.14.2)
globalid (0.4.2)
activesupport (>= 4.2.0)
highline (2.0.3)
i18n (1.8.7)
concurrent-ruby (~> 1.0)
i18n-tasks (0.9.34)
activesupport (>= 4.0.2)
ast (>= 2.1.0)
erubi
highline (>= 2.0.0)
i18n
parser (>= 2.2.3.0)
rails-i18n
rainbow (>= 2.2.2, < 4.0)
terminal-table (>= 1.5.1)
jquery-rails (4.4.0)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand Down Expand Up @@ -124,6 +136,10 @@ GEM
nio4r (2.5.4)
nokogiri (1.11.1-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.11.1-x86_64-linux)
racc (~> 1.4)
parser (3.0.0.0)
ast (~> 2.4.1)
racc (1.5.2)
rack (2.2.3)
rack-test (1.1.0)
Expand All @@ -148,12 +164,16 @@ GEM
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
rails-i18n (6.0.0)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 7)
railties (6.1.1)
actionpack (= 6.1.1)
activesupport (= 6.1.1)
method_source
rake (>= 0.8.7)
thor (~> 1.0)
rainbow (3.0.0)
rake (13.0.3)
sassc (2.4.0)
ffi (~> 1.9)
Expand All @@ -172,21 +192,26 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.4.2)
terminal-table (3.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
thor (1.1.0)
tilt (2.0.10)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (1.7.0)
websocket-driver (0.7.3)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.4.2)

PLATFORMS
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
administrate-field-active_storage!
i18n-tasks (~> 0.9.34)
sqlite3 (~> 1.4)

BUNDLED WITH
2.2.5
2.2.14
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ Defaults to `false`.

Don't forget to include [ActiveStorage JavaScript](https://edgeguides.rubyonrails.org/active_storage_overview.html#direct-uploads). You can use `rails generate administrate:assets:javascripts` to be able to customize Administrate JavaScripts in your application.

## I18n

You can see translation example [here](https://github.com/Dreamersoul/administrate-field-active_storage/blob/master/config/locales/administrate-field-active_storage.en.yml).

## Things To Do:
- [x] upload single file
- [x] adding image support through url_for to support 3rd party cloud storage
Expand Down
1 change: 1 addition & 0 deletions administrate-field-active_storage.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Gem::Specification.new do |gem|
gem.add_dependency "rails", ">= 6.0"

gem.add_development_dependency 'sqlite3', '~> 1.4'
gem.add_development_dependency 'i18n-tasks', '~> 0.9.34'
end
5 changes: 4 additions & 1 deletion app/views/fields/active_storage/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ By default, the input is a text field for the image's URL.
<% end %>

<div>
<%= field.can_add_attachment? ? "Add:" : "Replace:" %>
<%= field.can_add_attachment? ?
I18n.t("administrate.fields.active_storage.add", default: 'Add') :
I18n.t("administrate.fields.active_storage.replace", default: 'Replace')
%>
<%= f.file_field field.attribute, multiple: field.many?, direct_upload: field.direct? %>
</div>
</div>
5 changes: 4 additions & 1 deletion app/views/fields/active_storage/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ By default, the attribute is rendered as an image tag.

<% if field.index_display_count? %>
<div class="attachments-count">
<%= pluralize(field.attachments.count, 'Attachment') %>
<%= pluralize(field.attachments.count,
I18n.t("administrate.fields.active_storage.attachment", default: 'Attachment')
)
%>
</div>
<% end %>
<% end %>
3 changes: 2 additions & 1 deletion app/views/fields/active_storage/_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ controlled via a boolean local variable.
<% if field.destroy_url.present? %>
<% destroy_url = field.destroy_url.call(namespace, field.data.record, attachment) %>
<div>
<%= link_to 'Remove', destroy_url, method: :delete, class: 'remove-attachment-link', data: { confirm: t("administrate.actions.confirm") } %>
<%= link_to I18n.t("administrate.fields.active_storage.remove", default: 'Remove'),
destroy_url, method: :delete, class: 'remove-attachment-link', data: { confirm: t("administrate.actions.confirm") } %>
</div>
<hr>
<% end %>
2 changes: 1 addition & 1 deletion app/views/fields/active_storage/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ By default, the attribute is rendered as an image tag.
<% if field.attached? %>
<%= render partial: 'fields/active_storage/items', locals: { field: field } %>
<% else %>
No attachment
<%= I18n.t("administrate.fields.active_storage.no_attachment", default: 'No attachment') %>
<% end %>
8 changes: 8 additions & 0 deletions config/i18n-tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
base_locale: en

data:
read:
- config/locales/administrate-field-active_storage.%{locale}.yml

ignore:
- 'administrate.actions.*'
10 changes: 10 additions & 0 deletions config/locales/administrate-field-active_storage.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
en:
administrate:
fields:
active_storage:
add: Add
attachment: Attachment
no_attachment: No attachment
remove: Remove
replace: Replace
10 changes: 10 additions & 0 deletions config/locales/administrate-field-active_storage.ru.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
ru:
administrate:
fields:
active_storage:
add: Добавить
attachment: Вложение
no_attachment: Нет
remove: Удалить
replace: Заменить
1 change: 1 addition & 0 deletions contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
- Sébastien Dubois [@sedubois](https://github.com/sedubois)
- Jazzy Gasper [@jazzygasper](https://github.com/jazzygasper)
- David Ma [@taikon](https://github.com/taikon)
- Dmitry Davydov [@haukot](https://github.com/haukot)
35 changes: 35 additions & 0 deletions spec/i18n_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'i18n/tasks'

# Template i18n-tasks test file from https://github.com/glebm/i18n-tasks/blob/main/templates/rspec/i18n_spec.rb
RSpec.describe I18n do
let(:i18n) { I18n::Tasks::BaseTask.new }
let(:missing_keys) { i18n.missing_keys }
let(:unused_keys) { i18n.unused_keys }
let(:inconsistent_interpolations) { i18n.inconsistent_interpolations }

it 'does not have missing keys' do
expect(missing_keys).to be_empty,
"Missing #{missing_keys.leaves.count} i18n keys, run `i18n-tasks missing' to show them"
end

it 'does not have unused keys' do
expect(unused_keys).to be_empty,
"#{unused_keys.leaves.count} unused i18n keys, run `i18n-tasks unused' to show them"
end

it 'files are normalized' do
non_normalized = i18n.non_normalized_paths
error_message = "The following files need to be normalized:\n" \
"#{non_normalized.map { |path| " #{path}" }.join("\n")}\n" \
"Please run `i18n-tasks normalize' to fix"
expect(non_normalized).to be_empty, error_message
end

it 'does not have inconsistent interpolations' do
error_message = "#{inconsistent_interpolations.leaves.count} i18n keys have inconsistent interpolations.\n" \
"Run `i18n-tasks check-consistent-interpolations' to show them"
expect(inconsistent_interpolations).to be_empty, error_message
end
end

0 comments on commit a2be284

Please sign in to comment.