Skip to content

Commit bb44c55

Browse files
iHiDdem4ron
authored andcommitted
Use db-only for HAML
1 parent dfe1c82 commit bb44c55

133 files changed

Lines changed: 165 additions & 35 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Admin::BaseController < ApplicationController
22
before_action :ensure_staff!
3+
before_action :redirect_to_english!
34

45
layout "admin"
56
end

app/controllers/bootcamp/base_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Bootcamp::BaseController < ApplicationController
22
layout "bootcamp"
3+
before_action :redirect_to_english!
34
before_action :redirect_unless_attendee!
45
before_action :setup_bootcamp_data!
56

app/controllers/challenges_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class ChallengesController < ApplicationController
2+
before_action :redirect_to_english!
3+
24
skip_before_action :authenticate_user!, only: %i[show implementation_status]
35
before_action :use_challenge_id!, except: %i[implementation_status track_implementation_status]
46
before_action :use_track!, only: [:track_implementation_status]

app/controllers/concerns/locale_routing.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ def switch_locale!(&action)
3939
locale = specified_locale || default_locale
4040
I18n.with_locale(locale, &action)
4141
end
42+
43+
# Use this as a before)action in places where we don't want a
44+
# locale-specific version of a controller
45+
# e.g. in admin or bootcamp or challenges
46+
def redirect_to_english!
47+
return unless html_request? # Only HTML requests.
48+
return unless request.get? # Only GETs
49+
return unless specified_locale.present?
50+
return if specified_locale == default_locale
51+
52+
response.headers['Cache-Control'] = 'no-store'
53+
redirect_to url_for_locale(:en, request.fullpath), allow_other_host: false, status: :found
54+
end
4255
end

config/initializers/i18n.rb

Lines changed: 123 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,131 @@
1-
class ExercismI18nBackend
2-
include I18n::Backend::Flatten
3-
include I18n::Backend::Base
4-
include I18n::Backend::Memoize
5-
include I18n::Backend::Cache
6-
include I18n::Backend::Fallbacks
7-
8-
def translate(locale, key, options = {})
9-
value = Localization::Translation.lookup(locale, key, options.merge(use_cache: true))
10-
return value if value.present?
11-
12-
throw(:exception, I18n::MissingTranslationData.new(locale, key, options))
13-
end
1+
# frozen_string_literal: true
2+
3+
require 'i18n/backend/base'
4+
require 'i18n/backend/simple'
5+
6+
module I18n
7+
module Backend
8+
class Exercism < Simple
9+
def available_locales = %i[en hu nl de pt pt-BR]
10+
def wip_locales = %i[es]
11+
def default_locale = :en
12+
13+
def load_translations
14+
rows = Localization::Translation.pluck(:locale, :key, :value)
15+
16+
rows.group_by(&:first).each do |locale, group|
17+
merged = {}
1418

15-
# I had this LOC before, but I removed it to hardcode the locales.
16-
# We don't actually want to do this as we have WIP ones.
17-
# Localization::Translation.distinct.pluck(:locale).map(&:to_sym)
18-
def available_locales = %i[en hu nl de pt pt-BR]
19-
def wip_locales = %i[es]
20-
def default_locale = :en
19+
group.each do |(_, key, value)|
20+
keys = key.split(".")
21+
nested = build_nested_hash(keys, value)
22+
Utils.deep_merge!(merged, nested)
23+
end
24+
25+
store_translations(locale.to_sym, merged)
26+
end
27+
end
28+
29+
private
30+
def build_nested_hash(keys, value)
31+
keys.reverse.inject(value) { |acc, key| { key.to_sym => acc } }
32+
end
33+
end
34+
end
2135
end
2236

2337
# Put this in front of the existing backend.
2438
I18n.backend = I18n::Backend::Chain.new(
25-
ExercismI18nBackend.new,
39+
I18n::Backend::Exercism.new,
2640
I18n.backend
2741
)
2842

29-
module I18n
30-
def self.wip_locales = ExercismI18nBackend.new.wip_locales
31-
end
43+
#
44+
#
45+
#
46+
#
47+
# module I18n
48+
# class ExercismBackend
49+
# include Base
50+
#
51+
# # Mutex to ensure that concurrent translations loading will be thread-safe
52+
# MUTEX = Mutex.new
53+
#
54+
# def reload!
55+
# @initialized = false
56+
# @translations = nil
57+
# super
58+
# end
59+
#
60+
# def eager_load!
61+
# init_translations unless initialized?
62+
# super
63+
# end
64+
#
65+
# def translations(do_init: false)
66+
# # To avoid returning empty translations,
67+
# # call `init_translations`
68+
# init_translations if do_init && !initialized?
69+
#
70+
# @translations ||= Concurrent::Hash.new do |h, k|
71+
# MUTEX.synchronize do
72+
# h[k] = Concurrent::Hash.new
73+
# end
74+
# end
75+
# end
76+
#
77+
# def translate(locale, key, options = {})
78+
# value = Localization::Translation.lookup(locale, key, options.merge(use_cache: true))
79+
# return value if value.present?
80+
#
81+
# throw(:exception, I18n::MissingTranslationData.new(locale, key, options))
82+
# end
83+
#
84+
# # I had this LOC before, but I removed it to hardcode the locales.
85+
# # We don't actually want to do this as we have WIP ones.
86+
# # Localization::Translation.distinct.pluck(:locale).map(&:to_sym)
87+
# def available_locales = %i[en hu nl de pt pt-BR]
88+
# def wip_locales = %i[es]
89+
# def default_locale = :en
90+
#
91+
# protected
92+
#
93+
# def init_translations
94+
# load_translations
95+
# @initialized = true
96+
# end
97+
#
98+
# # Looks up a translation from the translations hash. Returns nil if
99+
# # either key is nil, or locale, scope or key do not exist as a key in the
100+
# # nested translations hash. Splits keys or scopes containing dots
101+
# # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
102+
# # <tt>%w(currency format)</tt>.
103+
# def lookup(locale, key, scope = [], options = EMPTY_HASH)
104+
# init_translations unless initialized?
105+
# keys = I18n.normalize_keys(locale, key, scope, options[:separator])
106+
#
107+
# keys.inject(translations) do |result, _key|
108+
# return nil unless result.is_a?(Hash)
109+
# unless result.has_key?(_key)
110+
# _key = _key.to_s.to_sym
111+
# return nil unless result.has_key?(_key)
112+
# end
113+
# result = result[_key]
114+
# result = resolve_entry(locale, _key, result, Utils.except(options.merge(:scope => nil), :count)) if result.is_a?(Symbol)
115+
# result
116+
# end
117+
# end
118+
# end
119+
#
120+
# include Implementation
121+
# end
122+
#
123+
# def self.wip_locales = ExercismI18nBackend.new.wip_locales
124+
# end
125+
#
126+
# Put this in front of the existing backend.
127+
# I18n.backend = I18n::Backend::Chain.new(
128+
# I18n.Backend::Exercism.new,
129+
# I18n.backend
130+
# )
131+
nd
File renamed without changes.

0 commit comments

Comments
 (0)