class Translatomatic::ResourceFile::Base

Base class for resource file implementations @abstract Subclasses implement different types of resource files

Attributes

locale[RW]

@return [Locale] The locale of the contents of this resource file

options[R]

@return [Hash<Symbol,Object] Options used in the constructor

path[RW]

@return [Pathname] The path to this resource file

Public Class Methods

enabled?() click to toggle source

@return [boolean] True if this resource format is enabled

# File lib/translatomatic/resource_file/base.rb, line 16
def self.enabled?
  true
end
extensions() click to toggle source

@return [Array<String>] File extensions supported by this resource file

# File lib/translatomatic/resource_file/base.rb, line 27
def self.extensions
  raise 'extensions must be implemented by subclass'
end
key_value?() click to toggle source

@return [boolean] True if the file format consists of keys and values

# File lib/translatomatic/resource_file/base.rb, line 32
def self.key_value?
  false
end
new(path = nil, options = {}) click to toggle source

Create a new resource file or load an existing file. If options is unspecified, attempts to determine the locale of the file automatically, and if that fails, uses the default locale. Raises an exception if errors were encountered loading the file. @param path [String] Path to the file @param options [Hash<Symbol,String>] Options @return [Translatomatic::ResourceFile::Base] the resource file.

# File lib/translatomatic/resource_file/base.rb, line 50
def initialize(path = nil, options = {})
  raise 'expected options hash' if options && !options.is_a?(Hash)
  raise t('file.unsupported', file: path) unless self.class.enabled?
  initialize_attributes(path, options)
  update_locale
  init
  try_load
end
preferred_locale_separator() click to toggle source

@return [String] Preferred character to use to separate the locale

from the file basename
# File lib/translatomatic/resource_file/base.rb, line 22
def self.preferred_locale_separator
  '.'
end
supports_variable_interpolation?() click to toggle source

@return [boolean] true if this resource file supports

variable interpolation
# File lib/translatomatic/resource_file/base.rb, line 38
def self.supports_variable_interpolation?
  false
end

Public Instance Methods

create_variable(name) click to toggle source

Create an interpolated variable string. @param name [String] The variable name @return [String] A string representing the interpolated variable, or

nil if this resource file doesn't support variable interpolation.
# File lib/translatomatic/resource_file/base.rb, line 137
def create_variable(name)
  return nil unless self.class.supports_variable_interpolation?
  raise 'create_variable(name) must be implemented by subclass'
end
get(key) click to toggle source

Get the value of a property @param key [String] The name of the property @return [String] The value of the property

# File lib/translatomatic/resource_file/base.rb, line 99
def get(key)
  @properties[key]
end
get_context(key) click to toggle source

Get context of a property @param key [String] The name of the property @return [Array<String>] The property context, may be nil

# File lib/translatomatic/resource_file/base.rb, line 106
def get_context(key)
  @metadata.get_context(key)
end
locale_path(target_locale) click to toggle source

Create a path for the current resource file with a given locale @param target_locale [String] The target locale @return [Pathname] The path of this resource file modified

for the given locale
# File lib/translatomatic/resource_file/base.rb, line 76
def locale_path(target_locale)
  modify_path_locale(path, target_locale,
                     self.class.preferred_locale_separator)
end
properties() click to toggle source

@return [Hash<String,String>] key -> value properties

# File lib/translatomatic/resource_file/base.rb, line 92
def properties
  @properties.dup
end
properties=(properties) click to toggle source

Set all properties @param properties [Hash<String,String>] New properties

# File lib/translatomatic/resource_file/base.rb, line 83
def properties=(properties)
  # use set rather that set @properties directly as subclasses
  # override set()
  properties.each do |key, value|
    set(key, value)
  end
end
save(target = path, options = {}) click to toggle source

Save the resource file. @param target [Pathname] The destination path @param options [Hash<Symbol, Object>] Output format options @return [void]

# File lib/translatomatic/resource_file/base.rb, line 63
def save(target = path, options = {})
  raise 'save(path) must be implemented by subclass'
end
sentences() click to toggle source

@return [Array<Text>] All property values split into sentences

# File lib/translatomatic/resource_file/base.rb, line 124
def sentences
  sentences = []
  properties.each_value do |value|
    string = build_text(value, locale)
    sentences += string.sentences
  end
  sentences
end
set(key, value) click to toggle source

Set a property @param key [String] The name of the property @param value [String] The new value of the property @return [String] The new value of the property

# File lib/translatomatic/resource_file/base.rb, line 114
def set(key, value)
  @properties[key] = value
end
to_s() click to toggle source

@return [String] String representation of this file

# File lib/translatomatic/resource_file/base.rb, line 119
def to_s
  relative_path(path).to_s
end
type() click to toggle source

@return [Symbol] The type of this resource file, e.g. “:properties”

# File lib/translatomatic/resource_file/base.rb, line 68
def type
  self.class.name.demodulize.downcase.to_sym
end
variable_regex() click to toggle source

@return [Regexp] A regexp used to match interpolated variables

# File lib/translatomatic/resource_file/base.rb, line 143
def variable_regex
  return nil unless self.class.supports_variable_interpolation?
  raise 'variable_regex must be implemented by subclass'
end

Private Instance Methods

created_by() click to toggle source
# File lib/translatomatic/resource_file/base.rb, line 183
def created_by
  options = {
    app: 'Translatomatic',
    version: Translatomatic::VERSION,
    date: I18n.l(Time.now),
    locale: locale.language,
    url: Translatomatic::URL
  }
  t('file.created_by', options)
end
created_by?() click to toggle source
# File lib/translatomatic/resource_file/base.rb, line 194
def created_by?
  @created_by
end
init() click to toggle source

called by constructor before load

# File lib/translatomatic/resource_file/base.rb, line 156
def init; end
initialize_attributes(path, options) click to toggle source
# File lib/translatomatic/resource_file/base.rb, line 169
def initialize_attributes(path, options)
  @options = options || {}
  @properties = {}
  @metadata = Metadata.new
  @path = path.nil? || path.is_a?(Pathname) ? path : Pathname.new(path)
end
load() click to toggle source

load contents from @path

# File lib/translatomatic/resource_file/base.rb, line 159
def load
  raise 'load must be implemented by subclass'
end
parsing_error(error) click to toggle source
# File lib/translatomatic/resource_file/base.rb, line 198
def parsing_error(error)
  raise StandardError, error
end
try_load() click to toggle source
# File lib/translatomatic/resource_file/base.rb, line 163
def try_load
  return unless @path
  raise t('file.not_found', file: path) unless @path.exist?
  load
end
update_locale() click to toggle source
# File lib/translatomatic/resource_file/base.rb, line 176
def update_locale
  default = Translatomatic::Locale.default
  locale = @options[:locale] || detect_path_locale(path) || default
  @locale = Translatomatic::Locale.parse(locale)
  raise t('file.unknown_locale') unless @locale && @locale.language
end