Class: Translatomatic::ResourceFile::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/resource_file/base.rb

Overview

This class is abstract.

Subclasses implement different types of resource files

Direct Known Subclasses

Properties, Text, XCodeStrings, XML, YAML

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#log, #string

Constructor Details

#initialize(path, locale = nil) ⇒ Translatomatic::ResourceFile::Base

Create a new resource file. If locale is unspecified, attempts to determine the locale of the file automatically, and if that fails, uses the default locale.

Parameters:

  • path (String)

    Path to the file

  • locale (String) (defaults to: nil)

    Locale of the file contents



21
22
23
24
25
26
27
# File 'lib/translatomatic/resource_file/base.rb', line 21

def initialize(path, locale = nil)
  @path = path.kind_of?(Pathname) ? path : Pathname.new(path)
  @locale = locale || detect_locale || Translatomatic::Locale.default
  raise "unable to determine locale" unless @locale && @locale.language
  @valid = false
  @properties = {}
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



4
5
6
# File 'lib/translatomatic/resource_file/base.rb', line 4

def locale
  @locale
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/translatomatic/resource_file/base.rb', line 5

def path
  @path
end

#propertiesHash<String,String>

Returns key -> value properties.

Returns:



8
9
10
# File 'lib/translatomatic/resource_file/base.rb', line 8

def properties
  @properties
end

Class Method Details

.extensionsArray<String>

Returns File extensions supported by this resource file.

Returns:

  • (Array<String>)

    File extensions supported by this resource file



11
12
13
# File 'lib/translatomatic/resource_file/base.rb', line 11

def self.extensions
  raise "extensions must be implemented by subclass"
end

Instance Method Details

#formatString

Returns The format of this resource file, e.g. “Properties”.

Returns:

  • (String)

    The format of this resource file, e.g. “Properties”



30
31
32
# File 'lib/translatomatic/resource_file/base.rb', line 30

def format
  self.class.name.demodulize.downcase.to_sym
end

#get(name) ⇒ String

Get the value of a property

Parameters:

  • name (String)

    The name of the property

Returns:

  • (String)

    The value of the property



68
69
70
# File 'lib/translatomatic/resource_file/base.rb', line 68

def get(name)
  @properties[name]
end

#locale_path(locale) ⇒ Pathname

Create a path for the current resource file with a given locale

Parameters:

  • locale (String)

    for the path

Returns:

  • (Pathname)

    The path of this resource file modified for the given locale



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/translatomatic/resource_file/base.rb', line 37

def locale_path(locale)
  basename = path.sub_ext('').basename.to_s

  extlist = extension_list
  if extlist.length >= 2 && loc_idx = find_locale(extlist)
    # extension(s) contains locale, replace it
    extlist[loc_idx] = locale.to_s
  elsif valid_locale?(basename)
    # basename is a locale name, replace it
    path.dirname + (locale.to_s + path.extname)
  else
    # remove any underscore and trailing text from basename
    deunderscored = basename.sub(/_.*?$/, '')
    # add _locale.ext
    filename = deunderscored + "_" + locale.to_s + path.extname
    path.dirname + filename
  end
end

#save(target = path, options = {}) ⇒ void

This method returns an undefined value.

Save the resource file.

Parameters:

  • target (Pathname) (defaults to: path)

    The destination path

  • options (Hash<Symbol, Object>) (defaults to: {})

    Output format options



90
91
92
# File 'lib/translatomatic/resource_file/base.rb', line 90

def save(target = path, options = {})
  raise "save(path) must be implemented by subclass"
end

#sentencesObject



99
100
101
102
103
104
105
106
# File 'lib/translatomatic/resource_file/base.rb', line 99

def sentences
  sentences = []
  properties.values.each do |value|
    string = Translatomatic::String.new(value, locale)
    sentences += string.sentences
  end
  sentences
end

#set(name, value) ⇒ String

Set a property

Parameters:

  • key (String)

    The name of the property

  • value (String)

    The new value of the property

Returns:

  • (String)

    The new value of the property



76
77
78
# File 'lib/translatomatic/resource_file/base.rb', line 76

def set(name, value)
  @properties[name] = value
end

#to_sString

Returns String representation of this file.

Returns:

  • (String)

    String representation of this file



95
96
97
# File 'lib/translatomatic/resource_file/base.rb', line 95

def to_s
  "#{path.basename.to_s} (#{locale})"
end

#valid?Boolean

Test if the current resource file is valid

Returns:

  • (Boolean)

    true if the current file is valid



82
83
84
# File 'lib/translatomatic/resource_file/base.rb', line 82

def valid?
  @valid
end