Skip to content

Commit

Permalink
Internationalization
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusflorez committed Jul 25, 2022
1 parent c775b89 commit 714965a
Show file tree
Hide file tree
Showing 88 changed files with 7,479 additions and 226 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ gem "rolify", "~> 6.0"
gem "groupdate", "~> 6.1"

gem 'pg', '~> 1.4', '>= 1.4.1', group: :production

gem "devise-i18n", "~> 1.10"
27 changes: 27 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
bcrypt (3.1.18)
bcrypt (3.1.18-java)
bindex (0.8.1)
bootsnap (1.12.0)
msgpack (~> 1.2)
Expand All @@ -90,8 +91,13 @@ GEM
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
devise-i18n (1.10.2)
devise (>= 4.8.0)
erubi (1.10.0)
ffi (1.15.5)
ffi (1.15.5-java)
ffi (1.15.5-x64-mingw32)
ffi (1.15.5-x86-mingw32)
globalid (1.0.0)
activesupport (>= 5.0)
groupdate (6.1.0)
Expand All @@ -102,6 +108,7 @@ GEM
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.6.2)
json (2.6.2-java)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
Expand Down Expand Up @@ -134,22 +141,35 @@ GEM
mini_portile2 (2.8.0)
minitest (5.16.2)
msgpack (1.5.3)
msgpack (1.5.3-java)
nio4r (2.5.8)
nio4r (2.5.8-java)
nokogiri (1.13.8)
mini_portile2 (~> 2.8.0)
racc (~> 1.4)
nokogiri (1.13.8-java)
racc (~> 1.4)
nokogiri (1.13.8-x64-mingw32)
racc (~> 1.4)
nokogiri (1.13.8-x86-mingw32)
racc (~> 1.4)
orm_adapter (0.5.0)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
pg (1.4.1)
pg (1.4.1-x64-mingw32)
pg (1.4.1-x86-mingw32)
prettier_print (0.1.0)
public_suffix (4.0.7)
puma (5.6.4)
nio4r (~> 2.0)
puma (5.6.4-java)
nio4r (~> 2.0)
pundit (2.2.0)
activesupport (>= 3.0.0)
racc (1.6.0)
racc (1.6.0-java)
rack (2.2.4)
rack-mini-profiler (2.3.4)
rack (>= 1.2.0)
Expand Down Expand Up @@ -217,6 +237,8 @@ GEM
sassc-rails (~> 2.1, >= 2.1.1)
sassc (2.4.0)
ffi (~> 1.9)
sassc (2.4.0-x64-mingw32)
ffi (~> 1.9)
sassc-rails (2.1.2)
railties (>= 4.0.0)
sassc (>= 2.0)
Expand Down Expand Up @@ -251,6 +273,8 @@ GEM
turbolinks-source (5.2.0)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2022.1)
tzinfo (>= 1.0.0)
unicode-display_width (2.2.0)
uniform_notifier (1.16.0)
warden (1.2.9)
Expand All @@ -272,6 +296,8 @@ GEM
websocket (1.2.9)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-driver (0.7.5-java)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
Expand All @@ -290,6 +316,7 @@ DEPENDENCIES
byebug
capybara (>= 3.26)
devise (~> 4.8)
devise-i18n (~> 1.10)
groupdate (~> 6.1)
jbuilder (~> 2.7)
kaminari (~> 1.2)
Expand Down
20 changes: 17 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ class ApplicationController < ActionController::Base

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

around_action :switch_locale

private

def user_not_authorized
flash[:alert] = 'You are not authorized to perform this action.'
redirect_back(fallback_location: root_path)
def default_url_options
{ locale: I18n.locale }
end

def switch_locale(&action)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &action)
end


def user_not_authorized(exception)
policy_name = exception.policy.class.to_s.underscore

flash[:alert] = t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default
redirect_to(request.referrer || root_path)
end
end
8 changes: 4 additions & 4 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def index
.desc_order
.page(current_page)

@archives = Article.group_by_month(:created_at, format: '%B %Y').count
@archives = Article.group_by_month(:created_at, format: '%B %Y', locale: :en).count
end

def show
Expand All @@ -40,7 +40,7 @@ def create
@article = current_user.articles.new(article_params)

if @article.save
redirect_to @article, notice: 'Article was successfully created.'
redirect_to @article, notice: t('.success')
else
render :new
end
Expand All @@ -50,15 +50,15 @@ def edit; end

def update
if @article.update(article_params)
redirect_to @article, notice: 'Article was successfully updated.'
redirect_to @article, notice: t('.success')
else
render :new
end
end

def destroy
@article.destroy
redirect_to root_path, notice: 'Article was successfully deleted.'
redirect_to root_path, notice: t('.success')
end

private
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create

respond_to do |format|
if @category.save
format.html { redirect_to categories_url, notice: 'Category was successfully created.' }
format.html { redirect_to categories_url, notice: t('app.create.success', model: Category.model_name.human)}
format.json { render :show, status: :created, location: @category }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -36,7 +36,7 @@ def create
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to categories_url, notice: 'Category was successfully updated.' }
format.html { redirect_to categories_url, notice: t('app.update.success', model: Category.model_name.human) }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -49,7 +49,7 @@ def update
def destroy
if @category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully deleted.' }
format.html { redirect_to categories_url, notice: t('app.destroy.success', model: Category.model_name.human) }
format.json { head :no_content }
end
else
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class CommentsController < ApplicationController

def create
@article.comments.create(comment_params.to_h.merge!({ user_id: current_user.id }))
redirect_to article_path(@article), notice: 'Comment was successfuly created.'
redirect_to article_path(@article), notice: t('app.create.success', model: Comment.model_name.human)
end

def destroy
comment = @article.comments.find(params[:id])
authorize comment

comment.destroy
redirect_to article_path(@article), notice: 'Comment was successfuly deleted.'
redirect_to article_path(@article), notice: t('app.destroy.success', model: Comment.model_name.human)
end

private
Expand Down
8 changes: 6 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module ApplicationHelper
def month_day_comma_year(datetime)
datetime.strftime('%B %e, %Y')
def month_day_comma_year(value)
l(value, format: '%B %e, %Y').capitalize
end

def month_year(value)
l(value.to_datetime, format: '%B %Y').capitalize
end

def render_if(condition, template, record)
Expand Down
6 changes: 3 additions & 3 deletions app/policies/category_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def index?
end

def create?
user.admin? || user.has_role?(:moderator)
user&.admin? || user.has_role?(:moderator)
end

def update?
user.admin? || user.has_role?(:moderator)
user&.admin? || user.has_role?(:moderator)
end

def destroy?
user.admin?
user&.admin?
end
end
4 changes: 2 additions & 2 deletions app/views/articles/_about.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="p-4 mb-3 bg-light rounded">
<h4 class="fst-italic">About</h4>
<p class="mb-0">Hey there! My name is Mateus, im a programmer aiming to become a Ruby on Rails developer!</p>
<h4 class="fst-italic"><%= t 'articles.about.title' %></h4>
<p class="mb-0"><%= t 'articles.about.description' %></p>
</div>
4 changes: 2 additions & 2 deletions app/views/articles/_archives.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<div class="p-4">
<h4 class="fst-italic">Archives</h4>
<h4 class="fst-italic"><%= t 'articles.archives' %></h4>
<ol class="list-unstyled mb-0">
<% @archives.each do |month_year, count| %>
<li>
<%= link_to "#{month_year} (#{count})", articles_path(month_year: month_year),
<%= link_to "#{month_year(month_year)} (#{count})", articles_path(month_year: month_year),
class: "p-2 link-secondary #{'filter-active' if month_year == params[:month_year]}" %>
</li>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/_article.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
<h2 class="blog-post-title mb-1"><%= article.title %></h2>
<p class="blog-post-meta"><%= render 'article_detail', article: article %></p>
<p><%= truncate(article.body, length: 150) %></p>
<p><%= link_to 'continue reading...', article, class: 'btn btn-link' %></p>
<p><%= link_to t('articles.continue_reading'), article, class: 'btn btn-link' %></p>
</article>
4 changes: 2 additions & 2 deletions app/views/articles/_article_detail.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<%= month_day_comma_year(article.created_at) %> by
<%= sub_masked_email(article.user.email) %> in
<%= month_day_comma_year(article.created_at) %> <%= t 'app.word.by' %>
<%= sub_masked_email(article.user.email) %> <%= t 'app.word.in' %>
<%= article.category.name %>
2 changes: 1 addition & 1 deletion app/views/articles/_elsewhere.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="p-4">
<h4 class="fst-italic">Elsewhere</h4>
<h4 class="fst-italic"><%= t 'articles.elsewhere' %></h4>
<ol class="list-unstyled">
<li><a href="https://github.com/mateusflorez" target="_blank">GitHub</a></li>
<li><a href="https://www.linkedin.com/in/mateuspaz/" target="_blank">LinkedIn</a></li>
Expand Down
4 changes: 2 additions & 2 deletions app/views/articles/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :body, input_html: { rows: 10 } %>
<%= f.association :category, collection: @categories, prompt: 'Choose a category' %>
<%= f.association :category, collection: @categories, prompt: t('articles.form.choose_category') %>
</div>
<div class="form-actions">
<%= f.button :submit, class: 'btn btn-primary' %>
<%= link_to 'Back', root_path, class: 'btn btn-secondary' %>
<%= link_to t('app.button.back'), root_path, class: 'btn btn-secondary' %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/articles/_longer_featured.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<h1 class="display-4 fst-italic"><%= article.title %></h1>
<p class="lead my-3"><%= truncate(article.body, length: 100) %></p>
<p class="lead mb-0">
<%= link_to 'Continue reading...', article, class: 'text-white fw-bold' %>
<%= link_to t('articles.continue_reading'), article, class: 'text-white fw-bold' %>
</p>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/articles/_medium_featured.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<h3 class="mb-0"><%= article.title %></h3>
<div class="mb-1 text-muted"><%= render 'article_detail', article: article %></div>
<p class="card-text mb-auto"><%= truncate(article.body, length: 100) %></p>
<%= link_to 'Continue reading...', article, class: 'stretched-link' %>
<%= link_to t('articles.continue_reading'), article, class: 'stretched-link' %>
</div>
<div class="col-auto d-none d-lg-block">
<svg class="bd-placeholder-img" width="200" height="250" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Placeholder: Thumbnail" preserveAspectRatio="xMidYMid slice" focusable="false"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"/><text x="50%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
Expand Down
4 changes: 2 additions & 2 deletions app/views/articles/_pagination.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<nav class="blog-pagination" aria-label="Pagination">
<%= link_to_next_page @articles, 'Older', class: 'btn btn-outline-primary rounded-pill' %>
<%= link_to_previous_page @articles, 'Newer', class: 'btn btn-outline-secondary rounded-pill' %>
<%= link_to_next_page @articles, t('app.pagination.older'), class: 'btn btn-outline-primary rounded-pill' %>
<%= link_to_previous_page @articles, t('app.pagination.newer'), class: 'btn btn-outline-secondary rounded-pill' %>
</nav>
2 changes: 1 addition & 1 deletion app/views/articles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h1>Edit article</h1>
<h1><%= t('app.edit.title', model: Article.model_name.human ) %></h1>

<%= render 'form', article: @article %>
4 changes: 2 additions & 2 deletions app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<div class="row g-5">
<div class="col-md-8">
<h3 class="pb-4 mb-4 fst-italic border-bottom">
Newest posts
<%= t '.subtitle' %>
</h3>
<p><%= link_to 'New article', new_article_path,
<p><%= link_to t('.new_article'), new_article_path,
{class: 'btn btn-success',
'data-bs-toggle':'tooltip',
'data-bs-placement':'top',
Expand Down
2 changes: 1 addition & 1 deletion app/views/articles/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<h1>New article</h1>
<h1><%= t '.title' %></h1>

<%= render 'form', article: @article %>
14 changes: 7 additions & 7 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<h2 class="blog-post-title mb-1"><%= @article.title %></h2>
<p class="blog-post-meta"><%= render 'article_detail', article: @article %></p>
<p><%= @article.body %></p>
<%= link_to 'Edit', edit_article_path(@article), class: 'btn btn-primary' if policy(@article).update? %>
<%= link_to 'Delete', article_path(@article),
<%= link_to t('app.button.edit'), edit_article_path(@article), class: 'btn btn-primary' if policy(@article).update? %>
<%= link_to t('app.button.destroy'), article_path(@article),
method: :delete,
data: { confirm: 'Are your sure?' },
data: { confirm: t('app.message.confirm_destroy') },
class: 'btn btn-danger' if policy(@article).destroy? %>
<%= link_to 'Back', root_path, class: 'btn btn-secondary' %>
<h2 class="mt-3">Comments</h2>
<%= link_to t('app.button.back'), root_path, class: 'btn btn-secondary' %>
<h2 class="mt-3"><%= t '.comments' %></h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<h3><%= t '.add_comment' %>:</h3>
<% if policy(:comment).create? %>
<%= render 'comments/form' %>
<% else %>
Please, login to create a comment
<%= t '.login_create_comment' %>
<% end %>
</article>
2 changes: 1 addition & 1 deletion app/views/categories/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</div>
<div class="form-actions">
<%= f.button :submit, class: 'btn btn-primary' %>
<%= link_to 'Back', categories_path, class: 'btn btn-secondary' %>
<%= link_to t('app.button.back'), categories_path, class: 'btn btn-secondary' %>
</div>
<% end %>
4 changes: 3 additions & 1 deletion app/views/categories/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<h1>Editing Category</h1>
<h1><%= t('app.edit.title', model: Category.model_name.human) %></h1>

<%= render 'form', category: @category %>

<%= link_to t('app.button.back'), categories_path %>
Loading

0 comments on commit 714965a

Please sign in to comment.