Class: Twine::Formatters::Flutter

Inherits:
Abstract
  • Object
show all
Defined in:
lib/formatter.rb

Instance Method Summary collapse

Instance Method Details

#default_file_nameObject



12
13
14
# File 'lib/formatter.rb', line 12

def default_file_name
    'app.arb'
end

#determine_language_given_path(path) ⇒ Object



16
17
18
19
20
21
# File 'lib/formatter.rb', line 16

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

#extensionObject



8
9
10
# File 'lib/formatter.rb', line 8

def extension
    '.arb'
end

#format_comment_and_placeholders(definition, lang) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/formatter.rb', line 72

def format_comment_and_placeholders(definition, lang)
    placeholdersScan = definition.translation_for_lang(lang).scan(/{[a-zA-Z0-9\-\_\.]+}/m)
    if definition.comment || !placeholdersScan.empty?
        comment = "        \"description\": \"#{definition.comment}\"" if definition.comment
        placeholders = placeholdersScan.map! { |placeholder| "            \"#{placeholder.tr('{}','')}\": {}" }.join(",\n") if !placeholdersScan.empty?
        placeholdersBlock = "        \"placeholders\": {\n#{placeholders}\n        }" if placeholders
        return "    \"@#{definition.key}\": {\n#{[comment, placeholdersBlock].compact.join(",\n")}\n    }"
    end
end

#format_definition(definition, lang) ⇒ Object



64
65
66
# File 'lib/formatter.rb', line 64

def format_definition(definition, lang)
    [format_key_value(definition, lang), format_comment_and_placeholders(definition, lang)].compact.join(",\n")
end

#format_file(lang) ⇒ Object



40
41
42
43
44
# File 'lib/formatter.rb', line 40

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

#format_key(key) ⇒ Object



82
83
84
# File 'lib/formatter.rb', line 82

def format_key(key)
    escape_quotes(key)
end

#format_nameObject



4
5
6
# File 'lib/formatter.rb', line 4

def format_name
    'flutter'
end

#format_section(section, lang) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/formatter.rb', line 56

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



52
53
54
# File 'lib/formatter.rb', line 52

def format_section_header(section)
    "\n"
end

#format_sections(twine_file, lang) ⇒ Object



46
47
48
49
50
# File 'lib/formatter.rb', line 46

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



86
87
88
# File 'lib/formatter.rb', line 86

def format_value(value)
    escape_quotes(value)
end

#key_value_patternObject



68
69
70
# File 'lib/formatter.rb', line 68

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

#read(io, lang) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/formatter.rb', line 23

def read(io, lang)
    require "json"
    
    json = JSON.load(io)
    json.each do |key, value|
        if key == "@@locale"
            # Ignore because it represents the file lang
        elsif key[0,1] == "@"
            if value["description"]
                set_comment_for_key(key[1..-1], value["description"])
            end
        else
            set_translation_for_key(key, lang, value)
        end
    end
end