class Preferences
Immutable localization preferences for a request or resolved resource.
Definitions
def initialize(all_locales:, preferred_locales:, default_locale:, locale: preferred_locales.first)
Initialize localization preferences.
Signature
-
parameter
all_localesArray(String) All configured locales.
-
parameter
preferred_localesArray(String | Nil) Locales in resolution order.
-
parameter
default_localeString | Nil The configured default locale.
-
parameter
localeString | Nil The currently selected locale.
Implementation
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
def freeze
Freeze this object and its internal state.
Signature
-
returns
self This object.
Implementation
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
def localized?
Whether localization is active.
Signature
-
returns
Boolean truewhen locales are configured.
Implementation
def localized?
!@all_locales.empty?
end
def localized_path(path, locale = @locale)
Build a path with the given locale prefix.
Signature
-
parameter
pathUtopia::Path | String The path.
-
parameter
localeString | Nil The locale.
-
returns
String The locale-prefixed path.
Implementation
def localized_path(path, locale = @locale)
if locale
return "/#{locale}#{path}"
else
return path.to_s
end
end
def with(locale:)
Select a locale without changing the original preferences.
Signature
-
parameter
localeString | Nil The selected locale.
-
returns
Preferences A new localization preference object.
Implementation
def with(locale:)
self.class.new(
all_locales: @all_locales,
preferred_locales: @preferred_locales,
default_locale: @default_locale,
locale: locale,
)
end