diff --git a/app/controllers/i18n_controller.rb b/app/controllers/i18n_controller.rb new file mode 100644 index 0000000..1fa65d1 --- /dev/null +++ b/app/controllers/i18n_controller.rb @@ -0,0 +1,21 @@ +class I18nController < ApplicationController + before_action :authenticate! + + def index + @site = find_site + + redirect_to site_i18n_edit_path(@site) + end + + def edit + @site = find_site + @lang_from = I18n.locale.to_s + @lang_to = @site.config['i18n'].reject { |i| i == @lang_from }.sample + @lang_to = 'es' + end + + def update + @site = find_site + binding.pry + end +end diff --git a/app/models/site.rb b/app/models/site.rb index 488bae9..3eace1b 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -22,6 +22,25 @@ class Site end alias :to_s :id + def data + if @jekyll.data.empty? + @jekyll.read + Rails.logger.info 'Leyendo data' + end + + @jekyll.data + end + + def config + if @jekyll.config.empty? + @jekyll.read + Rails.logger.info 'Leyendo config' + end + + @jekyll.config + + end + # Los posts de este sitio def posts return @posts if @posts diff --git a/app/views/i18n/_form.haml b/app/views/i18n/_form.haml new file mode 100644 index 0000000..795c605 --- /dev/null +++ b/app/views/i18n/_form.haml @@ -0,0 +1,6 @@ +%h1= t('i18n.translating', from: t("i18n.#{@lang_from}"), to: t("i18n.#{@lang_to}")) += form_tag site_i18n_path(@site), method: :post, class: 'form' do + .form-group + = submit_tag t('i18n.save'), class: 'btn btn-success' + + = render 'i18n/recursive', data: @site.data.dig(@lang_from), superkeys: [] diff --git a/app/views/i18n/_recursive.haml b/app/views/i18n/_recursive.haml new file mode 100644 index 0000000..410760d --- /dev/null +++ b/app/views/i18n/_recursive.haml @@ -0,0 +1,8 @@ +- data.each_pair do |key, value| + - keys = [superkeys, key].flatten + - if value.is_a? String + = render 'i18n/text_field', keys: keys, value: value + - elsif value.is_a? Hash + = render 'i18n/recursive', data: value, superkeys: keys + -# elsif value.is_a? Array + = render 'i18n/recursive_array', data: value diff --git a/app/views/i18n/_recursive_array.haml b/app/views/i18n/_recursive_array.haml new file mode 100644 index 0000000..6f0d141 --- /dev/null +++ b/app/views/i18n/_recursive_array.haml @@ -0,0 +1,7 @@ +- data.each do |value| + - if value.is_a? String + = render 'i18n/text_field', value: value, key: "#{key}[]" + - elsif value.is_a? Hash + = render 'i18n/recursive', data: value + - elsif value.is_a? Array + = render 'i18n/recursive_array', data: value diff --git a/app/views/i18n/_text_field.haml b/app/views/i18n/_text_field.haml new file mode 100644 index 0000000..fb0c89c --- /dev/null +++ b/app/views/i18n/_text_field.haml @@ -0,0 +1,15 @@ +.form-group + - key = keys.pop + - form_keys = keys.map { |k| "[#{k.to_s}]" }.join('') + - form_help = (keys.size > 0) ? [keys,key].flatten.join('.') : key + - value = @site.data[@lang_to] + - [keys,key].flatten.each do |k| + - value = value[k] + = label_tag "i18n[#{@lang_to}]#{form_keys}", value + - if value.length > 140 + = text_area "i18n[#{@lang_to}]#{form_keys}", key, value: value, + class: 'form-control' + - else + = text_field "i18n[#{@lang_to}]#{form_keys}", key, value: value, + class: 'form-control' + %small.text-muted.form-text= form_help diff --git a/app/views/i18n/edit.haml b/app/views/i18n/edit.haml new file mode 100644 index 0000000..66655c7 --- /dev/null +++ b/app/views/i18n/edit.haml @@ -0,0 +1,11 @@ +.row + .col + = render 'layouts/breadcrumb', + crumbs: [ link_to(t('sites.index'), sites_path), + link_to(@site.name, site_path(@site)), + t('i18n.index'), + t('i18n.edit')] + +.row + .col + = render 'i18n/form' diff --git a/config/locales/en.yml b/config/locales/en.yml index 816afdf..d5687f6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -10,6 +10,14 @@ en: index: 'Sites' footer: powered_by: 'is developed by' + i18n: + index: 'Translations' + edit: 'Edit translations' + save: 'Save' + translating: 'Translating into %{to} from %{from}' + es: 'Castillian Spanish' + en: 'English' + ar: 'Arabic' posts: index: 'Posts' edit: 'Edit' diff --git a/config/locales/es.yml b/config/locales/es.yml index 6b9b9f8..54d583e 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -10,6 +10,14 @@ es: index: 'Sitios' footer: powered_by: 'es desarrollada por' + i18n: + index: 'Traducciones' + edit: 'Editar traducciones' + save: 'Guardar' + translating: 'Traduciendo al %{to} desde %{from}' + es: 'castellano' + en: 'inglés' + ar: 'árabe' posts: index: 'Artículos' edit: 'Editar' diff --git a/config/routes.rb b/config/routes.rb index a16d6e7..c6defd0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,5 +6,9 @@ Rails.application.routes.draw do resources :sites do resources :posts + + get 'i18n', to: 'i18n#index' + get 'i18n/edit', to: 'i18n#edit' + post 'i18n', to: 'i18n#update' end end