2009/01/24

Rails国际化的关键步骤。Rails I18n Key Steps.

Rails I18n的关键点


1、设定locales文档。
config/locales/en.yml
------------------------------
en:
  hello: "Hello world"
  store: "Store"
------------------------------
config/locales/zh-CN.yml
------------------------------
zh-CN:
  hello: "你好"
  store: "商店"
------------------------------
2、解析locale。
app/controller/application.rb
------------------------------
  class ApplicationController < ActionController::Base
    before_filter :set_locale
    def set_locale
      I18n.locale = params[:locale] || 'zh-CN'              #如果没有,则使用默认值。
    end
    ...
  end
------------------------------
3、在控制器中读取变量
------------------------------
class StoreController < ApplicationController
  def index
    @products = Product.find_products_for_sale
    flash[:hello_flash] = t(:hello_flash)
  end
  ...
end
------------------------------
4、在视图层中使用变量
------------------------------
      <% if flash[:hello_flash] -%>
        <div id="hello_flash"><%= flash[:hello_flash] %></div>
      <% end -%>
------------------------------

--
Liu Lantao
College of Information Science and Technology, Beijing Normal University
EMAIL: liulantao ( at ) gmail ( dot ) com ;
WEBSITE: http://www.liulantao.com/ .
------

2009/01/20

Rails开发中的MySQL中文问题以及简单解决方式

Rails2.2.2对中文的支持是不错的,当然不仅仅是中文,所有多字节文字都能够很好得被支持。
对于一个有很高本地化l10n和国际化i18n需求的网站,多语言支持的实现方式很多,最常用的是采用gettext来实现。

在网页生成过程中,初学者最容易出问题的就是数据库连接的编码问题。最便捷的解决方式是采用utf8编码方式。
Rails应用中config/database.yml中可以在数据库段中设置encoding: utf8如下:
----------------
development:
adapter: mysql
encoding: utf8
database: project_development
pool: 5
username: root
password:
host: localhost
----------------
对于使用MySQL数据库的应用,只要设置/etc/my.cnf就可以保证正确的存取utf8数据了。

检查/etc/my.cnf文件,保证里面有如下两段内容,如果没有则添加进去:
--------------
[client]
default-character-set=utf8

[mysqld]
default-character-set=utf8
init_connect='SET NAMES utf8'
--------------
修改保存之后,重启服务器即可。
--
Liu Lantao
College of Information Science and Technology, Beijing Normal University
EMAIL: liulantao ( at ) gmail ( dot ) com ;
WEBSITE: http://www.liulantao.com/ .
------

2009/01/17

ROR development with RadRails in Eclipse

安装Eclips。
菜单:Help - Software Updates - Available Software - Add Sites, http://radrails.sourceforge.net/update, http://updatesite.rubypeople.org/release
------------参考文章-------------------
installing RadRails on Eclipse ...........

I could not find a proper answer when i wanted to install RadRails on eclipse . This is how it is done , .... in the Eclipse menu go to
Help-->Software Updates --> Find and Install....
then in the pop - up window which appears ,..
Search for new features to install
and then click next and then u will have to add 2 new remote sites, .. the details for the sites are the ones which were very difficult to obtain , they are
site 1 :
Name :RadRails
URL : http://radrails.sourceforge.net/update
site 2 :
Name :RDT
URL : http://updatesite.rubypeople.org/release
then click on finish , u are almost done with the installation , u have to just follow the instructions from here on to finish the installation .

Original link:http://satisheerpini.blogspot.com/2008/05/installing-radrails-on-eclipse.html
-------------------------------
--
Liu Lantao
College of Information Science and Technology, Beijing Normal University
EMAIL: liulantao ( at ) gmail ( dot ) com ;
WEBSITE: http://www.liulantao.com/ .
------

2009/01/06

从“Rails 2.2 / Scaffold功能增强”看Rails敏捷性的提高

这几天看《Agile Web Development With Rails, 2nd Edition》(AWDWR),体验一下rails开发的敏捷。
不料第一个例子就卡住了。原来书上的例子所需要使用的方法是在rails1.2中有效的,而在rails2.2不被默认支持了。


我遇到的这个东西就是那个scaffold!从网上搜索发现大家都说要装plugin才能够使用,可是找到plugin的老巢也没看出什么门道。
于是打开AWDWR,3rd edition beta电子书来看,才发现这个东西已经是可以直接作为script/generate的对象来使用了。


下面是成功的使用方式,创建一个product的scaffold:
---------------------------------------------
depot> ruby script/generate scaffold product \
> title:string description:text image_url:string
exists app/models/
exists app/controllers/
exists app/helpers/
create app/views/products
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists public/stylesheets/
create app/views/products/index.html.erb
create app/views/products/show.html.erb
create app/views/products/new.html.erb
create app/views/products/edit.html.erb
create app/views/layouts/products.html.erb
create public/stylesheets/scaffold.css
create app/controllers/products_controller.rb
create test/functional/products_controller_test.rb
create app/helpers/products_helper.rb
route map.resources :products
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/product.rb
create test/unit/product_test.rb
create test/fixtures/products.yml
create db/migrate
create db/migrate/20080601000001_create_products.rb
-------------------------------------------------------
从上面的输出可以看出,这个命令已经整合起原来的命令方式。

下面是原来的方式:
------------------------
script/generate model product 并且修改db/migrate下的任务
script/generate controller admin  并且修改app/controllers/admin下的admin_controller.rb,增加scaffold :product
------------------------

下面的迁移任务还是同以前一样操作:
--------------------------------------------------------
depot> rake db:migrate
(in /Users/rubys/work/depot)
== 20080601000001 CreateProducts: migrating ===================================
-- create_table(:products)
-> 0.0027s
== 20080601000001 CreateProducts: migrated (0.0028s) ==========================
----------------------------------------------------

可以查看数据的版本:
----------------------------------------------------
depot> sqlite3 db/development.sqlite3 "select version from schema_migrations"
20080601000001
---------------------------------------------------

启动服务器:
---------------------------------------------------
depot> ruby script/server
=> Booting WEBrick...
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2008-03-27 11:54:55] INFO WEBrick 1.3.1
[2008-03-27 11:54:55] INFO ruby 1.8.6 (2007-09-24) [i486-linux]
[2008-03-27 11:54:55] INFO WEBrick::HTTPServer#start: pid=6200 port=3000
----------------------------------------------------

对比下两个版本中操作的便捷程度:
rails 1.2中, 输入命令 5 次,修改文件 2 处,
rails 2.2中,输入命令 3 次,修改文件 0 处。
可以明显看出新版的效率有了如此大的改进,真是让人欣喜。现在Rails仍然处于一个高速发展的阶段,相信不久它很快还能为我们带来更加便捷的开发方式。

--
Liu Lantao
College of Information Science and Technology, Beijing Normal University
EMAIL: liulantao ( at ) gmail ( dot ) com ;
WEBSITE: http://www.liulantao.com/ .
------