Skip to content

Commit

Permalink
lesson 11 - I18n
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvlopes committed Aug 23, 2012
1 parent facc9e3 commit ecb92a1
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base
protect_from_forgery

respond_to :json, :html, :xml

end
16 changes: 14 additions & 2 deletions app/helpers/admin/base_helper.rb
@@ -1,12 +1,17 @@
module Admin::BaseHelper

def link_to_new(model, url, html_options = {})
link_to t("new", model: model.model_name.human), url, html_options
end

def link_to_edit(url, html_options = {})
html_options.reverse_merge!(:class => "green")
link_to "edit", url, html_options
link_to t("edit"), url, html_options
end

def link_to_destroy(url, html_options = {})
html_options.reverse_merge!(:confirm => 'Are you sure?', :method => :delete, :class => "red")
link_to "destroy", url, html_options
link_to t("destroy"), url, html_options
end

def section(title="", &block)
Expand All @@ -17,4 +22,11 @@ def section(title="", &block)
html.html_safe
end
end

def translate_attribute(model, attribute)
model.human_attribute_name(attribute)
end
alias_method :ta, :translate_attribute


end
16 changes: 8 additions & 8 deletions app/views/admin/posts/_form.html.erb
Expand Up @@ -3,43 +3,43 @@

<div id="body">
<div class="field">
<%= f.label :author_id, "Post by: " %>
<%= f.label :author_id, t(".author") %>
<%= f.collection_select :author_id, @authors, :id, :full_name %>
</div>

<div class="field">
<%= f.text_field :title, :placeholder => "Title", :class => "title" %>
<%= f.text_field :title, :placeholder => ta(Post, :title), :class => "title" %>
</div>

<div class="field">
<%= f.text_area :excerpt, :placeholder => "Excerpt", :rows => 5, :cols => 70 %>
<%= f.text_area :excerpt, :placeholder => ta(Post, :excerpt), :rows => 5, :cols => 70 %>
</div>

<div class="field">
<%= f.text_area :body, :placeholder => "Body", :rows => 30, :cols => 70 %>
<%= f.text_area :body, :placeholder => ta(Post, :body), :rows => 30, :cols => 70 %>
</div>

<div class="field">
<%= f.check_box :draft %>
<%= f.label :draft, "Save as Draft" %>
<%= f.label :draft %>
</div>

<hr class="thick" />

<p>
<%= f.submit :class => "button" %> or <%= link_to "cancel", admin_posts_path %>
<%= f.submit :class => "button" %> or <%= link_to t("cancel"), admin_posts_path %>
</p>
</div>

<div id="sidebar">
<%= section "Publish with these categories" do %>
<%= section t(".categories") do %>
<ul class="categories-field">
<% @categories.each do |c| %>
<li><%= check_box_tag "post[category_ids][]", c.id, @post.categories.include?(c) %> <%= c.name %></li>
<% end %>
</ul>
<div class="bar">
<%= link_to "New category", new_admin_category_path, :class => "button right" %>
<%= link_to_new Category, new_admin_category_path, :class => "button right" %>
</div>
<br/>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/posts/edit.html.erb
@@ -1,6 +1,6 @@
<h2>

Editing post
<%=t ".title" %>
<%= link_to "Preview", [:admin, @post],
:class => "button right",
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/posts/index.html.erb
Expand Up @@ -14,13 +14,13 @@
<% end %>
</ul>
<% else %>
<p>You don't have any posts yet.</p>
<p><%=t "empty", collection: "posts" %></p>
<% end %>

<hr class="thick" />

<div class="bar">
<div class="right">
<%= link_to 'New Post', new_admin_post_path, :class => "button" %>
<%= link_to_new Post, new_admin_post_path, :class => "button" %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/admin/posts/new.html.erb
@@ -1,3 +1,3 @@
<h2>New post</h2>
<h2><%=t ".title" %></h2>

<%= render 'form' %>
13 changes: 13 additions & 0 deletions app/views/pages/index.pt-BR.html.erb
@@ -0,0 +1,13 @@
<%= image_tag "banner.jpg", :class => "frame" %>

<div id="content">
<div id="column1">
<h2>Quem já utiliza Rails?</h2>
<p>Dezenas de centenas de aplicações Rails já estão rodando em todo o mundo. Pessoas estão utilizando o Rails em diversas situações, desde pequena operações pequenas até gigantes corporações.</p>
</div>

<div id="column2">
<h2>Quem está por trás?</h2>
<p>Rails foi criado em 2003 por David Heinemeier Hansson e desde então foi expandido pelo time central do Rails, mais de 1.400 contribuidores e suportado por um vibrante ecossistema.</p>
</div>
</div>
6 changes: 5 additions & 1 deletion app/views/shared/_error_messages.html.erb
@@ -1,6 +1,10 @@
<% if resource.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(resource.errors.count, "error") %> prohibited this post from being saved:</h2>
<h2>
<%= t "error_message_header",
:count => resource.errors.count,
:model => resource.class.model_name.human %>
</h2>

<ul>
<% resource.errors.full_messages.each do |msg| %>
Expand Down
4 changes: 2 additions & 2 deletions config/application.rb
Expand Up @@ -30,8 +30,8 @@ class Application < Rails::Application
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.i18n.load_path += Dir[File.join(Rails.root, 'config', 'locales', '**', '*.{rb,yml}')]
# config.i18n.default_locale = "pt-BR"

# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
Expand Down
5 changes: 0 additions & 5 deletions config/locales/en.yml

This file was deleted.

22 changes: 22 additions & 0 deletions config/locales/en/app.yml
@@ -0,0 +1,22 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
empty: "We don't have any %{collection} yet."
confirm: Are you sure?
edit: Edit
destroy: Destroy
new: Create %{model}
cancel: Cancel
error_message_header: "%{count} error(s) prohibited this %{model} from being saved:"

admin:
posts:
new:
title: New post
edit:
title: Editing post
form:
categories: Publish with these categories
slug: Set a custom url
author: "Post by:"
41 changes: 41 additions & 0 deletions config/locales/pt-BR/app.yml
@@ -0,0 +1,41 @@
pt-BR:
empty: "Não temos nenhum %{model} ainda."
confirm: Tem certeza?
edit: Editar
destroy: Apagar
cancel: Cancelar
new: Criar %{model}
error_message_header: "%{count} erro(s) impediram este %{model} de ser gravado:"

admin:
posts:
new:
title: Novo post
edit:
title: Editando post
form:
categories: Publicar com estas categorias
slug: Defina uma url
author: "Post por:"

activerecord:
models:
user:
one: Usuário
other: Usuários
category:
one: Categoria
other: Categorias
post:
one: Post
other: Posts

attributes:
post:
title: "Título"
body: "Conteúdo"
excerpt: "Resumo"
published_at: "Publicado em"
slug: "Url customizada"
author: "Autor"
draft: "Rascunho"

0 comments on commit ecb92a1

Please sign in to comment.