Class: Utopia::Localization::Preferences

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

Overview

Immutable localization preferences for a request or resolved resource.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_locales:, preferred_locales:, default_locale:, locale: preferred_locales.first) ⇒ Preferences

Initialize localization preferences.



15
16
17
18
19
20
21
22
# File 'lib/utopia/localization/preferences.rb', line 15

def initialize(all_locales:, preferred_locales:, default_locale:, locale: preferred_locales.first)
	@all_locales = all_locales
	@preferred_locales = preferred_locales
	@default_locale = default_locale
	@locale = locale
	
	freeze
end

Instance Attribute Details

#all_localesObject (readonly)

Returns the value of attribute all_locales.



40
41
42
# File 'lib/utopia/localization/preferences.rb', line 40

def all_locales
  @all_locales
end

#default_localeObject (readonly)

Returns the value of attribute default_locale.



42
43
44
# File 'lib/utopia/localization/preferences.rb', line 42

def default_locale
  @default_locale
end

#localeObject (readonly)

Returns the value of attribute locale.



43
44
45
# File 'lib/utopia/localization/preferences.rb', line 43

def locale
  @locale
end

#preferred_localesObject (readonly)

Returns the value of attribute preferred_locales.



41
42
43
# File 'lib/utopia/localization/preferences.rb', line 41

def preferred_locales
  @preferred_locales
end

Instance Method Details

#freezeObject

Freeze this object and its internal state.



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/utopia/localization/preferences.rb', line 26

def freeze
	return self if frozen?
	
	@all_locales.each(&:freeze)
	@preferred_locales.each(&:freeze)
	@default_locale.freeze
	@locale.freeze
	
	@all_locales.freeze
	@preferred_locales.freeze
	
	return super
end

#localized?Boolean

Whether localization is active.

Returns:

  • (Boolean)


47
48
49
# File 'lib/utopia/localization/preferences.rb', line 47

def localized?
	!@all_locales.empty?
end

#localized_path(path, locale = @locale) ⇒ Object

Build a path with the given locale prefix.



55
56
57
58
59
60
61
# File 'lib/utopia/localization/preferences.rb', line 55

def localized_path(path, locale = @locale)
	if locale
		return "/#{locale}#{path}"
	else
		return path.to_s
	end
end

#with(locale:) ⇒ Object

Select a locale without changing the original preferences.



66
67
68
69
70
71
72
73
# File 'lib/utopia/localization/preferences.rb', line 66

def with(locale:)
	self.class.new(
		all_locales: @all_locales,
		preferred_locales: @preferred_locales,
		default_locale: @default_locale,
		locale: locale,
	)
end