Skip to content

Instantly share code, notes, and snippets.

@dabit
Last active May 1, 2019 15:18
Show Gist options
  • Save dabit/f4b43f5abc9b2e8042832437173b1a4c to your computer and use it in GitHub Desktop.
Save dabit/f4b43f5abc9b2e8042832437173b1a4c to your computer and use it in GitHub Desktop.
translations
I18n.t('welcome', scope: 'pages.title.welcome')
I18n.t('pages.title.welcome')
I18n.t('activerecord.model.dog') # => "Bienvenido"
<h1>Welcome to RailsConf</h1>
<p>I hope you enjoy my talk</p>
<h1><%= t('pages.welcome.title') %></h1>
<p><%= t('pages.welcome.body') %></p>
es:
pages:
title:
welcome: "Bienvenido"
es:
pages:
welcome:
title: "Bienvenidos a RailsConf"
body: "Espero que disfrutes mi charla"
en:
pages:
welcome:
title: "Welcome to RailsConf"
body: "I hope you enjoy my talk"
class Habitacion ; end
class HabitacionesController ; end
class Reservacion ; end
class ReservacionesController ; end
class Room ; end
class RoomsController ; end
class Reservation ; end
class ReservactionsController ; end
# config/application.rb
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
gem 'i18n-debug', group: :development
es:
attributes:
name: Nombre
es:
activerecord:
attributes:
author:
name: Nombre de pila
<h1>New Author</h1>
<%= render 'form', author: @author %>
<%= link_to 'Back', authors_path %>
<h1><%= t('.page_title') %></h1>
<%= render 'form', author: @author %>
<%= link_to 'Back', authors_path %>
<h1><%= I18n.t('.page_title') %></h1>
<%= render 'form', author: @author %>
<%= link_to 'Back', authors_path %>
<h1><%= t('.page_title') %></h1>
<h2><%= t('.title1') %></h2>
<p><%= t('.paragraph1') %></p>
<h2><%= t('.title2') %></h2>
<p><%= t('.paragraph2') %></p>
<h2><%= t('.title3') %></h2>
<p><%= t('.paragraph3') %></p>
<h2><%= t('.title4') %></h2>
<p><%= t('.paragraph4') %></p>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
# POST /authors
def create
@author = Author.new(author_params)
respond_to do |format|
if @author.save
format.html { redirect_to @author, notice: I18n.t('.flash') }
else
format.html { render :new }
end
end
end
# POST /authors
def create
@author = Author.new(author_params)
respond_to do |format|
if @author.save
format.html { redirect_to @author, notice: 'Author was successfully created.' }
else
format.html { render :new }
end
end
end
module Railsconf
class Application < Rails::Application
config.i18n.fallbacks = { en_GB: [ :en ] }
end
end
en:
fallbacks:
show:
soccer: Soccer
football: Football
restroom: Restroom
bill: Bill
shower: Shower
words: WORDS
in: IN
common: COMMON
en_GB:
fallbacks:
show:
soccer: Football
football: American Football
restroom: Toilet
bill: Note
shower: Bath
class Author < ApplicationRecord
enum category: %i(classic independent mistery fiction)
end
def new
@author = Author.new
@categories = Author.categories
end
def new
@author = Author.new
@categories = Author.categories.map {|c,i| [t(c), i]}
end
gem 'i18n-active_record', :require => 'i18n/active_record'
class CreateTranslations < ActiveRecord::Migration
def self.up
create_table :translations do |t|
t.string :locale
t.string :key
t.text :value
t.text :interpolations
t.boolean :is_proc, :default => false
t.timestamps
end
end
end
# config/initializers/translations.rb
I18n.backend = x
require 'i18n/backend/active_record'
I18n.backend = I18n::Backend::ActiveRecord.new
Translation = I18n::Backend::ActiveRecord::Translation
Translation.create locale: :es, key: 'authors.new.page_title', value: 'Autor Nuevo'
Translation.create locale: :es, key: 'helpers.label.author.name', value: 'Nombre de autor'
Translation.create locale: :es, key: 'attributes.bio', value: 'Biografía'
Translation.create locale: :es, key: 'attributes.age', value: 'Edad'
Translation.create locale: :es, key: 'attributes.category', value: 'Categoría'
Translation.create locale: :es, key: 'helpers.submit.create', value: 'Crear %{model}'
Translation.create locale: :es, key: 'classic', value: 'Clásico'
require 'i18n/backend/hodor'
I18n.backend = I18n::Backend::Hodor.new
require 'i18n/backend/base'
class I18n::Backend::Hodor
include I18n::Backend::Base
def lookup(locale, key, scope = [], options = {})
"Hodor"
end
end
I18n::Backend::Simple
I18n::Backend::ActiveRecord
I18n::Backend::Redis
I18n::Backend::Mongodb
I18n::Backend::Gettext
I18n::Backend::Chain
require 'i18n/backend/active_record'
I18n.backend = I18n::Backend::Chain.new( I18n::Backend::ActiveRecord.new, I18n.backend )
en:
time:
formats:
default: "%H:%M"
formal: "The time is now %H:%M good sir"
informal: "The time is %H, bye"
thing: "Clobberin time"
rock: "It doesn't matter what time it is"
distance_of_time_in_words_to_now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment