diff --git a/app/controllers/translator/translations_controller.rb b/app/controllers/translator/translations_controller.rb index 4acc05e..533c3dd 100644 --- a/app/controllers/translator/translations_controller.rb +++ b/app/controllers/translator/translations_controller.rb @@ -75,6 +75,20 @@ def destroy redirect_to :back unless request.xhr? end + def locale + I18n.locale = params[:loc] || I18n.default_locale + + keys = Translator.keys_for_strings + translations = Hash[keys.map { |key| + [ + key, + begin I18n.t key; rescue; end + ] + }] + + render json: translations + end + private def auth diff --git a/config/routes.rb b/config/routes.rb index 009451a..be0ebe8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ collection do get :export post :import + get 'locale/:loc', to: 'translations#locale', as: :by_locale, defaults: { format: 'json' } end end end diff --git a/spec/controller/translator/translations_controller_spec.rb b/spec/controller/translator/translations_controller_spec.rb new file mode 100644 index 0000000..0d9571d --- /dev/null +++ b/spec/controller/translator/translations_controller_spec.rb @@ -0,0 +1,21 @@ +require_relative '../../spec_helper' + +describe Translator::TranslationsController, type: :controller do + + before :each do + Translator.current_store = Translator::RedisStore.new(Redis.new) + I18n.backend = Translator.setup_backend(I18n::Backend::Simple.new) + Translator.current_store.clear_database + end + + describe 'by_locale' do + it "has endpoint returning translations in JSON format" do + get 'locale', loc: 'en', use_route: :translator + json = JSON.parse(response.body) + Translator.keys_for_strings.each do |key| + expect(json).to have_key(key) + end + end + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7c7d6d6..ddce1ee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,6 +23,7 @@ # methods or matchers require 'rspec/expectations' config.include RSpec::Matchers + config.include Rails.application.routes.url_helpers # == Mock Framework config.mock_with :rspec