5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-15 02:31:42 +00:00

preferir las traducciones del sitio a las de sutty

This commit is contained in:
f 2018-05-08 18:14:00 -03:00
parent 35357c318f
commit 72c4952e7e
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
5 changed files with 57 additions and 20 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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'

View file

@ -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'