fix bugs in the translator service + use the template of the default locale if blank template
did
committed Feb 16, 2015
commit 88324b29d311eeae52525deb6e0ce6b3feadfd82
Showing 12
changed files with
66 additions
and 29 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb
+2
-0
| @@ | @@ -18,6 +18,8 @@ require_relative 'steam/services' |
| module Locomotive | |
| module Steam | |
| + | FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo |
| + | |
| class << self | |
| attr_writer :configuration | |
| end | |
locomotive/steam/decorators/template_decorator.rb b/lib/locomotive/steam/decorators/template_decorator.rb
+1
-1
| @@ | @@ -17,7 +17,7 @@ module Locomotive |
| def source_from_template_file | |
| source = File.open(template_path).read.force_encoding('utf-8') | |
| - | if match = source.match(/^((---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/m) |
| + | if match = source.match(FRONTMATTER_REGEXP) |
| source = match[:template] | |
| end | |
locomotive/steam/repositories/filesystem/models/translation.rb b/lib/locomotive/steam/repositories/filesystem/models/translation.rb
+5
-0
| @@ | @@ -5,6 +5,11 @@ module Locomotive |
| module Models | |
| class Translation < Base | |
| + | |
| + | def values |
| + | self[:values] |
| + | end |
| + | |
| end | |
| end | |
locomotive/steam/repositories/filesystem/sanitizers/page.rb b/lib/locomotive/steam/repositories/filesystem/sanitizers/page.rb
+12
-0
| @@ | @@ -19,10 +19,22 @@ module Locomotive |
| set_fullpath_for(page, locale) | |
| modify_if_templatized(page, locale) | |
| build_editable_elements(page, locale) | |
| + | use_default_locale_template_path(page, locale) |
| end | |
| end | |
| end | |
| + | # If the page does not have a template in a locale |
| + | # then use the template of the default locale |
| + | # |
| + | def use_default_locale_template_path(page, locale) |
| + | paths = page.template_path |
| + | |
| + | if paths[locale] == false |
| + | paths[locale] = paths[default_locale] |
| + | end |
| + | end |
| + | |
| def build_editable_elements(page, locale) | |
| elements = page.editable_elements[locale] || {} | |
| elements.stringify_keys! | |
locomotive/steam/repositories/filesystem/yaml_loaders/concerns/common.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/concerns/common.rb
+8
-6
| @@ | @@ -7,16 +7,18 @@ module Locomotive |
| module Common | |
| - | def load(path, frontmatter = false) |
| + | def load(path, frontmatter = false, &block) |
| if File.exists?(path) | |
| - | yaml = File.open(path).read.force_encoding('utf-8') |
| + | yaml = File.open(path).read.force_encoding('utf-8') |
| + | template = nil |
| - | if frontmatter |
| - | yaml =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m |
| - | yaml = $1 |
| + | if frontmatter && match = yaml.match(FRONTMATTER_REGEXP) |
| + | yaml, template = match[:yaml], match[:template] |
| end | |
| - | HashConverter.to_sym(YAML.load(yaml)) |
| + | HashConverter.to_sym(YAML.load(yaml)).tap do |attributes| |
| + | block.call(attributes, template) if block_given? |
| + | end |
| else | |
| Locomotive::Common::Logger.error "No #{path} file found" | |
| {} | |
locomotive/steam/repositories/filesystem/yaml_loaders/page.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/page.rb
+17
-6
| @@ | @@ -41,7 +41,7 @@ module Locomotive |
| title: { locale => attributes.delete(:title) || (default_locale == locale ? slug.humanize : nil) }, | |
| slug: { locale => attributes.delete(:slug) || slug }, | |
| editable_elements: { locale => attributes.delete(:editable_elements) }, | |
| - | template_path: { locale => filepath }, |
| + | template_path: { locale => template_path(filepath, attributes, locale) }, |
| _fullpath: fullpath | |
| }.merge(attributes) | |
| end | |
| @@ | @@ -50,18 +50,21 @@ module Locomotive |
| slug = fullpath.split('/').last | |
| attributes = get_attributes(filepath, fullpath) | |
| - | leaf[:title][locale] = attributes.delete(:title) || slug.humanize |
| - | leaf[:slug][locale] = attributes.delete(:slug) || slug |
| - | leaf[:editable_elements][locale] = attributes.delete(:editable_elements) |
| - | leaf[:template_path][locale] = filepath |
| + | leaf[:title][locale] = attributes.delete(:title) || slug.humanize |
| + | leaf[:slug][locale] = attributes.delete(:slug) || slug |
| + | leaf[:editable_elements][locale] = attributes.delete(:editable_elements) |
| + | leaf[:template_path][locale] = template_path(filepath, attributes, locale) |
| leaf.merge!(attributes) | |
| end | |
| def get_attributes(filepath, fullpath) | |
| - | load(filepath, true).tap do |attributes| |
| + | load(filepath, true) do |attributes, template| |
| # make sure index/404 are the slugs of the index/404 pages | |
| attributes.delete(:slug) if %w(index 404).include?(fullpath) | |
| + | |
| + | # trick to use the template of the default locale (if available) |
| + | attributes[:template_path] = false if template.blank? |
| end | |
| end | |
| @@ | @@ -82,6 +85,14 @@ module Locomotive |
| filepath =~ /\.(#{template_extensions.join('|')})$/ | |
| end | |
| + | def template_path(filepath, attributes, locale) |
| + | if attributes.delete(:template_path) == false && locale != default_locale |
| + | false |
| + | else |
| + | filepath |
| + | end |
| + | end |
| + | |
| def get_relative_path(filepath) | |
| filepath.gsub(path, '').gsub(/^\//, '') | |
| end | |
locomotive/steam/repositories/filesystem/yaml_loaders/translation.rb b/lib/locomotive/steam/repositories/filesystem/yaml_loaders/translation.rb
+1
-1
| @@ | @@ -17,7 +17,7 @@ module Locomotive |
| def load_array | |
| [].tap do |array| | |
| load(path).each do |key, values| | |
| - | array << { key: key.to_s, values: values } |
| + | array << { key: key.to_s, values: HashConverter.to_string(values) } |
| end | |
| end | |
| end | |
locomotive/steam/services/translator.rb b/lib/locomotive/steam/services/translator.rb
+9
-2
| @@ | @@ -2,7 +2,7 @@ module Locomotive |
| module Steam | |
| module Services | |
| - | class Translator < Struct.new(:repository, :default_locale) |
| + | class Translator < Struct.new(:repository, :current_locale) |
| # Return the translation described by a key. | |
| # | |
| @@ | @@ -13,10 +13,17 @@ module Locomotive |
| # @return [ String ] the translated text or nil if not found | |
| # | |
| def translate(input, locale, scope = nil) | |
| + | locale ||= current_locale |
| + | |
| if scope.blank? | |
| values = repository.find(input).try(:values) || {} | |
| - | values[locale.to_s] || values[default_locale.to_s] |
| + | if translation = values[locale.to_s] |
| + | translation |
| + | else |
| + | Locomotive::Common::Logger.warn "Missing translation '#{input}' for the '#{locale}' locale".yellow |
| + | input |
| + | end |
| else | |
| I18n.t(input, scope: scope.split('.'), locale: locale) | |
| end | |
spec/integration/server/basic_spec.rb
+6
-8
| @@ | @@ -61,14 +61,12 @@ describe Locomotive::Steam::Server do |
| # last_response.body.should =~ /Leader: Eddie/ | |
| # end | |
| - | # it 'translates strings', pending: true do |
| - | # get '/en' |
| - | # last_response.body.should =~ /Powered by/ |
| - | # get '/fr' |
| - | # last_response.body.should =~ /Propulsé par/ |
| - | # get '/nb' |
| - | # last_response.body.should_not =~ /Powered by/ |
| - | # end |
| + | it 'translates strings' do |
| + | get '/en' |
| + | expect(last_response.body).to include 'Powered by' |
| + | get '/fr' |
| + | expect(last_response.body).to include 'Propulsé par' |
| + | end |
| # it 'provides translation in scopes', pending: true do | |
| # get '/' | |
spec/unit/repositories/filesystem/translation_spec.rb
+2
-2
| @@ | @@ -2,7 +2,7 @@ require 'spec_helper' |
| describe Locomotive::Steam::Repositories::Filesystem::Translation do | |
| - | let(:loader) { instance_double('Loader', list_of_attributes: [{ key: 'powered_by', values: { en: 'Powered by Steam', fr: 'Propulsé par Steam' } }]) } |
| + | let(:loader) { instance_double('Loader', list_of_attributes: [{ key: 'powered_by', values: { 'en' => 'Powered by Steam', 'fr' => 'Propulsé par Steam' } }]) } |
| let(:locale) { :en } | |
| let(:repository) { Locomotive::Steam::Repositories::Filesystem::Translation.new(loader) } | |
| @@ | @@ -26,7 +26,7 @@ describe Locomotive::Steam::Repositories::Filesystem::Translation do |
| context 'existing translation' do | |
| let(:key) { 'powered_by' } | |
| - | it { expect(subject.values).to eq({ en: 'Powered by Steam', fr: 'Propulsé par Steam' }) } |
| + | it { expect(subject.values).to eq({ 'en' => 'Powered by Steam', 'fr' => 'Propulsé par Steam' }) } |
| end | |
spec/unit/repositories/filesystem/yaml_loaders/translation_spec.rb
+1
-1
| @@ | @@ -13,7 +13,7 @@ describe Locomotive::Steam::Repositories::Filesystem::YAMLLoaders::Translation d |
| it 'tests various stuff' do | |
| expect(subject.size).to eq 1 | |
| expect(subject.first[:key]).to eq('powered_by') | |
| - | expect(subject.first[:values]).to eq({ en: 'Powered by', fr: 'Propulsé par' }) |
| + | expect(subject.first[:values]).to eq({ 'en' => 'Powered by', 'fr' => 'Propulsé par' }) |
| end | |
| end | |
spec/unit/services/translator_spec.rb
+2
-2
| @@ | @@ -27,7 +27,7 @@ describe Locomotive::Steam::Services::Translator do |
| context 'no translation found' do | |
| let(:translation) { nil } | |
| - | it { is_expected.to eq nil } |
| + | it { is_expected.to eq 'example_test' } |
| end | |
| @@ | @@ -43,7 +43,7 @@ describe Locomotive::Steam::Services::Translator do |
| let(:locale) { 'nl' } | |
| it 'reverts to default locale' do | |
| - | is_expected.to eq "Example text" |
| + | is_expected.to eq "example_test" |
| end | |
| end | |