Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken tests caused by locale configuration #1875

Merged
merged 5 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ addons:
- nodejs
- wkhtmltopdf

addons:
artifacts:
s3_region: "eu-west-2"

matrix:
fast_finish: true
include:
Expand Down
24 changes: 23 additions & 1 deletion app/models/language.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: languages
Expand All @@ -10,6 +12,7 @@
#

class Language < ActiveRecord::Base

# frozen_string_literal: true

include ValidationValues
Expand All @@ -20,7 +23,7 @@ class Language < ActiveRecord::Base

ABBREVIATION_MAXIMUM_LENGTH = 5

ABBREVIATION_FORMAT = /\A[a-z]{2}(\_[A-Z]{2})?\Z/
ABBREVIATION_FORMAT = /\A[a-z]{2}(\-[A-Z]{2})?\Z/

NAME_MAXIMUM_LENGTH = 20

Expand All @@ -47,6 +50,12 @@ class Language < ActiveRecord::Base

validates :default_language, inclusion: { in: BOOLEAN_VALUES }

# =============
# = Callbacks =
# =============

before_validation :format_abbreviation, if: :abbreviation_changed?

# ==========
# = Scopes =
# ==========
Expand All @@ -58,7 +67,20 @@ class Language < ActiveRecord::Base
where(abbreviation: abbreviation).pluck(:id).first
}

# ========================
# = Public class methods =
# ========================

def self.many?
Rails.cache.fetch([model_name, "many?"], expires_in: 1.hour) { all.many? }
end

private

def format_abbreviation
abbreviation.downcase!
return if abbreviation.blank? || abbreviation =~ /\A[a-z]{2}\Z/i
self.abbreviation = LocaleFormatter.new(abbreviation, format: :i18n).to_s
end

end
2 changes: 1 addition & 1 deletion app/views/org_admin/phases/_phase.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= phase.title %>
<div class="pull-right">
<% if template.customization_of.present? && template.latest? %>
<%= link_to _('Customize phase'), org_admin_template_phase_path(template.id, phase.id), { class: "btn btn-default", role: 'button' } %>
<%= link_to _('Customise phase'), org_admin_template_phase_path(template.id, phase.id), { class: "btn btn-default", role: 'button' } %>
<% elsif modifiable %>
<%= link_to _('Edit phase'), edit_org_admin_template_phase_path(template.id, phase.id), { class: "btn btn-default", role: 'button' } %>
<% else %>
Expand Down
6 changes: 3 additions & 3 deletions app/views/org_admin/templates/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
<%= _('Original funder template has changed!') %>
<% else %>
<% if template.published? %>
<%= template.customization_of.present? ? _('Customizations are published') : _('Published') %>
<%= template.customization_of.present? ? _('Customisations are published') : _('Published') %>
<% elsif template.draft? %>
<% tooltip = _('You have unpublished changes! Select "Publish changes" in the Actions menu when you are ready to make them available to users.') %>
<%= template.customization_of.present? ? _('Customizations are published') :_('Published')%>
<%= template.customization_of.present? ? _('Customisations are published') :_('Published')%>
<em class="sr-only"><%= tooltip %></em>
&nbsp;&nbsp;<i class="fa fa-pencil-square-o red" aria-hidden="true" data-toggle="tooltip" title="<%= tooltip %>"></i>
<% else %>
<%= template.customization_of.present? ? _('Customizations are unpublished') :_('Unpublished') %>
<%= template.customization_of.present? ? _('Customisations are unpublished') :_('Unpublished') %>
<% end %>
<% end %>
</dd>
Expand Down
2 changes: 1 addition & 1 deletion app/views/org_admin/templates/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<% if !current_user.org.funder_only? %>
<li role="presentation"
class="<%= action_name == 'customisable' ? 'active' : '' %>">
<%= link_to(_('Customizable Templates'),
<%= link_to(_('Customisable Templates'),
customisable_org_admin_templates_path, { role: 'tab' }) %>
</li>
<% end %>
Expand Down
2 changes: 0 additions & 2 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Rails.application.configure do

config.i18n.available_locales = %w[en en_GB]

# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
Expand Down
4 changes: 0 additions & 4 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
# Disable memory store cache set in application.rb
config.cache_store = :null_store

# This is required for the Faker gem. See this issue here:
# https://github.com/stympy/faker/issues/266
config.i18n.available_locales = %w[en en_GB]

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
Expand Down
18 changes: 12 additions & 6 deletions config/initializers/fast_gettext.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# When Travis runs this, the DB isn't always built yet.
if Language.table_exists?
def default_locale
Language.default.try(:abbreviation) || 'en_GB'
Language.default.try(:abbreviation) || 'en-GB'
end

def available_locales
Language.sorted_by_abbreviation.pluck(:abbreviation).presence || [default_locale]
LocaleSet.new(
Language.sorted_by_abbreviation.pluck(:abbreviation).presence || [default_locale]
)
end
else
def default_locale
Rails.application.config.i18n.available_locales.first
Rails.application.config.i18n.available_locales.first || 'en-GB'
end

def available_locales
Rails.application.config.i18n.available_locales
Rails.application.config.i18n.available_locales = LocaleSet.new(['en-GB', 'en'])
end
end

Expand All @@ -24,6 +26,10 @@ def available_locales
report_warning: false,
})

I18n.available_locales += available_locales.for(:i18n)
FastGettext.default_available_locales = available_locales.for(:fast_gettext)

FastGettext.default_text_domain = 'app'
FastGettext.default_locale = default_locale
FastGettext.default_available_locales = available_locales

I18n.default_locale = available_locales.for(:i18n).first
FastGettext.default_locale = available_locales.for(:fast_gettext).first
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a clean way of managing the fastGettext vs I18n expectations.

31 changes: 7 additions & 24 deletions config/initializers/locale.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
# This initilializer should not be removed unless all internationalisation is handled by gettext_rails
DMPRoadmap::Application.config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}').to_s]
module I18n
class Config
def locale
FastGettext.locale
end
def locale=(new_locale)
FastGettext.locale = (new_locale)
end
def default_locale
FastGettext.default_locale
end
def default_locale=(new_default_locale)
FastGettext.default_locale = (new_default_locale)
end
def available_locales
FastGettext.default_available_locales
end
def available_locales=(new_available_locales)
FastGettext.default_available_locales = (new_available_locales)
end
end
end
# frozen_string_literal: true

# This initilializer should not be removed unless all internationalisation is handled by
# gettext_rails
DMPRoadmap::Application.config.i18n.load_path += Dir[
Rails.root.join('config', 'locales', '**', '*.yml').to_s
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en_GB:
en-GB:
helpers:
actions: "Actions"
links:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en_US:
en-US:
helpers:
actions: "Actions"
links:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

fr_FR:
fr-FR:
helpers:
actions: "Actions"
links:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

pt_BR:
pt-BR:
helpers:
actions: "Ações"
links:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en_GB:
en-GB:
contact_us:
contact_mailer:
contact_email:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en_US:
en-US:
contact_us:
contact_mailer:
contact_email:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See http://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

fr_FR:
fr-FR:
contact_us:
contact_mailer:
contact_email:
Expand Down
25 changes: 0 additions & 25 deletions config/locales/contact_us/contact_us.pt_BR.yml

This file was deleted.

2 changes: 1 addition & 1 deletion config/locales/contact_us/contact_us.zh-TW.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
zh:
zh-TW:
contact_us:
contact_mailer:
contact_email:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n

en_GB:
en-GB:
devise:
confirmations:
confirmed: "Your account was successfully confirmed. Please sign in."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n

en_US:
en-US:
devise:
confirmations:
confirmed: "Your account was successfully confirmed. Please sign in."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n

fr_FR:
fr-FR:
devise:
confirmations:
confirmed: "Votre compte a bien été validé. Veuillez vous connecter."
send_instructions: "Vous recevrez dans quelques minutes un courriel contenant les directives pour activer votre compte."
resend_instructions: "Envoyer de nouveau les directives"
resend_instructions: "Envoyer de nouveau les directives"
send_paranoid_instructions: "Si votre adresse électronique existe dans notre base de données, vous recevrez dans quelques minutes un courriel contenant les directives pour valider votre compte."
failure:
already_authenticated: "Vous êtes déjà connecté(e)."
Expand Down Expand Up @@ -63,7 +63,7 @@ fr_FR:
unlocks:
send_instructions: "Vous recevrez dans quelques minutes un courriel contenant les directives pour déverrouiller votre compte."
send_paranoid_instructions: "Si votre compte existe, vous recevrez dans quelques minutes un courriel contenant les directives pour le déverrouiller."
resend_unlock_instructions: "Envoyer de nouveau les directives de déverouillage."
resend_unlock_instructions: "Envoyer de nouveau les directives de déverouillage."
unlocked: "Votre compte a bien été déverrouillé. Veuillez vous connecter pour continuer."
errors:
messages:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Additional translations at https://github.com/plataformatec/devise/wiki/I18n

pt_BR:
pt-BR:
devise:
confirmations:
confirmed: "Sua conta foi confirmada com sucesso. Por favor, inscreva-se."
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
en_GB:
en-GB:
devise:
invitations:
send_instructions: 'An invitation email has been sent to %{email}.'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
en_US:
en-US:
devise:
invitations:
send_instructions: 'An invitation email has been sent to %{email}.'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fr_FR:
fr-FR:
devise:
invitations:
send_instructions: "Un courriel d'invitation a été envoyé à %{email}."
Expand Down
4 changes: 2 additions & 2 deletions config/locales/en_GB.yml → config/locales/en-GB.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
en_GB:
en-GB:
activerecord:
errors:
messages:
Expand Down Expand Up @@ -95,7 +95,7 @@ en_GB:
other: "%{count} months"
x_years:
one: 1 year
other: "%{count} years"
other: "%{count} years"
x_seconds:
one: 1 second
other: "%{count} seconds"
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en_US.yml → config/locales/en-US.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
en_US:
en-US:
activerecord:
errors:
messages:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
en_GB:
en-GB:
faker:
language:
names:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/fr_CA.yml → config/locales/fr-CA.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
fr_CA:
fr-CA:
activerecord:
errors:
messages:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/fr_FR.yml → config/locales/fr-FR.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
fr_FR:
fr-FR:
activerecord:
errors:
messages:
Expand Down
Loading