Class: CitizenBudgetModel::TranslationsController

Inherits:
CitizenBudgetModelController show all
Defined in:
app/controllers/citizen_budget_model/translations_controller.rb

Instance Method Summary collapse

Methods inherited from CitizenBudgetModelController

#admin?, #check_authorization!, #set_locale, #simulators, #sort, #users

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/citizen_budget_model/translations_controller.rb', line 17

def create
  if params[:pk] && params[:value]
    locale, key = params[:pk].split('.', 2)
    if I18n.backend.exists?(I18n.default_locale, key)
      I18n.backend.store_translations(locale, {key => params[:value]}, :escape => false)
      head :no_content
    else
      head :bad_request
    end
  else
    head :bad_request
  end
end

#exportObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/citizen_budget_model/translations_controller.rb', line 31

def export
  translations = {'' => {lang: I18n.locale}}
  store = I18n.backend.backends.first.store
  store.keys.each do |k|
    locale, key = k.split('.', 2)
    if locale.to_sym == I18n.locale
      translations[key.tr(I18n::Backend::Flatten::SEPARATOR_ESCAPE_CHAR, I18n::Backend::Flatten::FLATTEN_SEPARATOR)] = [nil, JSON.load(store[k])]
    end
  end

  # @note We may need to add translations as necessary.
  %w(
    number.format.separator
    number.currency.format.delimiter
    number.currency.format.format
    number.currency.format.separator
    number.currency.format.unit
    number.percentage.format.delimiter
    number.percentage.format.format
  ).each do |key|
    translations[key] = [nil, I18n.translate(key.to_sym)]
  end

  render json: translations
end

#indexObject



6
7
8
9
10
11
12
13
14
15
# File 'app/controllers/citizen_budget_model/translations_controller.rb', line 6

def index
  @translations = Hash.new{|h,k| h[k] = {}}
  store = I18n.backend.backends.first.store
  store.keys.each do |k|
    locale, key = k.split('.', 2)
    @translations[key][locale.to_sym] = JSON.load(store[k])
  end

  @columns = I18n.available_locales.size + 1
end