Class: Twine::Formatters::JQuery

Inherits:
Abstract
  • Object
show all
Defined in:
lib/twine/formatters/jquery.rb

Constant Summary collapse

FORMAT_NAME =
'jquery'
EXTENSION =
'.json'
DEFAULT_FILE_NAME =
'localize.json'

Instance Attribute Summary

Attributes inherited from Abstract

#options, #strings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#escape_quotes, #format_comment, #format_header, #format_key_value, #format_row, #initialize, #output_path_for_language, #row_pattern, #set_comment_for_key, #set_translation_for_key, #write_all_files

Constructor Details

This class inherits a constructor from Twine::Formatters::Abstract

Class Method Details

.can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/twine/formatters/jquery.rb', line 8

def self.can_handle_directory?(path)
  Dir.entries(path).any? { |item| /^.+\.json$/.match(item) }
end

Instance Method Details

#default_file_nameObject



12
13
14
# File 'lib/twine/formatters/jquery.rb', line 12

def default_file_name
  return DEFAULT_FILE_NAME
end

#determine_language_given_path(path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/twine/formatters/jquery.rb', line 16

def determine_language_given_path(path)
  path_arr = path.split(File::SEPARATOR)
  path_arr.each do |segment|
    match = /^((.+)-)?([^-]+)\.json$/.match(segment)
    if match
      return match[3]
    end
  end

  return
end

#format_file(strings, lang) ⇒ Object



43
44
45
# File 'lib/twine/formatters/jquery.rb', line 43

def format_file(strings, lang)
  "{\n#{super}\n}"
end

#format_key(key) ⇒ Object



67
68
69
# File 'lib/twine/formatters/jquery.rb', line 67

def format_key(key)
  escape_quotes(key)
end

#format_section(section, lang) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/twine/formatters/jquery.rb', line 55

def format_section(section, lang)
  rows = section.rows.dup

  rows.map! { |row| format_row(row, lang) }
  rows.compact! # remove nil entries
  rows.join(",\n")
end

#format_section_header(section) ⇒ Object



52
53
# File 'lib/twine/formatters/jquery.rb', line 52

def format_section_header(section)
end

#format_sections(strings, lang) ⇒ Object



47
48
49
50
# File 'lib/twine/formatters/jquery.rb', line 47

def format_sections(strings, lang)
  sections = strings.sections.map { |section| format_section(section, lang) }
  sections.join(",\n\n")
end

#format_value(value) ⇒ Object



71
72
73
# File 'lib/twine/formatters/jquery.rb', line 71

def format_value(value)
  escape_quotes(value)
end

#key_value_patternObject



63
64
65
# File 'lib/twine/formatters/jquery.rb', line 63

def key_value_pattern
  "\"%{key}\":\"%{value}\""
end

#read_file(path, lang) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/twine/formatters/jquery.rb', line 28

def read_file(path, lang)
  begin
    require "json"
  rescue LoadError
    raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files."
  end

  open(path) do |io|
    json = JSON.load(io)
    json.each do |key, value|
      set_translation_for_key(key, lang, value)
    end
  end
end

#write_file(path, lang) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/twine/formatters/jquery.rb', line 75

def write_file(path, lang)
  begin
    require "json"
  rescue LoadError
    raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files."
  end
  super
end