From 923335b8da9219f20ac84159ffe9a949ddbeed15 Mon Sep 17 00:00:00 2001 From: Matchu Date: Wed, 6 Feb 2013 10:59:25 -0600 Subject: [PATCH] localize blog posts --- app/helpers/application_helper.rb | 5 ++++- public/javascripts/outfits/new.js | 33 ++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 017491ce..da969f84 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -105,7 +105,10 @@ module ApplicationHelper current_locale_is_public = false options = I18n.public_locales.map do |available_locale| current_locale_is_public = true if I18n.locale == available_locale - [translate('locale_name', :locale => available_locale), available_locale] + # Include fallbacks data on the tag. Right now it's used in blog + # localization, but may conceivably be used for something else later. + [translate('locale_name', :locale => available_locale), available_locale, + {'data-fallbacks' => I18n.fallbacks[available_locale].join(',')}] end unless current_locale_is_public diff --git a/public/javascripts/outfits/new.js b/public/javascripts/outfits/new.js index 4a1f29fb..05cef1a8 100644 --- a/public/javascripts/outfits/new.js +++ b/public/javascripts/outfits/new.js @@ -169,11 +169,42 @@ $(function () { body += '…'; }*/ el.find('h2').text(header).wrapInner($('', {href: url})); - el.find('div').html(body); + var contentEl = el.find('div'); + contentEl.html(body); $('', {'id': 'blog-preview-comments', href: url + '#disqus_thread'}).appendTo(el); if(image) { el.find('img').attr('src', image).parent().attr('href', url); } + + // Localize + var localizedBodies = {}; + contentEl.find('.locale').each(function () { + var localizedBody = $(this); + var locale = localizedBody.attr('class').match(/locale-(\S+)/)[1]; + localizedBodies[locale] = localizedBody; + }); + + var fallbacks = $('#locale option:selected').attr('data-fallbacks').split(','); + var bestLocale = null; + for(var i = 0; i < fallbacks.length; i++) { + if(localizedBodies.hasOwnProperty(fallbacks[i])) { + bestLocale = fallbacks[i]; + break; + } + } + + if(bestLocale) { + // I feel bad doing all this in JS rather than CSS, but sometimes you + // gotta do what you gotta do if you wanna support any number of locales. + for(var locale in localizedBodies) { + localizedBodies[locale].hide(); + } + + localizedBodies[bestLocale].show(); + + contentEl.find('.no-locale').hide(); + } + el.fadeIn('medium'); addDisqusCount(); });