Skip to content

Commit

Permalink
Completando I18n
Browse files Browse the repository at this point in the history
  • Loading branch information
peimelo committed Nov 26, 2021
1 parent 179bf09 commit f974b3d
Show file tree
Hide file tree
Showing 39 changed files with 215 additions and 58 deletions.
6 changes: 4 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def switch_locale(&action)
I18n.with_locale(locale, &action)
end

def user_not_authorized
flash[:alert] = 'You are not authorized to perform this action.'
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
6 changes: 3 additions & 3 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 @@ -52,7 +52,7 @@ 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 :edit
end
Expand All @@ -61,7 +61,7 @@ def update
def destroy
@article.destroy

redirect_to root_path, notice: 'Article was successfully destroyed.'
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 destroyed.' }
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 successfully 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 successfully destroyed.'
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)
l(datetime, format: '%B %e, %Y').capitalize
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
8 changes: 4 additions & 4 deletions app/policies/category_policy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CategoryPolicy < ApplicationPolicy
class Scope < Scope
def resolve
if user.admin? || user.has_role?(:moderator)
if user&.admin? || user&.has_role?(:moderator)
scope.all
else
raise Pundit::NotAuthorizedError
Expand All @@ -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
2 changes: 1 addition & 1 deletion app/views/articles/_archives.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<ol class="list-unstyled mb-0">
<% @archives.each do |month_year, count| %>
<li>
<%= link_to "#{month_year.capitalize} (#{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
6 changes: 3 additions & 3 deletions app/views/articles/_article.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

<p><%= link_to t('articles.continue_reading'), article, class: 'btn btn-link' %></p>

<%= link_to 'Edit', edit_article_path(article),
<%= link_to t('app.button.edit'), edit_article_path(article),
class: 'btn btn-primary' if policy(article).update? %>

<%= link_to 'Destroy', article_path(article),
<%= link_to t('app.button.destroy'), article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' },
data: { confirm: t('app.message.confirm_destroy') },
class: 'btn btn-danger' if policy(article).destroy? %>
</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) %> <%= t('app.words.by') %>
<%= sub_masked_email(article.user.email) %> <%= t('app.words.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 %>
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,11 +2,11 @@
<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-action">
<%= 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/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 %>
2 changes: 1 addition & 1 deletion app/views/articles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</h3>

<p>
<%= link_to t('.new_article'), new_article_path,
<%= link_to t('articles.new.title'), new_article_path,
class: 'btn btn-success' if user_signed_in? %>
</p>

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 @@ -6,23 +6,23 @@

<p><%= @article.body %></p>

<%= link_to 'Edit', edit_article_path(@article),
<%= link_to t('app.button.edit'), edit_article_path(@article),
class: 'btn btn-primary' if policy(@article).update? %>

<%= link_to 'Destroy', article_path(@article),
<%= link_to t('app.button.destroy'), article_path(@article),
method: :delete,
data: { confirm: 'Are you 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' %>
<%= link_to t('app.button.back'), root_path, class: 'btn btn-secondary' %>

<h2 class="mt-3">Comments</h2>
<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>
4 changes: 2 additions & 2 deletions app/views/categories/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1>Editing Category</h1>
<h1><%= t('app.edit.title', model: Category.model_name.human) %></h1>

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

<%= link_to 'Back', categories_path %>
<%= link_to t('app.button.back'), categories_path %>
12 changes: 6 additions & 6 deletions app/views/categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<h1>Categories</h1>
<h1><%= t Category.model_name.human(count: 2) %></h1>

<p>
<%= link_to 'New Category', new_category_path,
<%= link_to t('categories.new.title', model: Category.model_name.human), new_category_path,
class: 'btn btn-primary' if policy(:category).new? %>
</p>

<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Name</th>
<th><%= t Category.human_attribute_name('name') %></th>
<th>Actions</th>
</tr>
</thead>
Expand All @@ -18,9 +18,9 @@
<tr>
<td><%= category.name %></td>
<td>
<%= link_to 'Edit', edit_category_path(category) if policy(category).edit? %>
<%= link_to 'Destroy', category,
method: :delete, data: { confirm: 'Are you sure?' } if policy(category).destroy? %>
<%= link_to t('app.button.edit'), edit_category_path(category) if policy(category).edit? %>
<%= link_to t('app.button.destroy'), category,
method: :delete, data: { confirm: t('app.message.confirm_destroy') } if policy(category).destroy? %>
</td>
</tr>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/categories/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1>New Category</h1>
<h1><%= t '.title' %></h1>

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

<%= link_to 'Back', categories_path %>
<%= link_to t('app.button.back'), categories_path %>
7 changes: 0 additions & 7 deletions app/views/categories/show.html.erb

This file was deleted.

1 change: 0 additions & 1 deletion app/views/categories/show.json.jbuilder

This file was deleted.

6 changes: 3 additions & 3 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<div class="card-body">
<h5 class="card-title"><%= month_day_comma_year(comment.created_at) %></h5>
<h6 class="card-subtitle mb-2 text-muted">
Commenter: <%= sub_masked_email(comment.user.email) %>
<%= t 'comments.index.commenter' %>: <%= sub_masked_email(comment.user.email) %>
</h6>
<p class="card-text"><%= comment.body %></p>
<%= link_to 'Destroy', article_comment_path(@article, comment),
<%= link_to t('app.button.destroy'), article_comment_path(@article, comment),
method: :delete,
data: { confirm: 'Are you sure?' },
data: { confirm: t('app.message.confirm_destroy') },
class: 'btn btn-danger' if policy(comment).destroy? %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/layouts/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<p>
<%= t 'app.footer.template_info' %>
<a href="https://getbootstrap.com/">Bootstrap</a>
<%= t 'app.words.by' %>
<%= t 'app.word.by' %>
<a href="https://twitter.com/mdo">@mdo</a>.
</p>
<p>
Expand Down
5 changes: 5 additions & 0 deletions app/views/layouts/_locale_links.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if I18n.locale == I18n.default_locale %>
<%= link_to 'English', { locale: :'en' }, class: 'link-secondary' %>
<% else %>
<%= link_to 'Português', { locale: :'pt-BR' }, class: 'link-secondary' %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/layouts/_topnavbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
</div>

<div class="col-4 d-flex justify-content-end align-items-center">
<%= render 'layouts/locale_links' %>
&nbsp;&nbsp;
<%= render 'layouts/account_links' %>
</div>
</div>
Expand Down
11 changes: 11 additions & 0 deletions config/locales/models/article/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
en:
activerecord:
models:
article:
one: Article
other: Articles
attributes:
article:
title: Title
body: Body
category: Category
11 changes: 11 additions & 0 deletions config/locales/models/article/pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pt-BR:
activerecord:
models:
article:
one: Artigo
other: Artigos
attributes:
article:
title: Título
body: Conteúdo
category: Categoria
10 changes: 10 additions & 0 deletions config/locales/models/category/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
en:
activerecord:
models:
category:
one: Category
other: Categories
attributes:
category:
articles: Articles
name: Name
10 changes: 10 additions & 0 deletions config/locales/models/category/pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pt-BR:
activerecord:
models:
category:
one: Categoria
other: Categorias
attributes:
category:
articles: Artigos
name: Nome
9 changes: 9 additions & 0 deletions config/locales/models/comment/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
en:
activerecord:
models:
comment:
one: Comment
other: Comments
attributes:
comment:
body: Body
9 changes: 9 additions & 0 deletions config/locales/models/comment/pt-BR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pt-BR:
activerecord:
models:
comment:
one: Comentário
other: Comentários
attributes:
comment:
body: Conteúdo
14 changes: 14 additions & 0 deletions config/locales/views/articles/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@ en:
index:
subtitle: From my Experience
new_article: New Article
new:
title: New Article
create:
success: Article was successfully created.
update:
success: Article was successfully updated.
destroy:
success: Article was successfully destroyed.
form:
choose_category: Choose a Category
show:
comments: Comments
add_comment: "Add a comment:"
login_create_comment: Please, login to create a comment.
Loading

0 comments on commit f974b3d

Please sign in to comment.