translated_attr 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +152 -0
- metadata +95 -0
data/README.rdoc
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
== TranslatedAttr
|
2
|
+
|
3
|
+
TranslatedAttr is a minimal pure translation library for translating database values for Rails 3.x.
|
4
|
+
|
5
|
+
TranslatedAttr is quiet like puret http://github.com/jo/puret (and borrowed much of its code).
|
6
|
+
|
7
|
+
TranslatedAttr add some methods for dealing with nested form attributes
|
8
|
+
|
9
|
+
* ability to edit all translations ad one time with nested forms
|
10
|
+
* ability to find a record through a translated attribute (eg: Post.find_by_title)
|
11
|
+
* need some convenience methods for setting and getting specific locale attribute (eg: post.title_en = 'Hello')
|
12
|
+
|
13
|
+
== Installation
|
14
|
+
|
15
|
+
You need configure the translated_attr gem inside your gemfile:
|
16
|
+
|
17
|
+
gem 'translated_attr'
|
18
|
+
|
19
|
+
And then:
|
20
|
+
|
21
|
+
bundle install
|
22
|
+
|
23
|
+
|
24
|
+
== Basic Usage
|
25
|
+
|
26
|
+
This is a walkthrough with all steps you need to setup puret translated attributes, including model and migration. You MUST also check out the *Generators* section below to help you start.
|
27
|
+
|
28
|
+
We're assuming here you want a Post model with some puret attributes, as outlined below:
|
29
|
+
|
30
|
+
class Post < ActiveRecord::Base
|
31
|
+
translated_attr :title, :description
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
The pure translations are stored in a different translation model for every model you need translations for:
|
36
|
+
|
37
|
+
class PostTranslation < ActiveRecord::Base
|
38
|
+
translations_for :post
|
39
|
+
end
|
40
|
+
|
41
|
+
Create an initializer for setting the default locale and the available locale in your config/application.rb:
|
42
|
+
|
43
|
+
# config/initializers/locale.rb
|
44
|
+
I18n.available_locales = [:it, :en, :es]
|
45
|
+
# set default locale to something other than :en
|
46
|
+
I18n.default_locale = :it
|
47
|
+
|
48
|
+
Add the defa
|
49
|
+
|
50
|
+
|
51
|
+
You now need to create a migration for the translations table:
|
52
|
+
|
53
|
+
create_table(:post_translations) do |t|
|
54
|
+
t.references :post
|
55
|
+
t.string :locale
|
56
|
+
|
57
|
+
t.string :title
|
58
|
+
t.text :description
|
59
|
+
|
60
|
+
t.timestamps
|
61
|
+
end
|
62
|
+
add_index :post_translations, [:post_id, :locale],
|
63
|
+
:unique => true,
|
64
|
+
:name => 'index_on_post_translations' # Override default name because it could be too long for some DBMS as mySql
|
65
|
+
|
66
|
+
|
67
|
+
Now you are able to translate values for the attributes :title and :description per locale:
|
68
|
+
|
69
|
+
I18n.locale = :en
|
70
|
+
post.title = 'Hello!'
|
71
|
+
I18n.locale = :it
|
72
|
+
post.title = 'Ciao!'
|
73
|
+
|
74
|
+
I18n.locale = :en
|
75
|
+
post.title #=> Hello!
|
76
|
+
I18n.locale = :it
|
77
|
+
post.title #=> Ciao!
|
78
|
+
|
79
|
+
This fork let you do also:
|
80
|
+
|
81
|
+
post.title_en = 'Hello!'
|
82
|
+
post.title_it = 'Ciao!'
|
83
|
+
|
84
|
+
post.title_en #=> Hello!
|
85
|
+
post.title_it #=> Ciao!
|
86
|
+
|
87
|
+
And with dynamic finders:
|
88
|
+
|
89
|
+
I18n.locale = :en
|
90
|
+
Post.find_by_title('Hello!')
|
91
|
+
I18n.locale = :it
|
92
|
+
Post.find_by_title('Ciao!')
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
== Translation lookup fallback
|
97
|
+
|
98
|
+
If a translation is not available in your locale, puret looks
|
99
|
+
|
100
|
+
1. for an instance method called *default_locale* and the corresponding translation
|
101
|
+
2. for a class method called *default_locale* and the corresponding translation
|
102
|
+
3. for a translation in I18n.default_locale
|
103
|
+
|
104
|
+
In case a translation is not available in the default locale, puret uses the first locale it could find.
|
105
|
+
That order is specified by creation time, so the first created translation will be returned.
|
106
|
+
|
107
|
+
|
108
|
+
== Nested forms attributes
|
109
|
+
|
110
|
+
For dealing with nested form attributes you should use a partial like this (here we use simple_form gem):
|
111
|
+
|
112
|
+
<%= simple_form_for(@post) do |f| %>
|
113
|
+
<%= f.simple_fields_for :translations do |g| %>
|
114
|
+
<%= g.input :locale, :as => :hidden %>
|
115
|
+
<% g.object.class.attribute_names_for_translation.each do |attr| %>
|
116
|
+
<%= g.input attr,
|
117
|
+
:label => t("activerecord.attributes.#{f.object.class.name.underscore}.#{attr}_translation",
|
118
|
+
:lang => g.object.locale) %>
|
119
|
+
<% end %>
|
120
|
+
<% end %>
|
121
|
+
|
122
|
+
<%= f.button :submit %>
|
123
|
+
<% end %>
|
124
|
+
|
125
|
+
|
126
|
+
== Validators localization
|
127
|
+
|
128
|
+
For the validators you can create in config/locales/translated_attr folder the localization files:
|
129
|
+
|
130
|
+
# config/locales/translated_attr/it.yml
|
131
|
+
it:
|
132
|
+
translated_attr:
|
133
|
+
errors:
|
134
|
+
translations_presence: "manca la traduzione di %{attr} in :%{lang}"
|
135
|
+
translations_uniq: "traduzione duplicata con lo stesso locale"
|
136
|
+
|
137
|
+
# config/locales/translated_attr/en.yml
|
138
|
+
en:
|
139
|
+
translated_attr:
|
140
|
+
errors:
|
141
|
+
translations_presence: "missing :%{lang} translation of %{attr}"
|
142
|
+
translations_uniq: "duplicated translation with the same locale"
|
143
|
+
|
144
|
+
== Bugs and Feedback
|
145
|
+
|
146
|
+
If you discover any bugs or want to drop a line, feel free to create an issue on
|
147
|
+
GitHub:
|
148
|
+
|
149
|
+
http://github.com/mcanato/translated_attr/issues
|
150
|
+
|
151
|
+
|
152
|
+
Copyright (c) 2012 Matteo Canato, released under the MIT license
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: translated_attr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matteo Canato
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: jeweler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A minimal translation library for translating database values for Rails
|
63
|
+
3.x
|
64
|
+
email: mcanato@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files:
|
68
|
+
- README.rdoc
|
69
|
+
files:
|
70
|
+
- README.rdoc
|
71
|
+
homepage: http://github.com/mcanato/translated_attr
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.23
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: A minimal translation library for translating database values for Rails 3.x
|
95
|
+
test_files: []
|