Class: Translatomatic::ResourceFile::XCodeStrings

Inherits:
Base
  • Object
show all
Defined in:
lib/translatomatic/resource_file/xcode_strings.rb

Overview

XCode strings file

Instance Attribute Summary

Attributes inherited from Base

#locale, #path, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#format, #get, #sentences, #set, #to_s, #valid?

Methods included from Util

#locale, #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



13
14
15
16
17
# File 'lib/translatomatic/resource_file/xcode_strings.rb', line 13

def initialize(path, locale = nil)
  super(path, locale)
  @valid = true
  @properties = @path.exist? ? read(@path) : {}
end

Class Method Details

.extensionsArray<String>

Returns File extensions supported by this resource file.

Returns:

  • (Array<String>)

    File extensions supported by this resource file



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

def self.extensions
  %w{strings}
end

Instance Method Details

#locale_path(locale) ⇒ Pathname

Note:

localization files in XCode use the following file name convention: Project/locale.lproj/filename

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



22
23
24
25
26
27
28
29
30
# File 'lib/translatomatic/resource_file/xcode_strings.rb', line 22

def locale_path(locale)
  if path.to_s.match(/\/([-\w]+).lproj\/.+.strings$/)
    # xcode style
    filename = path.basename
    path.parent.parent + (locale.to_s + ".lproj") + filename
  else
    super(locale)
  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



33
34
35
36
37
38
39
40
41
42
# File 'lib/translatomatic/resource_file/xcode_strings.rb', line 33

def save(target = path, options = {})
  out = ""
  out += comment(created_by) unless options[:no_created_by]
  properties.each do |key, value|
    key = escape(key)
    value = escape(value)
    out += %Q{"#{key}" = "#{value}";\n}
  end
  target.write(out)
end