Skip to content

Commit

Permalink
Add support for Welsh mutations
Browse files Browse the repository at this point in the history
This commit addresses feedback from the Welsh translation changes in
issue #233. Specifically, we need to apply Welsh consonant mutations in
some cases. The mechanism this commit adds is possibly more than is
needed for the presenting issue, but we are expecting more mutions to be
needed in future.

This change addresses the feedback that some months should be mutated.
E.g:

    O Mai

should become:

    O Fai
  • Loading branch information
ijdickinson committed Sep 18, 2020
1 parent 4ea9fba commit 2824a08
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 9 deletions.
12 changes: 12 additions & 0 deletions app/lib/grammar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

# Entry wrapper for applying generalised grammar rules. Mostly
# this will be used for Welsh grammar actions, such as consonant
# mutations
class Grammar
def self.apply(options)
grammar_action = WelshGrammar.apply(options) if I18n.locale == :cy

grammar_action || GrammarAction.identity_action(options)
end
end
7 changes: 5 additions & 2 deletions app/lib/mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def match?(options)
end

def apply(options)
options[:source]&.sub(pattern, replacement)
GrammarAction.new(
options,
options[:source]&.sub(pattern, replacement)
)
end
end
end
12 changes: 6 additions & 6 deletions app/lib/welsh_grammar.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

Mutation = Struct.new(:pattern, :replacement)

MUTATIONS = {
'o' =>
[
Expand All @@ -17,19 +15,21 @@

# Assistance with formulating correct Welsh grammar
class WelshGrammar
def self.apply(options)
mutate(options)
end

def self.mutate(options)
result = nil

if I18n.locale == 'cy'
if I18n.locale == :cy
result = mutate_prefix(options[:assuming_prefix], options) if options[:assuming_prefix]
end

result || GrammarAction.identity_action(options)
end

private

def mutate_prefix(prefix, options)
def self.mutate_prefix(prefix, options)
return nil unless (mutations = MUTATIONS[prefix])

mutations
Expand Down
10 changes: 9 additions & 1 deletion app/presenters/landing_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ def period
if month
ref_month = to_value(month)
date = Date.strptime(ref_month.value, '%Y-%m')
I18n.l(date, format: '%B %Y')

# Uncomfortable choice here: we know that, in the Welsh translation, the
# month will be preceded by 'O' (i.e. 'From'), which will trigger a mutation
# for some month names. The mutation will only happen in Welsh-lang mode, but
# we have to allow for that case so we apply the Grammar -> GrammarAction
# transform even in English mode (where it's not really needed)
Grammar
.apply(source: I18n.l(date, format: '%B %Y'), assuming_prefix: 'o')
.result
else
I18n.t('landing.not_available')
end
Expand Down
25 changes: 25 additions & 0 deletions test/lib/welsh_grammar_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,40 @@

require 'test_helper'

# Unit tests on Welsh grammar support
class WelshGrammarTest < ActiveSupport::TestCase
describe 'WelshGrammar' do
teardown do
I18n.locale = :en
end

describe 'mutations' do
before do
I18n.locale = :en
end

it 'make no changes if language is not Welsh' do
_(
WelshGrammar
.mutate(source: 'Ionawr', assuming_prefix: 'o')
.result
).must_equal('Ionawr')
end

it 'should mutate months that are prefix by "O" if necessary' do
I18n.locale = :cy

_(
WelshGrammar
.mutate(source: 'Ionawr', assuming_prefix: 'o')
.result
).must_equal('Ionawr')

_(
WelshGrammar
.mutate(source: 'Mai', assuming_prefix: 'o')
.result
).must_equal('Fai')
end
end
end
Expand Down

0 comments on commit 2824a08

Please sign in to comment.