Class: Twine::Formatters::JQuery

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

Constant Summary

Constants inherited from Abstract

Abstract::LANGUAGE_CODE_WITH_OPTIONAL_REGION_CODE

Instance Attribute Summary

Attributes inherited from Abstract

#options, #twine_file

Instance Method Summary collapse

Methods inherited from Abstract

#can_handle_directory?, #escape_quotes, #format_comment, #format_definition, #format_header, #format_key_value, #initialize, #output_path_for_language, #set_comment_for_key, #set_translation_for_key, #should_include_definition

Constructor Details

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

Instance Method Details

#default_file_nameObject



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

def default_file_name
  'localize.json'
end

#determine_language_given_path(path) ⇒ Object



16
17
18
19
20
21
# File 'lib/twine/formatters/jquery.rb', line 16

def determine_language_given_path(path)
  match = /^.+-([^-]{2})\.json$/.match File.basename(path)
  return match[1] if match

  return super
end

#extensionObject



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

def extension
  '.json'
end

#format_file(lang) ⇒ Object



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

def format_file(lang)
  result = super
  return result unless result
  "{\n#{super}\n}\n"
end

#format_key(key) ⇒ Object



73
74
75
# File 'lib/twine/formatters/jquery.rb', line 73

def format_key(key)
  escape_quotes(key)
end

#format_nameObject



4
5
6
# File 'lib/twine/formatters/jquery.rb', line 4

def format_name
  'jquery'
end

#format_section(section, lang) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/twine/formatters/jquery.rb', line 61

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

  definitions.map! { |definition| format_definition(definition, lang) }
  definitions.compact! # remove nil definitions
  definitions.join(",\n")
end

#format_section_header(section) ⇒ Object



58
59
# File 'lib/twine/formatters/jquery.rb', line 58

def format_section_header(section)
end

#format_sections(twine_file, lang) ⇒ Object



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

def format_sections(twine_file, lang)
  sections = twine_file.sections.map { |section| format_section(section, lang) }
  sections.delete_if(&:empty?)
  sections.join(",\n\n")
end

#format_value(value) ⇒ Object



77
78
79
# File 'lib/twine/formatters/jquery.rb', line 77

def format_value(value)
  escape_quotes(value)
end

#key_value_patternObject



69
70
71
# File 'lib/twine/formatters/jquery.rb', line 69

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

#read(io, lang) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/twine/formatters/jquery.rb', line 33

def read(io, 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

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

#set_translation_for_key_recursive(key, lang, value) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/twine/formatters/jquery.rb', line 23

def set_translation_for_key_recursive(key, lang, value)
  if value.is_a?(Hash)
    value.each do |key2, value2|
      set_translation_for_key_recursive(key+"."+key2, lang, value2)
    end
  else
    set_translation_for_key(key, lang, value)
  end
end