Commit 852b2fe7df3ec0e4b17479f14d6f118a6c5146cd
1 parent
b5d67b0a
Exists in
master
and in
29 other branches
[language-selection] Migration and model base
Showing
22 changed files
with
122 additions
and
33 deletions
Show diff stats
app/controllers/application_controller.rb
... | ... | @@ -42,9 +42,9 @@ class ApplicationController < ActionController::Base |
42 | 42 | |
43 | 43 | before_filter :set_locale |
44 | 44 | def set_locale |
45 | - FastGettext.available_locales = Noosfero.available_locales | |
46 | - FastGettext.default_locale = Noosfero.default_locale | |
47 | - FastGettext.locale = (params[:lang] || session[:lang] || Noosfero.default_locale || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en') | |
45 | + FastGettext.available_locales = environment.available_locales | |
46 | + FastGettext.default_locale = environment.default_locale | |
47 | + FastGettext.locale = (params[:lang] || session[:lang] || environment.default_locale || request.env['HTTP_ACCEPT_LANGUAGE'] || 'en') | |
48 | 48 | I18n.locale = FastGettext.locale |
49 | 49 | if params[:lang] |
50 | 50 | session[:lang] = params[:lang] | ... | ... |
app/controllers/my_profile/cms_controller.rb
... | ... | @@ -345,7 +345,7 @@ class CmsController < MyProfileController |
345 | 345 | end |
346 | 346 | |
347 | 347 | def translations |
348 | - @locales = Noosfero.locales.invert.reject { |name, lang| !@article.possible_translations.include?(lang) } | |
348 | + @locales = environment.locales.invert.reject { |name, lang| !@article.possible_translations.include?(lang) } | |
349 | 349 | @selected_locale = @article.language || FastGettext.locale |
350 | 350 | end |
351 | 351 | ... | ... |
app/helpers/content_viewer_helper.rb
... | ... | @@ -42,7 +42,7 @@ module ContentViewerHelper |
42 | 42 | def article_translations(article) |
43 | 43 | unless article.native_translation.translations.empty? |
44 | 44 | links = (article.native_translation.translations + [article.native_translation]).map do |translation| |
45 | - { Noosfero.locales[translation.language] => { :href => url_for(translation.url) } } | |
45 | + { article.environment.locales[translation.language] => { :href => url_for(translation.url) } } | |
46 | 46 | end |
47 | 47 | content_tag(:div, link_to(_('Translations'), '#', |
48 | 48 | :onclick => "toggleSubmenu(this, '#{_('Translations')}', #{links.to_json}); return false", | ... | ... |
app/helpers/language_helper.rb
... | ... | @@ -13,18 +13,18 @@ module LanguageHelper |
13 | 13 | |
14 | 14 | alias :calendar_date_select_language :tinymce_language |
15 | 15 | |
16 | - def language_chooser(options = {}) | |
16 | + def language_chooser(environment, options = {}) | |
17 | 17 | current = language |
18 | 18 | separator = options[:separator] || ' — ' |
19 | 19 | |
20 | 20 | if options[:element] == 'dropdown' |
21 | 21 | select_tag('lang', |
22 | - options_for_select(Noosfero.locales.map{|code,name| [name, code]}, current), | |
22 | + options_for_select(environment.locales.map{|code,name| [name, code]}, current), | |
23 | 23 | :onchange => "document.location.href= #{url_for(params.merge(:lang => 'LANGUAGE')).inspect}.replace(/LANGUAGE/, this.value) ;", |
24 | 24 | :help => _('The language you choose here is the language used for options, buttons, etc. It does not affect the language of the content created by other users.') |
25 | 25 | ) |
26 | 26 | else |
27 | - languages = Noosfero.locales.map do |code,name| | |
27 | + languages = environment.locales.map do |code,name| | |
28 | 28 | if code == current |
29 | 29 | content_tag('strong', name) |
30 | 30 | else | ... | ... |
app/models/article.rb
... | ... | @@ -325,14 +325,14 @@ class Article < ActiveRecord::Base |
325 | 325 | end |
326 | 326 | |
327 | 327 | def possible_translations |
328 | - possibilities = Noosfero.locales.keys - self.native_translation.translations(:select => :language).map(&:language) - [self.native_translation.language] | |
328 | + possibilities = environment.locales.keys - self.native_translation.translations(:select => :language).map(&:language) - [self.native_translation.language] | |
329 | 329 | possibilities << self.language unless self.language_changed? |
330 | 330 | possibilities |
331 | 331 | end |
332 | 332 | |
333 | 333 | def known_language |
334 | 334 | unless self.language.blank? |
335 | - errors.add(:language, N_('Language not supported by Noosfero')) unless Noosfero.locales.key?(self.language) | |
335 | + errors.add(:language, N_('Language not supported by the environment.')) unless environment.locales.key?(self.language) | |
336 | 336 | end |
337 | 337 | end |
338 | 338 | ... | ... |
app/models/block.rb
app/models/box.rb
... | ... | @@ -2,4 +2,8 @@ class Box < ActiveRecord::Base |
2 | 2 | belongs_to :owner, :polymorphic => true |
3 | 3 | acts_as_list :scope => 'owner_id = #{owner_id} and owner_type = \'#{owner_type}\'' |
4 | 4 | has_many :blocks, :dependent => :destroy, :order => 'position' |
5 | + | |
6 | + def environment | |
7 | + owner.kind_of?(Environment) ? owner : owner.environment | |
8 | + end | |
5 | 9 | end | ... | ... |
app/models/environment.rb
... | ... | @@ -766,4 +766,21 @@ class Environment < ActiveRecord::Base |
766 | 766 | def image_galleries |
767 | 767 | portal_community ? portal_community.image_galleries : [] |
768 | 768 | end |
769 | + | |
770 | + def locales | |
771 | + languages || Noosfero.locales | |
772 | + end | |
773 | + | |
774 | + def default_locale | |
775 | + default_language || Noosfero.default_locale | |
776 | + end | |
777 | + | |
778 | + def available_locales | |
779 | + locales_list = locales.keys | |
780 | + # move English to the beginning | |
781 | + if locales_list.include?('en') | |
782 | + locales_list = ['en'] + (locales_list - ['en']).sort | |
783 | + end | |
784 | + locales_list | |
785 | + end | |
769 | 786 | end | ... | ... |
app/sweepers/article_sweeper.rb
... | ... | @@ -20,7 +20,7 @@ protected |
20 | 20 | BlockSweeper.expire_blocks(blocks) |
21 | 21 | env = article.profile.environment |
22 | 22 | if env && (env.portal_community == article.profile) |
23 | - Noosfero.locales.keys.each do |locale| | |
23 | + article.environment.locales.keys.each do |locale| | |
24 | 24 | expire_fragment(env.portal_news_cache_key(locale)) |
25 | 25 | end |
26 | 26 | end | ... | ... |
app/sweepers/block_sweeper.rb
... | ... | @@ -10,7 +10,7 @@ class BlockSweeper < ActiveRecord::Observer |
10 | 10 | regex = '-[a-z]*$' |
11 | 11 | clean_ck = block.cache_key.gsub(/#{regex}/,'') |
12 | 12 | |
13 | - Noosfero.locales.keys.each do |locale| | |
13 | + block.environment.locales.keys.each do |locale| | |
14 | 14 | expire_timeout_fragment("#{clean_ck}-#{locale}") |
15 | 15 | end |
16 | 16 | end | ... | ... |
app/views/box_organizer/edit.rhtml
... | ... | @@ -21,7 +21,7 @@ |
21 | 21 | <%= label_tag('block_display_never', _("Don't display")) %> |
22 | 22 | </div> |
23 | 23 | |
24 | - <%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + Noosfero.locales.map {|key, value| [value, key]} )) %> | |
24 | + <%= labelled_form_field(_('Show for:'), select(:block, :language, [ [ _('all languages'), 'all']] + environment.locales.map {|key, value| [value, key]} )) %> | |
25 | 25 | |
26 | 26 | <% button_bar do %> |
27 | 27 | <%= submit_button(:save, _('Save')) %> | ... | ... |
app/views/cms/_blog.rhtml
... | ... | @@ -62,7 +62,7 @@ |
62 | 62 | |
63 | 63 | <% f.fields_for 'feed', @article.feed do |feed| %> |
64 | 64 | <%= labelled_form_field(_('Limit of posts in RSS Feed'), feed.select(:limit, [5, 10, 20, 50])) %> |
65 | - <%= labelled_form_field(_('Include in RSS Feed only posts from language:'), feed.select(:language, [[_('All'), nil ]] + Noosfero.locales.map { |k,v| [v, k]})) %> | |
65 | + <%= labelled_form_field(_('Include in RSS Feed only posts from language:'), feed.select(:language, [[_('All'), nil ]] + environment.locales.map { |k,v| [v, k]})) %> | |
66 | 66 | <% end %> |
67 | 67 | |
68 | 68 | <% f.fields_for 'external_feed_builder', @article.external_feed do |efeed| %> | ... | ... |
app/views/cms/_rss_feed.rhtml
... | ... | @@ -6,7 +6,7 @@ |
6 | 6 | |
7 | 7 | <%= required labelled_form_field(_('Limit of articles'), text_field(:article, :limit)) %> |
8 | 8 | |
9 | -<%= labelled_form_field(_('Include in RSS Feed only posts from language:'), f.select(:language, [[_('All'), nil ]] + Noosfero.locales.map { |k,v| [v, k]})) %> | |
9 | +<%= labelled_form_field(_('Include in RSS Feed only posts from language:'), f.select(:language, [[_('All'), nil ]] + environment.locales.map { |k,v| [v, k]})) %> | |
10 | 10 | |
11 | 11 | <%= labelled_form_field(_('Use as item description:'), select(:article, :feed_item_description, [ [ _('Article abstract'), 'abstract'], [ _('Article body'), 'body']])) %> |
12 | 12 | ... | ... |
app/views/layouts/application.rhtml
db/migrate/20120823215007_add_languages_and_default_language_to_environment.rb
0 → 100644
... | ... | @@ -0,0 +1,11 @@ |
1 | +class AddLanguagesAndDefaultLanguageToEnvironment < ActiveRecord::Migration | |
2 | + def self.up | |
3 | + add_column :environments, :languages, :string | |
4 | + add_column :environments, :default_language, :string | |
5 | + end | |
6 | + | |
7 | + def self.down | |
8 | + remove_column :environments, :languages | |
9 | + remove_column :environments, :default_language | |
10 | + end | |
11 | +end | ... | ... |
db/schema.rb
... | ... | @@ -9,7 +9,7 @@ |
9 | 9 | # |
10 | 10 | # It's strongly recommended to check this file into your version control system. |
11 | 11 | |
12 | -ActiveRecord::Schema.define(:version => 20120818030329) do | |
12 | +ActiveRecord::Schema.define(:version => 20120823215007) do | |
13 | 13 | |
14 | 14 | create_table "abuse_reports", :force => true do |t| |
15 | 15 | t.integer "reporter_id" |
... | ... | @@ -259,6 +259,8 @@ ActiveRecord::Schema.define(:version => 20120818030329) do |
259 | 259 | t.datetime "created_at" |
260 | 260 | t.datetime "updated_at" |
261 | 261 | t.integer "reports_lower_bound", :default => 0, :null => false |
262 | + t.string "languages" | |
263 | + t.string "default_language" | |
262 | 264 | end |
263 | 265 | |
264 | 266 | create_table "external_feeds", :force => true do |t| | ... | ... |
public/designs/themes/base/footer.rhtml
... | ... | @@ -4,4 +4,4 @@ |
4 | 4 | <div id="copyright"> |
5 | 5 | <p><%= _('This social network uses <a href="http://noosfero.org/">Noosfero</a>, developed by %s and licensed under the <a href="http://www.gnu.org/licenses/agpl.html">GNU Affero General Public License</a> version 3 or any later version.') % link_to('Colivre', 'http://colivre.coop.br/') %></p> |
6 | 6 | </div><!-- end id="copyright" --> |
7 | -<%= language_chooser %> | |
7 | +<%= language_chooser(environment) %> | ... | ... |
test/unit/article_test.rb
... | ... | @@ -1211,7 +1211,7 @@ class ArticleTest < ActiveSupport::TestCase |
1211 | 1211 | end |
1212 | 1212 | |
1213 | 1213 | should 'validade inclusion of language' do |
1214 | - a = build(Article) | |
1214 | + a = build(Article, :profile_id => fast_create(Profile).id) | |
1215 | 1215 | a.language = '12' |
1216 | 1216 | a.valid? |
1217 | 1217 | assert a.errors.invalid?(:language) |
... | ... | @@ -1243,7 +1243,7 @@ class ArticleTest < ActiveSupport::TestCase |
1243 | 1243 | end |
1244 | 1244 | |
1245 | 1245 | should 'list possible translations' do |
1246 | - native_article = fast_create(Article, :language => 'pt') | |
1246 | + native_article = fast_create(Article, :language => 'pt', :profile_id => fast_create(Profile).id ) | |
1247 | 1247 | article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id) |
1248 | 1248 | possible_translations = native_article.possible_translations |
1249 | 1249 | assert !possible_translations.include?('en') |
... | ... | @@ -1253,7 +1253,7 @@ class ArticleTest < ActiveSupport::TestCase |
1253 | 1253 | should 'verify if translation is already in use' do |
1254 | 1254 | native_article = fast_create(Article, :language => 'pt') |
1255 | 1255 | article_translation = fast_create(Article, :language => 'en', :translation_of_id => native_article.id) |
1256 | - a = build(Article) | |
1256 | + a = build(Article, :profile => fast_create(Profile)) | |
1257 | 1257 | a.language = 'en' |
1258 | 1258 | a.translation_of = native_article |
1259 | 1259 | a.valid? |
... | ... | @@ -1265,7 +1265,7 @@ class ArticleTest < ActiveSupport::TestCase |
1265 | 1265 | |
1266 | 1266 | should 'verify if native translation is already in use' do |
1267 | 1267 | native_article = fast_create(Article, :language => 'pt') |
1268 | - a = build(Article) | |
1268 | + a = build(Article, :profile => fast_create(Profile)) | |
1269 | 1269 | a.language = 'pt' |
1270 | 1270 | a.translation_of = native_article |
1271 | 1271 | a.valid? |
... | ... | @@ -1277,7 +1277,7 @@ class ArticleTest < ActiveSupport::TestCase |
1277 | 1277 | |
1278 | 1278 | should 'translation have a language' do |
1279 | 1279 | native_article = fast_create(Article, :language => 'pt') |
1280 | - a = build(Article) | |
1280 | + a = build(Article, :profile_id => fast_create(Profile).id) | |
1281 | 1281 | a.translation_of = native_article |
1282 | 1282 | a.valid? |
1283 | 1283 | assert a.errors.invalid?(:language) |
... | ... | @@ -1287,8 +1287,8 @@ class ArticleTest < ActiveSupport::TestCase |
1287 | 1287 | end |
1288 | 1288 | |
1289 | 1289 | should 'native translation have a language' do |
1290 | - native_article = fast_create(Article) | |
1291 | - a = build(Article) | |
1290 | + native_article = fast_create(Article, :profile_id => fast_create(Profile).id ) | |
1291 | + a = build(Article, :profile_id => fast_create(Profile).id) | |
1292 | 1292 | a.language = 'en' |
1293 | 1293 | a.translation_of = native_article |
1294 | 1294 | a.valid? |
... | ... | @@ -1356,15 +1356,15 @@ class ArticleTest < ActiveSupport::TestCase |
1356 | 1356 | end |
1357 | 1357 | |
1358 | 1358 | should 'not list own language as a possible translation if language has changed' do |
1359 | - a = build(Article, :language => 'pt') | |
1359 | + a = build(Article, :language => 'pt', :profile_id => fast_create(Profile).id) | |
1360 | 1360 | assert !a.possible_translations.include?('pt') |
1361 | - a = fast_create(Article, :language => 'pt') | |
1361 | + a = fast_create(Article, :language => 'pt', :profile_id => fast_create(Profile).id ) | |
1362 | 1362 | a.language = 'en' |
1363 | 1363 | assert !a.possible_translations.include?('en') |
1364 | 1364 | end |
1365 | 1365 | |
1366 | 1366 | should 'list own language as a possible translation if language has not changed' do |
1367 | - a = fast_create(Article, :language => 'pt') | |
1367 | + a = fast_create(Article, :language => 'pt', :profile_id => fast_create(Profile).id) | |
1368 | 1368 | assert a.possible_translations.include?('pt') |
1369 | 1369 | end |
1370 | 1370 | ... | ... |
test/unit/block_test.rb
... | ... | @@ -107,7 +107,7 @@ class BlockTest < ActiveSupport::TestCase |
107 | 107 | |
108 | 108 | should 'be able to save display setting' do |
109 | 109 | user = create_user('testinguser').person |
110 | - box = fast_create(Box, :owner_id => user.id) | |
110 | + box = fast_create(Box, :owner_id => user.id, :owner_type => 'Profile') | |
111 | 111 | block = create(Block, :display => 'never', :box_id => box.id) |
112 | 112 | block.reload |
113 | 113 | assert_equal 'never', block.display |
... | ... | @@ -115,7 +115,7 @@ class BlockTest < ActiveSupport::TestCase |
115 | 115 | |
116 | 116 | should 'be able to update display setting' do |
117 | 117 | user = create_user('testinguser').person |
118 | - box = fast_create(Box, :owner_id => user.id) | |
118 | + box = fast_create(Box, :owner_id => user.id, :owner_type => 'Profile') | |
119 | 119 | block = create(Block, :display => 'never', :box_id => box.id) |
120 | 120 | assert block.update_attributes!(:display => 'always') |
121 | 121 | block.reload |
... | ... | @@ -148,4 +148,12 @@ class BlockTest < ActiveSupport::TestCase |
148 | 148 | assert_equal false, block.visible?(:locale => 'en') |
149 | 149 | end |
150 | 150 | |
151 | + should 'delegate environment to box' do | |
152 | + box = fast_create(Box, :owner_id => fast_create(Profile).id) | |
153 | + block = Block.new(:box => box) | |
154 | + box.stubs(:environment).returns(Environment.default) | |
155 | + | |
156 | + assert_equal box.environment, block.environment | |
157 | + end | |
158 | + | |
151 | 159 | end | ... | ... |
... | ... | @@ -0,0 +1,12 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class BoxTest < ActiveSupport::TestCase | |
4 | + should 'retrieve environment based on owner' do | |
5 | + profile = fast_create(Profile) | |
6 | + box = fast_create(Box, :owner_id => profile.id, :owner_type => 'Profile') | |
7 | + assert_equal profile.environment, box.environment | |
8 | + | |
9 | + box = fast_create(Box, :owner_id => Environment.default.id, :owner_type => 'Environment') | |
10 | + assert_equal Environment.default, box.environment | |
11 | + end | |
12 | +end | ... | ... |
test/unit/environment_test.rb
... | ... | @@ -1220,4 +1220,37 @@ class EnvironmentTest < ActiveSupport::TestCase |
1220 | 1220 | assert_includes environment.licenses, l2 |
1221 | 1221 | assert_not_includes environment.licenses, l3 |
1222 | 1222 | end |
1223 | + | |
1224 | + should 'define default locale or use the config default locale' do | |
1225 | + environment = Environment.default | |
1226 | + environment.default_language = nil | |
1227 | + environment.save! | |
1228 | + assert_equal Noosfero.default_locale, environment.default_locale | |
1229 | + | |
1230 | + environment.default_language = 'en' | |
1231 | + environment.save! | |
1232 | + assert_equal environment.default_language, environment.default_locale | |
1233 | + end | |
1234 | + | |
1235 | + should 'define locales or use the config locales' do | |
1236 | + environment = Environment.default | |
1237 | + environment.languages = nil | |
1238 | + environment.save! | |
1239 | + assert_equal Noosfero.locales, environment.locales | |
1240 | + | |
1241 | + environment.languages = {'en' => 'English'} | |
1242 | + environment.save! | |
1243 | + assert_equal environment.languages, environment.locales | |
1244 | + end | |
1245 | + | |
1246 | + should 'define available_locales or use the config available_locales' do | |
1247 | + environment = Environment.default | |
1248 | + environment.languages = nil | |
1249 | + environment.save! | |
1250 | + assert_equal Noosfero.available_locales, environment.available_locales | |
1251 | + | |
1252 | + environment.languages = {'pt' => 'Português', 'en' => 'English'} | |
1253 | + environment.save! | |
1254 | + assert_equal ['en', 'pt'], environment.available_locales | |
1255 | + end | |
1223 | 1256 | end | ... | ... |
test/unit/language_helper_test.rb
... | ... | @@ -23,14 +23,14 @@ class LanguageHelperTest < ActiveSupport::TestCase |
23 | 23 | Noosfero.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro', 'fr' => 'Français', 'it' => 'Italiano' }).at_least_once |
24 | 24 | |
25 | 25 | self.expects(:language).returns('pt_BR') |
26 | - result = self.language_chooser | |
26 | + result = self.language_chooser(Environment.default) | |
27 | 27 | assert_match /<strong>Português Brasileiro<\/strong>/, result |
28 | 28 | assert_no_match /<strong>English<\/strong>/, result |
29 | 29 | assert_no_match /<strong>Français<\/strong>/, result |
30 | 30 | assert_no_match /<strong>Italiano<\/strong>/, result |
31 | 31 | |
32 | 32 | self.expects(:language).returns('fr') |
33 | - result = self.language_chooser | |
33 | + result = self.language_chooser(Environment.default) | |
34 | 34 | assert_no_match /<strong>Português Brasileiro<\/strong>/, result |
35 | 35 | assert_no_match /<strong>English<\/strong>/, result |
36 | 36 | assert_match /<strong>Français<\/strong>/, result |
... | ... | @@ -42,7 +42,7 @@ class LanguageHelperTest < ActiveSupport::TestCase |
42 | 42 | Noosfero.expects(:locales).returns({ 'en' => 'English', 'pt_BR' => 'Português Brasileiro', 'fr' => 'Français', 'it' => 'Italiano' }).at_least_once |
43 | 43 | |
44 | 44 | self.expects(:language).returns('en') |
45 | - result = self.language_chooser(:element => 'dropdown') | |
45 | + result = self.language_chooser(Environment.default, :element => 'dropdown') | |
46 | 46 | assert_match /<option value="en" selected="selected">English<\/option>/, result |
47 | 47 | assert_match /<option value="pt_BR">Português Brasileiro<\/option>/, result |
48 | 48 | assert_match /<option value="fr">Français<\/option>/, result | ... | ... |