AlchemyCMS / alchemy_cms

Alchemy is the Open Source Rails CMS framework for the component based web that can be used as classic server side rendered or headless CMS.
https://www.alchemy-cms.com
BSD 3-Clause "New" or "Revised" License
846 stars 315 forks source link

Alchemy CMS overrides setting my locale #492

Closed hopewise closed 10 years ago

hopewise commented 10 years ago

In my ApplicationController I have

before_filter :set_locale

Which I need to read locale from url ex: /en/ but I found that setting I18n.locale no more effect my app locale, after investigation, I found that lib/alchemy/controller_actions.rb:95 in set_alchemy_language would override my setting in set_locale before_filter

Even if I am at my existing app routes, set_alchemy_language will be called and override any set_locale filter, before I discovered this, I posted a question that has more details here http://stackoverflow.com/questions/22353229/how-to-force-locale-in-routes

Please tell me how to fix this, visiting http://lvh.me:3000/ will result into:

I18n::InvalidLocale in HomeController#home
"en-us" is not a valid locale

Rails.root: /home/samir/RubymineProjects/dcaclab

Application Trace | Framework Trace | Full Trace
i18n (0.6.9) lib/i18n.rb:288:in `enforce_available_locales!'
i18n (0.6.9) lib/i18n/config.rb:11:in `locale='
actionpack (4.0.3) lib/action_view/lookup_context.rb:226:in `locale='
actionpack (4.0.3) lib/abstract_controller/rendering.rb:28:in `locale='
i18n (0.6.9) lib/i18n.rb:35:in `locale='
/home/samir/.rvm/gems/ruby-2.1.0@dcaclab/bundler/gems/alchemy_cms-5dcddb40f801/lib/alchemy/controller_actions.rb:95:in `set_alchemy_language'
activesupport (4.0.3) lib/active_support/callbacks.rb:457:in `_run__345302891__process_action__callbacks'
activesupport (4.0.3) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.3) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.3) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.3) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.3) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.3) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.3) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.3) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.3) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.3) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.3) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.3) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.3) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.3) lib/action_dispatch/routing/route_set.rb:680:in `call'
omniauth (1.1.1) lib/omniauth/strategy.rb:177:in `call!'
omniauth (1.1.1) lib/omniauth/strategy.rb:157:in `call'
omniauth (1.1.1) lib/omniauth/builder.rb:48:in `call'
meta_request (0.2.0) lib/meta_request/middlewares/app_request_handler.rb:11:in `call'
rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.2.0) lib/meta_request/middlewares/headers.rb:16:in `call'
meta_request (0.2.0) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.3) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.3) lib/active_support/callbacks.rb:373:in `_run__167865611__call__callbacks'
activesupport (4.0.3) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.3) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.3) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.3) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.3) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.3) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
dragonfly (1.0.3) lib/dragonfly/cookie_monster.rb:9:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.3) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.3) lib/rails/engine.rb:511:in `call'
railties (4.0.3) lib/rails/application.rb:97:in `call'
railties (4.0.3) lib/rails/railtie/configurable.rb:30:in `method_missing'
rack (1.5.2) lib/rack/deflater.rb:25:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/samir/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/samir/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/samir/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
tvdeyen commented 10 years ago

Ok, we should move setting the I18n.locale into Alchemy::BaseController, so it not always sets the locale globally in any controller. I will fix this in a sec.

But besides that, you should always ensure that you have matching languages in your app and in Alchemy, so that the content from Alchemy and your App's locales is always in sync.

hopewise commented 10 years ago

Yea, but my matching language for English is 'en' while, for Alchemy, its 'en-us' I already have popular links for my website using /en/ so, I have to stick with /en/ rather than /en-us/ Can I keep my en locale to serve my existing app and set en-us for the CMS to en too?

May be its a good idea to give the hosting app a chance to have a callback for set_alchemy_language of the CMS, for ex: my existing app always needs to set locale upon forced locale in urls.

Please inform me when you re-scope it locally into the CMS.

tvdeyen commented 10 years ago

Any reason why are you using en-us for the Alchemy language?

Why not also just use en for it? The country code part is completely optional.

hopewise commented 10 years ago

I am already setting it to en at alchemy config, I don't know why it get en-us On Mar 15, 2014 12:06 PM, "Thomas von Deyen" notifications@github.com wrote:

Any reason why are you using en-us for the Alchemy language?

Why not also just use en for it? The country code part is completely optional.

Reply to this email directly or view it on GitHubhttps://github.com/magiclabs/alchemy_cms/issues/492#issuecomment-37720161 .

tvdeyen commented 10 years ago

The config setting is only relevant while seeding the default language.

You can always change the settings of all languages in the languages module.

hopewise commented 10 years ago

Can I change the settings of all languages in the languages module while I am using the CMS within my existing rails app? sorry, as it seems easy thing to do, but I am not sure how to this..

tvdeyen commented 10 years ago

The Languages module only manages the alchemy languages. They are not related to the locales from your host app. These are completely different things.

So yes, you change the alchemy languages independent from your apps locales. But you should keep them in sync. Meaning, you should have a language (and therefore a language page tree) for every locale in your app. So that you can manage translated pages for each of them.

I hope that clears things up a little bit.

hopewise commented 10 years ago

Thanks for clarification, I will read about it, meanwhile I will wait the fix you've mentioned in this post..

Good luck On Mar 15, 2014 3:31 PM, "Thomas von Deyen" notifications@github.com wrote:

The Languages module only manages the alchemy languages. They are not related to the locales from your host app. These are completely different things.

So yes, you change the alchemy languages independent from your apps locales. But you should keep them in sync. Meaning, you should have a language (and therefore a language page tree) for every locale in your app. So that you can manage translated pages for each of them.

I hope that clears things up a little bit.

Reply to this email directly or view it on GitHubhttps://github.com/magiclabs/alchemy_cms/issues/492#issuecomment-37723526 .

tvdeyen commented 10 years ago

The fix already landed. So please try it and report back.

hopewise commented 10 years ago

Thanks, the fix worked.

On Sat, Mar 15, 2014 at 3:39 PM, Thomas von Deyen notifications@github.comwrote:

The fix already landed. So please try it and report back.

Reply to this email directly or view it on GitHubhttps://github.com/magiclabs/alchemy_cms/issues/492#issuecomment-37723679 .

Kind Regards,

Samir Sabri Software Architect& Developer www.dcaclab.com Jordan-Middle East

tvdeyen commented 10 years ago

Great