Class: Twine::Formatters::Flutter
- Inherits:
-
Abstract
- Object
- Abstract
- Twine::Formatters::Flutter
- Includes:
- Placeholders
- Defined in:
- lib/flutter-intl-twine-formatter.rb
Instance Method Summary collapse
- #convert_placeholders_from_twine_to_icu(input) ⇒ Object
- #default_file_name ⇒ Object
- #determine_language_given_path(path) ⇒ Object
- #extension ⇒ Object
- #format_file(lang) ⇒ Object
- #format_key(key) ⇒ Object
- #format_name ⇒ Object
- #format_section(section, lang) ⇒ Object
- #format_section_header(section) ⇒ Object
- #format_sections(twine_file, lang) ⇒ Object
- #format_value(value) ⇒ Object
- #key_value_pattern ⇒ Object
- #read(io, lang) ⇒ Object
Instance Method Details
#convert_placeholders_from_twine_to_icu(input) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/flutter-intl-twine-formatter.rb', line 79 def convert_placeholders_from_twine_to_icu(input) value = convert_twine_string_placeholder(input) value.gsub(PLACEHOLDER_REGEX).each_with_index do |match, index| "{arg#{index + 1}}" end end |
#default_file_name ⇒ Object
15 16 17 |
# File 'lib/flutter-intl-twine-formatter.rb', line 15 def default_file_name 'intl_en.arb' end |
#determine_language_given_path(path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/flutter-intl-twine-formatter.rb', line 19 def determine_language_given_path(path) path_arr = path.split(File::SEPARATOR) path_arr.each do |segment| match = /^((.+)-)?([^-]+)\.arb$/.match(segment) if match return match[3] end end return end |
#extension ⇒ Object
11 12 13 |
# File 'lib/flutter-intl-twine-formatter.rb', line 11 def extension '.arb' end |
#format_file(lang) ⇒ Object
44 45 46 47 48 |
# File 'lib/flutter-intl-twine-formatter.rb', line 44 def format_file(lang) result = super return result unless result "{\n#{super}\n}\n" end |
#format_key(key) ⇒ Object
71 72 73 |
# File 'lib/flutter-intl-twine-formatter.rb', line 71 def format_key(key) escape_quotes(key) end |
#format_name ⇒ Object
7 8 9 |
# File 'lib/flutter-intl-twine-formatter.rb', line 7 def format_name 'flutter' end |
#format_section(section, lang) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/flutter-intl-twine-formatter.rb', line 59 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
56 57 |
# File 'lib/flutter-intl-twine-formatter.rb', line 56 def format_section_header(section) end |
#format_sections(twine_file, lang) ⇒ Object
50 51 52 53 54 |
# File 'lib/flutter-intl-twine-formatter.rb', line 50 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
75 76 77 |
# File 'lib/flutter-intl-twine-formatter.rb', line 75 def format_value(value) convert_placeholders_from_twine_to_icu(value) end |
#key_value_pattern ⇒ Object
67 68 69 |
# File 'lib/flutter-intl-twine-formatter.rb', line 67 def key_value_pattern "\"%{key}\": \"%{value}\"" end |
#read(io, lang) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flutter-intl-twine-formatter.rb', line 31 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 Flutter-localize files." end json = JSON.load(io) json.each do |key, value| set_translation_for_key(key, lang, value) end end |