From 72c4952e7ec994fa6f7a68ef8e980c3e8fcfa056 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 8 May 2018 18:14:00 -0300 Subject: [PATCH] preferir las traducciones del sitio a las de sutty --- app/controllers/application_controller.rb | 13 +++++++--- app/controllers/posts_controller.rb | 12 ++++----- app/models/site.rb | 31 ++++++++++++++++++++++- app/views/posts/_form.haml | 19 +++++++------- app/views/posts/index.haml | 2 +- 5 files changed, 57 insertions(+), 20 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 85e969cb..0aca486c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -26,7 +26,7 @@ class ApplicationController < ActionController::Base def find_post(site) id = params[:post_id] || params[:id] - lang = find_lang + lang = find_lang(site) posts = site.posts_for(lang) posts.find do |p| @@ -34,8 +34,15 @@ class ApplicationController < ActionController::Base end end - def find_lang - params.fetch(:lang, I18n.locale.to_s) + def find_lang(site) + params.fetch(:lang, site.default_lang) + end + + def find_template(site) + id = params[:template_id] || params[:id] + site.templates.find do |t| + t.id == id + end end def set_locale diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 72b3967d..610e6565 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,8 +2,8 @@ class PostsController < ApplicationController before_action :authenticate! def index - @lang = find_lang @site = find_site + @lang = find_lang(@site) @category = session[:category] = params.dig(:category) @posts = @site.posts_for(@lang) @@ -15,20 +15,20 @@ class PostsController < ApplicationController end def show - @lang = find_lang @site = find_site + @lang = find_lang(@site) @post = find_post(@site) end def new - @lang = find_lang @site = find_site + @lang = find_lang(@site) @post = Post.new(site: @site, front_matter: { date: Time.now }, lang: @lang) end def create - @lang = find_lang @site = find_site + @lang = find_lang(@site) @post = Post.new(site: @site, front_matter: post_params.to_hash, lang: @lang) if @post.save @@ -39,15 +39,15 @@ class PostsController < ApplicationController end def edit - @lang = find_lang @site = find_site + @lang = find_lang(@site) @post = find_post(@site) end def update p = post_params - @lang = find_lang @site = find_site + @lang = find_lang(@site) @post = find_post(@site) @post.update_attributes(p) diff --git a/app/models/site.rb b/app/models/site.rb index 612bf9f0..71a82598 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -13,7 +13,36 @@ class Site # Determina si el sitio está en varios idiomas def i18n? - @jekyll.config.dig('i18n').present? + !translations.empty? + end + + # Obtiene la lista de traducciones actuales + def translations + @jekyll.config.dig('i18n') || [] + end + + # Devuelve el idioma por defecto del sitio + # + # TODO: volver elegante + def default_lang + # Si está traducido, intentamos saber si podemos trabajar en el + # idioma actual de la plataforma. + if i18n? + i18n = I18n.locale.to_s + if translations.include? i18n + # Podemos trabajar en el idioma actual + i18n + else + # Sino, trabajamos con el primer idioma + translations.first + end + else + # Si el sitio no está traducido, estamos trabajando con posts en + # cualquier idioma + # + # XXX: no será un dirty hack? + 'posts' + end end # Obtener el nombre del sitio diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index e5264a2b..543b05de 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -115,14 +115,15 @@ options_for_select(@site.everything_of(:materiales_requeridos, lang: @lang), @post.get_front_matter(:materiales_requeridos)), { class: 'form-control select2', multiple: 'multiple' } %small.text-muted.form-text= t('posts.materiales_requeridos_help') - - I18n.available_locales.map(&:to_s).each do |lang| - - next if lang == @lang - .form-group - = label_tag 'post_lang', t("posts.lang.#{lang}") - = select_tag "post[lang][#{lang}]", - options_for_select(@site.posts_for(lang).map { |p| [p.title, p.id] }, - @post.get_front_matter(:lang).try(:dig, lang)), - { class: 'form-control select2' } - %small.text-muted.form-text= t('posts.lang_help') + - if @site.i18n? + - @site.translations.each do |lang| + - next if lang == @lang + .form-group + = label_tag 'post_lang', t("posts.lang.#{lang}") + = select_tag "post[lang][#{lang}]", + options_for_select(@site.posts_for(lang).map { |p| [p.title, p.id] }, + @post.get_front_matter(:lang).try(:dig, lang)), + { class: 'form-control select2' } + %small.text-muted.form-text= t('posts.lang_help') .form-group = submit_tag t('posts.save'), class: 'btn btn-success' diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index 84e32a8d..6c99649e 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -12,7 +12,7 @@ .btn-group = link_to t('posts.new'), new_site_post_path(@site, lang: @lang), class: 'btn btn-success' - - I18n.available_locales.map(&:to_s).each do |l| + - @site.translations.each do |l| = link_to t("i18n.#{l}"), site_posts_path(@site, category: @category, lang: l), class: 'btn btn-info'