实现Rails应用的国际化与插件扩展
立即解锁
发布时间: 2025-08-20 01:21:02 阅读量: 1 订阅数: 3 


Rails 3入门:从零开始构建Web应用
### 实现 Rails 应用的国际化与插件扩展
#### 国际化支持
现在博客应用已经具备了国际化(i18n)支持。不过,要让 Rails 加载翻译文件,你必须重启服务器。重启后浏览网站,可能暂时看不到任何差异,因为虽然 i18n 支持已就位,但你仍然使用英语作为语言环境。为了看到 i18n 的实际效果,我们将语言环境更改为巴西葡萄牙语。
##### 本地化博客应用到巴西葡萄牙语
将支持 i18n 的 Rails 应用本地化到另一种语言出奇地简单。你只需添加一个新的翻译文件,并将 Rails 应用配置为使用该语言环境作为默认语言环境即可。
在这部分,我们将博客应用本地化到巴西葡萄牙语。巴西葡萄牙语的语言环境符号是 `pt-br`。首先,你需要更改 `config/locales/pt-br.yml` 文件,使用与英语翻译文件相同的键,但将英语文本替换为巴西葡萄牙语。翻译文件与应用代码的分离非常有用,例如,你可以将翻译文件发送给翻译人员,收到翻译好的文件后,将其插入应用即可。以下是新创建的巴西葡萄牙语翻译文件示例:
```yaml
pt-br:
general:
are_you_sure: Tem certeza?
back: Volta
cancel: Cancelar
create: Criar
delete: Apagar
edit: Editar
editing: Editando
footer: Um blog simples desenvolvido para o livro
email_a_friend: Avisar um amigo
search: Pesquisar
send_email: Mandar email
show: Mostrar
title: Blog
update: Atualizar
your_name: Seu nome
your_friend_email: O email do seu amigo
or: ou
application:
access_denied: "Por favor, efetue o login para continuar"
articles:
editing_article: Editando Artigo
listing_articles: Listando Artigos
new_article: Novo Artigo
article: artigo
create_success: Artigo foi criado com sucesso.
update_success: Artigo foi atualizado com sucesso.
articles: artigos
notify_friend_success: Seu amigo foi avisado a respeito desse artigo
users:
new_user: Novo Usuario
edit_password: Editar senha
editing_user: Editando usuario
create_success: Usuario editado com sucesso.
update_success: Usuario atualizado com sucesso.
sessions:
email: Email
password: Senha
login: Logar
logout: Desconectar
successful_login: Logado com sucesso
invalid_login: Senha ou Email invalidos
logout_success: Voce desconectou do sistem com sucesso
comments:
name: Nome
email: Email
body: Conteudo
comments: Comentarios
new_comment: Novo Comentario
create_success: Obrigado pelo comentario
create_failure: Nao foi possivel adicionar o comentario
destroy_success: Comentario deletado
add: Adicionar
errors:
not_published_yet: ainda nao foi publicado
activerecord:
errors:
models:
article:
attributes:
title:
blank: "não pode ficar em branco"
body:
blank: "não pode ficar em branco"
date:
formats:
default: "%d/%m/%Y"
short: "%d de %B"
long: "%d de %B de %Y"
day_names: [Domingo, Segunda, Terça, Quarta, Quinta, Sexta, Sábado]
abbr_day_names: [Dom, Seg, Ter, Qua, Qui, Sex, Sáb]
month_names: [~, Janeiro, Fevereiro, Março, Abril, Maio, Junho, Julho, Agosto, Setembro, Outubro, Novembro, Dezembro]
abbr_month_names: [~, Jan, Fev, Mar, Abr, Mai, Jun, Jul, Ago, Set, Out, Nov, Dez]
order: [:day, :month, :year]
```
你还需要告诉 Rails 使用 `pt-br` 作为默认语言环境。这可以通过在 `config/application.rb` 文件中添加配置来实现。以下是更新后的 `config/application.rb` 文件示例:
```ruby
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
module Blog
class Application < Rails::Application
# Activate observers that should always be running
config.active_record.observers = :comment_observer
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
# Setting the locale
config.i18n.default_locale = 'pt-br'
end
end
```
重启服务器,你就可以看到本地化到巴西葡萄牙语的博客应用了。你通过两个简单的步骤完成了应用的本地化:添加翻译文件和设置语言环境。
##### 双语博客
现在你已经知道,更改语言环境只需将 `I18n.locale` 配置设置为所需的语言环境。那么,如何让用户自己选择语言呢?你可以实现一个控制器过滤器,根据用户输入设置语言环境,并为用户提供一个语言选择器。
首先,在 `application_helper` 中创建一个名为 `language_selector` 的辅助方法,用于显示可供用户选择的语言环境。以下是 `application_helper` 中新增的辅助方法示例:
```ruby
module ApplicationHelper
# Creates a submit button with the given name with a cancel link
# Accepts two arguments: Form object and the cancel link name
def submit_or_cancel(form, name=t('general.cancel'))
```
0
0
复制全文
相关推荐










