Class: Utopia::Localization

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/localization.rb

Overview

If you request a URL which has localized content, a localized redirect would be returned based on the content requested.

Defined Under Namespace

Classes: RequestWrapper

Constant Summary collapse

RESOURCE_NOT_FOUND =
[400, {}, []].freeze
HTTP_ACCEPT_LANGUAGE =
'HTTP_ACCEPT_LANGUAGE'.freeze
LOCALIZATION_KEY =
'utopia.localization'.freeze
CURRENT_LOCALE_KEY =
'utopia.localization.current_locale'.freeze
DEFAULT_LOCALE =
'en'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, **options) ⇒ Localization

Returns a new instance of Localization.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/utopia/localization.rb', line 68

def initialize(app, **options)
  @app = app
  
  @all_locales = options[:locales]
  
  # Locales here are represented as an array of strings, e.g. ['en', 'ja', 'cn', 'de'].
  unless @default_locales = options[:default_locales] 
    @default_locales = @all_locales + [nil]
  end
  
  if @default_locale = options[:default_locale]
    @default_locales.unshift(default_locale)
  else
    @default_locale = @default_locales.first
  end
  
  @hosts = options[:hosts] || {}
  
  @nonlocalized = options.fetch(:nonlocalized, [])
  
  # puts "All:#{@all_locales.inspect} defaults:#{@default_locales.inspect} default:#{default_locale}"
  
  self.freeze
end

Instance Attribute Details

#all_localesObject (readonly)

Returns the value of attribute all_locales.



103
104
105
# File 'lib/utopia/localization.rb', line 103

def all_locales
  @all_locales
end

#default_localeObject (readonly)

Returns the value of attribute default_locale.



104
105
106
# File 'lib/utopia/localization.rb', line 104

def default_locale
  @default_locale
end

Class Method Details

.[](request) ⇒ Object



56
57
58
# File 'lib/utopia/localization.rb', line 56

def self.[] request
  RequestWrapper.new(request)
end

Instance Method Details

#browser_preferred_locales(env) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/utopia/localization.rb', line 157

def browser_preferred_locales(env)
  accept_languages = env[HTTP_ACCEPT_LANGUAGE]
  
  # No user prefered languages:
  return [] unless accept_languages
  
  languages = accept_languages.split(',').map { |language|
    language.split(';q=').tap{|x| x[1] = (x[1] || 1.0).to_f}
  }.sort{|a, b| b[1] <=> a[1]}.collect(&:first)
  
  # Returns available languages based on the order of the first argument:
  return languages & @all_locales
end

#call(env) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/utopia/localization.rb', line 197

def call(env)
  # Pass the request through with no localization if it is a nonlocalized path:
  return @app.call(env) if nonlocalized?(env)
  
  env[LOCALIZATION_KEY] = self
  
  response = nil
  
  # We have a non-localized request, but there might be a localized resource. We return the best localization possible:
  preferred_locales(env) do |env|
    # puts "Trying locale: #{env[CURRENT_LOCALE_KEY]}: #{env[Rack::PATH_INFO]}..."
    
    response = @app.call(env)
    
    break unless response[0] >= 400
  end
  
  return vary(env, response)
end

#freezeObject



93
94
95
96
97
98
99
100
101
# File 'lib/utopia/localization.rb', line 93

def freeze
  @all_locale.freeze
  @default_locales.freeze
  @default_locale.freeze
  @hosts.freeze
  @nonlocalized.freeze
  
  super
end

#host_preferred_locales(env) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/utopia/localization.rb', line 132

def host_preferred_locales(env)
  http_host = env[Rack::HTTP_HOST]
  
  locales = []
  
  # Get a list of all hosts which match the incoming htt_host:
  matching_hosts = @hosts.select{|host_pattern, locale| http_host =~ host_pattern}
  
  # Extract all the valid locales:
  matching_hosts.flat_map{|host_pattern, locale| locale}
end

#nonlocalized?(env) ⇒ Boolean

Returns:

  • (Boolean)


171
172
173
174
175
# File 'lib/utopia/localization.rb', line 171

def nonlocalized?(env)
  path_info = env[Rack::PATH_INFO]
  
  @nonlocalized.any? { |pattern| path_info[pattern] != nil }
end

#preferred_locales(env) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/utopia/localization.rb', line 106

def preferred_locales(env)
  return to_enum(:preferred_locales, env) unless block_given?
  
  # Keep track of what locales have been tried:
  locales = Set.new
  
  host_preferred_locales(env).each do |locale|
    yield env.merge(CURRENT_LOCALE_KEY => locale) if locales.add? locale
  end
  
  request_preferred_locale(env) do |locale, path|
    # We have extracted a locale from the path, so from this point on we should use the updated path:
    env = env.merge(Rack::PATH_INFO => path.to_s)
    
    yield env.merge(CURRENT_LOCALE_KEY => locale) if locales.add? locale
  end
  
  browser_preferred_locales(env).each do |locale|
    yield env.merge(CURRENT_LOCALE_KEY => locale) if locales.add? locale
  end
  
  @default_locales.each do |locale|
    yield env.merge(CURRENT_LOCALE_KEY => locale) if locales.add? locale
  end
end

#request_preferred_locale(env) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/utopia/localization.rb', line 144

def request_preferred_locale(env)
  path = Path[env[Rack::PATH_INFO]]
  
  if @all_locales.include? path.first
    request_locale = path.first
    
    # Remove the localization prefix:
    path.delete_at(0)
    
    yield request_locale, path
  end
end

#vary(env, response) ⇒ Object

Set the Vary: header on the response to indicate that this response should include the header in the cache key.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/utopia/localization.rb', line 178

def vary(env, response)
  headers = response[1]
  
  # This response was based on the Accept-Language header:
  if headers['Vary']
    headers['Vary'] += ',Accept-Language'
  else
    headers['Vary'] = 'Accept-Language'
  end
  
  # Althought this header is generally not supported, we supply it anyway as it is useful for debugging:
  if locale = env[CURRENT_LOCALE_KEY]
    # Set the Content-Location to point to the localized URI as requested:
    headers['Content-Location'] = "/#{locale}" + env[Rack::PATH_INFO]
  end
  
  return response
end