#-- # Copyright (c) 2015 Translation Exchange Inc. http://translationexchange.com # # _______ _ _ _ ______ _ # |__ __| | | | | (_) | ____| | | # | |_ __ __ _ _ __ ___| | __ _| |_ _ ___ _ __ | |__ __ _____| |__ __ _ _ __ __ _ ___ # | | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \| __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \ # | | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ > < (__| | | | (_| | | | | (_| | __/ # |_|_| \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___| # __/ | # |___/ # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ module Tr8nClientSdk module ActionControllerExtension def self.included(base) base.send(:include, Tr8nClientSdk::ActionCommonMethods) base.send(:include, InstanceMethods) base.before_filter :tr8n_init_client_sdk base.after_filter :tr8n_reset_client_sdk if defined? base.rescue_from base.rescue_from 'Tr8n::Exception' do |e| Tr8n.logger.error(e) Tr8n.logger.error(e.backtrace) Tr8n.session.reset raise e end end end module InstanceMethods def tr8n_browser_accepted_locales @tr8n_browser_accepted_locales ||= Tr8n::Utils.browser_accepted_locales(request) end def tr8n_user_preferred_locale tr8n_browser_accepted_locales.each do |locale| next unless Tr8n.session.application.locales.include?(locale) return locale end Tr8n.config.default_locale end # Overwrite this method in a controller to assign a custom source for all views def tr8n_source "/#{controller_name}/#{action_name}" rescue self.class.name end # Overwrite this method in a controller to assign a custom component for all views def tr8n_component nil end def tr8n_toggle_tools(flag) session[:tr8n_tools_disabled] = !flag end def tr8n_tools_enabled? not session[:tr8n_tools_disabled] end def tr8n_locale self.send(Tr8n.config.current_locale_method) if Tr8n.config.current_locale_method rescue session[:locale] = tr8n_user_preferred_locale unless session[:locale] session[:locale] = params[:locale] if params[:locale] session[:locale] || Tr8n.config.default_locale end def tr8n_init_client_sdk return if Tr8n.config.disabled? @tr8n_started_at = Time.now if params[:tr8n] tr8n_toggle_tools(params[:tr8n] == 'on') end tr8n_session_params = { :tools_enabled => tr8n_tools_enabled?, :source => tr8n_source, :component => tr8n_component } if Tr8n.config.current_user_method begin tr8n_session_params.merge!(:user => self.send(Tr8n.config.current_user_method)) rescue # Tr8n.logger.error('Current user method is specified but not provided') end end if tr8n_tools_enabled? # gets translator and locale from the cookie tr8n_session_params.merge!(:cookies => request.cookies) else # uses default locale tr8n_session_params.merge!(:locale => tr8n_locale) end Tr8n.session.init(tr8n_session_params) I18n.enforce_available_locales = false I18n.locale = Tr8n.session.current_language.locale end def tr8n_reset_client_sdk return if Tr8n.config.disabled? @tr8n_finished_at = Time.now tr8n_application.submit_missing_keys Tr8n.session.reset Tr8n.logger.info("Request took #{@tr8n_finished_at - @tr8n_started_at} mls") if @tr8n_started_at end end end end