module Translatomatic::ResourceFile
Provides methods to create resource files of various types.
Public Class Methods
create(path, options = {})
click to toggle source
Create a new resource file
# File lib/translatomatic/resource_file.rb, line 24 def create(path, options = {}) klass = types_for_path(path).first return nil unless klass file = klass.new file.path = path file.locale = build_locale(options[:locale]) file end
find(path, options = {})
click to toggle source
Find all resource files under the given directory. Follows symlinks. @param path [String, Pathname] The path to search from @return [Array<Translatomatic::ResourceFile>] Resource files found
# File lib/translatomatic/resource_file.rb, line 36 def find(path, options = {}) files = [] include_dot_directories = options[:include_dot_directories] path = Pathname.new(path) unless path.is_a?(Pathname) path.find do |file| puts "loading #{file}" if !include_dot_directories && file.basename.to_s[0] == '.' Find.prune else resource = load(file) files << resource if resource end end files end
load(path, options = {})
click to toggle source
Load a resource file. If options is not specified, the locale of the file will be determined from the filename, or else the current default locale will be used. @param path [String] Path to the resource file @return [Translatomatic::ResourceFile::Base] The resource file, or nil
if the file type is unsupported.
# File lib/translatomatic/resource_file.rb, line 14 def load(path, options = {}) path = path.is_a?(Pathname) ? path : Pathname.new(path) types_for_path(path).each do |klass| log.debug(t('file.loading', file: path, name: klass.name.demodulize)) return klass.new(path, options) end nil end
types()
click to toggle source
Find all configured resource file classes @return [Array<Class>] Available resource file classes
# File lib/translatomatic/resource_file.rb, line 54 def types @types ||= constants.map { |c| const_get(c) }.select do |klass| klass.is_a?(Class) && klass != Base && klass < Base end end
Private Class Methods
extension_match(klass, path)
click to toggle source
# File lib/translatomatic/resource_file.rb, line 68 def extension_match(klass, path) filename = path.basename.to_s.downcase klass.extensions.each do |extension| # don't match end of line in case file has locale extension return true if filename =~ /\.#{extension}/ end false end
types_for_path(path)
click to toggle source
find classes that can load the given path by file extension
# File lib/translatomatic/resource_file.rb, line 63 def types_for_path(path) path = path.is_a?(Pathname) ? path : Pathname.new(path) types.select { |klass| klass.enabled? && extension_match(klass, path) } end