Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 390
- Log:
Initial import of Canvass, a donations-based poll application.
- Author:
- rool
- Date:
- Mon Mar 21 14:58:04 +0000 2011
- Size:
- 3312 Bytes
1 | h1. TranslatableColumns |
2 | |
3 | |
4 | TranslatableColumns is a Ruby on Rails plugin, providing localization for model attributes. |
5 | |
6 | |
7 | |
8 | h2. Installing |
9 | |
10 | 1. First make sure you’re running Rails >= 2.2 or edge: |
11 | |
12 | rake rails:freeze:edge |
13 | |
14 | 2. Install the plugin: |
15 | |
16 | ./script/plugin install git://github.com/iain/translatable_columns.git |
17 | |
18 | 3. Create or modify a model to have multiple columns for one attribute: |
19 | |
20 | ./script/generate model Topic title_en:string title_nl:string title_de:string title_fr:string |
21 | |
22 | |
23 | |
24 | h2. Usage |
25 | |
26 | Identify the columns you want to translate: |
27 | |
28 | class Topic < ActiveRecord::Base |
29 | translatable_columns :title |
30 | end |
31 | |
32 | And you’re done! |
33 | |
34 | Create a form like this: |
35 | |
36 | <% form_for(@topic) do |f| %> |
37 | <%= f.text_field :title %> |
38 | <% end %> |
39 | |
40 | And it will save to whatever locale is set in I18n. No hard feelings, nothing to worry about. |
41 | |
42 | |
43 | h2. Validating |
44 | |
45 | Validation is of course built in. If you want to validate the presence of at least one of the translations, just call validates_translation_of: |
46 | |
47 | class Topic < ActiveRecord::Base |
48 | translatable_columns :title |
49 | validates_translation_of :title |
50 | end |
51 | |
52 | This will make your record invalid when none of the translated columns exist. It works exactly as validates_presence_of, including all its options! |
53 | |
54 | h2. Customizing |
55 | |
56 | You can change the settings of translatable_columns on both a global level and at individual attribute level. There are two configuration options at the moment, called full_locale and use_default. |
57 | |
58 | Set the global configuration in your environment file. These are the defaults of translatable_columns: |
59 | |
60 | ActiveRecord::Base.translatable_columns_config.full_locale = false |
61 | ActiveRecord::Base.translatable_columns_config.use_default = true |
62 | |
63 | |
64 | h3. full_locale |
65 | |
66 | With this option you can change which part of the locale is used in the columns. Default is false, so only the first part of the locale is expected in the column. So a title for en-US is called title_en and a title for en-GB is also called title_en. When you set full_locale to true, it uses the entire locale, substituting the hyphen with an underscore. This way a title for en-US is called title_en_us and a title for en-GB is called title_en_gb. |
67 | |
68 | full_locale cannot be set per attribute just now. |
69 | |
70 | |
71 | h3. use_default |
72 | |
73 | With this option you can specify which value will be returned automatically if no proper value has been found. Default is true, so it will try harder to find a value. It might even be a value in another language. |
74 | |
75 | You can set this option per attribute if you’d like, to override the global config. |
76 | |
77 | class Topic < ActiveRecord::Base |
78 | translatable_columns :title, :use_default => false |
79 | end |
80 | |
81 | |
82 | |
83 | h2. Some extras |
84 | |
85 | What if the user has selected a locale which you don’t have in the database? In this case it’ll get the column belonging to the I18n.default_locale. Make sure you have a column for this locale, because you’ll be serving a nasty error if even this one isn’t present! |
86 | |
87 | You might want to provide multiple languages for a user to fill in at once. This is one way to do it: |
88 | |
89 | <% form_for(@topic) do |f| %> |
90 | <% Topic.available_translatable_columns_of(:title).each do |attribute| %> |
91 | <%= f.text_field attribute %> |
92 | <% end %> |
93 | <% end %> |
94 | |
95 | |
96 | --- |
97 | Copyright (c) 2008 Iain Hecker, released under the MIT license |