From 763320cd5e6b4e28b17620847b21e6266812db26 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 19 Feb 2018 17:00:21 -0300 Subject: [PATCH] intercambiar idiomas --- app/controllers/i18n_controller.rb | 13 ++++++++----- app/views/i18n/_form.haml | 14 +++++++++++++- app/views/i18n/_text_field.haml | 4 +++- app/views/sites/index.haml | 1 + config/locales/en.yml | 5 ++++- config/locales/es.yml | 5 ++++- 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/controllers/i18n_controller.rb b/app/controllers/i18n_controller.rb index 226e7804..b7298ea3 100644 --- a/app/controllers/i18n_controller.rb +++ b/app/controllers/i18n_controller.rb @@ -9,19 +9,22 @@ class I18nController < ApplicationController def edit @site = find_site - @lang_from = I18n.locale.to_s - @lang_to = @site.config['i18n'].reject { |i| i == @lang_from }.sample - @lang_to = 'ar' + @lang_from = params.fetch(:from, I18n.locale.to_s) + @lang_to = params.fetch(:to, I18n.available_locales.sample.to_s) + @options = I18n.available_locales.map do |lang| + [ t("i18n.#{lang.to_s}"), lang.to_s ] + end end def update @site = find_site + @lang_to = params.require(:i18n).require(:lang_to) # No usamos params porque nos obliga a hacer una lista blanca de # todos los parámetros que queremos, pero no tenemos forma aun de # pasarse a permit un array de todas las keys y sus tipos en base al # idioma que ya existe - p = request.parameters[:i18n][:ar] - i = JekyllI18n.new(site: @site, lang: :ar, attributes: p) + p = request.parameters[:i18n][@lang_to] + i = JekyllI18n.new(site: @site, lang: @lang_to, attributes: p) if i.save redirect_to site_path(@site) diff --git a/app/views/i18n/_form.haml b/app/views/i18n/_form.haml index 795c605d..5f2c630d 100644 --- a/app/views/i18n/_form.haml +++ b/app/views/i18n/_form.haml @@ -1,5 +1,17 @@ -%h1= t('i18n.translating', from: t("i18n.#{@lang_from}"), to: t("i18n.#{@lang_to}")) += form_tag site_i18n_edit_path(@site), method: :get, class: 'form-inline' do + %h1 + = t('i18n.translating.from') + = select_tag 'from', + options_for_select(@options, @lang_from), + class: 'form-control' + = t('i18n.translating.to') + = select_tag 'to', + options_for_select(@options, @lang_to), + class: 'form-control' + = submit_tag t('i18n.change'), class: 'btn btn-info', name: nil + = form_tag site_i18n_path(@site), method: :post, class: 'form' do + = hidden_field 'i18n', 'lang_to', value: @lang_to .form-group = submit_tag t('i18n.save'), class: 'btn btn-success' diff --git a/app/views/i18n/_text_field.haml b/app/views/i18n/_text_field.haml index 4869a39d..cc0a26bf 100644 --- a/app/views/i18n/_text_field.haml +++ b/app/views/i18n/_text_field.haml @@ -22,7 +22,9 @@ -# no especificar el id en una key numerica para que no se genere un -# hash en lugar de un array de valores - key = '' if key.is_a? Integer - = label_tag "i18n[#{@lang_to}]#{form_keys}[#{key}]", value + - value_to = value if value_to.blank? + = label_tag "i18n[#{@lang_to}]#{form_keys}[#{key}]", value, + class: (@lang_from == 'ar') ? 'rtl' : 'ltr' -# creamos un campo a mano porque los helpers de niveles mas altos -# quieren hacer magia con los ids y fallan - if value.length > 140 diff --git a/app/views/sites/index.haml b/app/views/sites/index.haml index 6e175b77..15f59d7e 100644 --- a/app/views/sites/index.haml +++ b/app/views/sites/index.haml @@ -11,3 +11,4 @@ - @sites.each do |site| %tr %td= link_to site.name, site_path(site) + %td= link_to t('i18n.edit'), site_i18n_edit_path(site) diff --git a/config/locales/en.yml b/config/locales/en.yml index ccd7b37a..e930453d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -15,7 +15,10 @@ en: index: 'Translations' edit: 'Edit translations' save: 'Save' - translating: 'Translating into %{to} from %{from}' + change: 'Change' + translating: + from: 'Translating from' + to: 'into' es: 'Castillian Spanish' en: 'English' ar: 'Arabic' diff --git a/config/locales/es.yml b/config/locales/es.yml index 0f97c524..e3368be8 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -15,7 +15,10 @@ es: index: 'Traducciones' edit: 'Editar traducciones' save: 'Guardar' - translating: 'Traduciendo al %{to} desde %{from}' + change: 'Cambiar' + translating: + from: 'Traduciendo desde' + to: 'a' es: 'castellano' en: 'inglés' ar: 'árabe'