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_locales Array(String)

All configured locales.

parameter preferred_locales Array(String | Nil)

Locales in resolution order.

parameter default_locale String | Nil

The configured default locale.

parameter locale String | 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

true when 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 path Utopia::Path | String

The path.

parameter locale String | 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 locale String | 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