Skip to content
Newer
Older
100644 85 lines (49 sloc) 2.51 KB
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
1 = has_foreign_language
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
2
3 has_foreign_language makes it easy to internationalize your database without mucking up your views and controllers or having to write a bunch of YAML.
e82d1a3 @SeanRoberts Fixed up the readme
SeanRoberts authored Jan 21, 2009
4 Just create fields for each locale you want to support in addition to your default field and has_foreign_language will automatically call the appropriate one based on the current locale.
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
5
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
6 == Installation
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
7
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
8 ==== Using Rails 3
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
9
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
10 Include the gem in your Gemfile:
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
11
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
12 gem "has_foreign_language", :git => "git@github.com:develonlab/has_foreign_language.git"
13
14 == Example
15
16 === First create your model:
17
18 rails g model book title:string description:text
19
20 === Then specify which fields need to be internationalized in your model:
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
21
22 class Country < ActiveRecord::Base
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
23 has_foreign_language :title, :description
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
24 end
25
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
26 === Finally generate a migration with internationalized fields:
27
28 Sintax:
29
30 rails g has_foreign_language <language> [Model]
31
32 Example:
33
34 rails g has_foreign_language fr
35
36 rails g has_foreign_language de
37
38
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
39 Now let's create a book in the console and see how this works:
40
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
41 I18n.default_locale = "en"
42 b = Book.new(:title => "Slaughterhouse Five", :title_fr => "Abattoir Cinq", :title_de => "Schlachthof Fünf")
43 b.title # Slaughterhouse Five
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
44
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
45 I18n.locale = "fr"
46 b.title # Abattoir Cinq
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
47
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
48 I18n.locale = "de"
49 b.title # Schlachthof Fünf
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
50
51 Calling title on the model returns the appropriate column depending on which locale we're in. This also works when we change the attribute:
52
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
53 I18n.locale = "de"
54 b.title = "Etwas Anderes"
55 b # Book id: nil, title: "Slaughterhouse Five", title_fr: "Abattoir Cinq", title_de: "Etwas Anderes"
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
56
e82d1a3 @SeanRoberts Fixed up the readme
SeanRoberts authored Jan 21, 2009
57 If you want to set or return the default language attribute while in a different locale, you can call it the way you would call any other. Assuming the default is "en", you would run:
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
58
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
59 b.title_en # Slaughterhouse Five
7b76978 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
60 b.title_en = "Something Else" # Book id: nil, title: "Something Else", title_fr: "Abattoir Cinq", title_de: "Etwas Anderes"
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
61
62
63
64
cce4461 @dalpo has_foreign_language gemified
dalpo authored Sep 22, 2011
65 == Validations
e82d1a3 @SeanRoberts Fixed up the readme
SeanRoberts authored Jan 21, 2009
66
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
67 Validations are easy. Just run the validation on the default field name and it will apply to whatever locale you're in.
68
69 class Country < ActiveRecord::Base
70 has_foreign_language :title
71 validates_presence_of :title
72 end
73
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
74 I18n.locale = "de"
18c591a @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
75 b = Book.new(:title => "Etwas Anderes") # Book id: nil, title: nil, title_fr: nil, title_de: "Etwas Anderes"
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
76 b.valid? # true
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
77
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
78 I18n.locale = "en"
79 b.valid? # false
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
80
388c238 @SeanRoberts More readme cleanup
SeanRoberts authored Jan 21, 2009
81 b.title = "Slaughterhouse Five"
82 b.valid? # true
06db1fd @SeanRoberts First commit
SeanRoberts authored Jan 20, 2009
83
84
e82d1a3 @SeanRoberts Fixed up the readme
SeanRoberts authored Jan 21, 2009
85 Copyright (c) 2008 factor[e] design initiative, released under the MIT license
Something went wrong with that request. Please try again.