Module: PolyglotFlutter::Helper::General

Included in:
Command::Projects, Command::Pull, Command::Setup, Resource::Language, Serializer::Localization::Base, Serializer::Translation
Defined in:
lib/flutter_polyglot_cli/helpers/general.rb

Constant Summary collapse

ESCAPE_KEYWORDS =
%w[abstract as assert async await break case
catch class const continue covariant default deferred do
else enum export extends external factory false final
finally for Function get hide if import in interface
is library mixin new null on operator part rethrow return
set static super switch sync this throw true try typedef var
void while with yield _].freeze

Instance Method Summary collapse

Instance Method Details

#clean_enum_name(name) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 46

def clean_enum_name(name)
  clean_name = name
               .split(/[^0-9a-zA-Z]/)
               .reject(&:empty?)
               .map(&:capitalize)
               .join

  escape_with_underscore_if_needed(clean_name)
end

#clean_variable_name(name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 56

def clean_variable_name(name)
  clean_name = name
               .split(/[^0-9a-zA-Z]/)
               .reject(&:empty?)
               .each_with_index
               .map { |value, index| index == 0 ? value.downcase : value.capitalize }
               .join

  escaped_underscore = escape_with_underscore_if_needed(clean_name)
  escape_keyword_if_needed(escaped_underscore)
end

#configObject



20
21
22
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 20

def config
  @config ||= PolyglotFlutter::IO::Config.read.with_indifferent_access
end

#escape_keyword_if_needed(name) ⇒ Object



74
75
76
77
78
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 74

def escape_keyword_if_needed(name)
  return name unless ESCAPE_KEYWORDS.include? name

  "`#{name}`"
end

#escape_with_underscore_if_needed(name) ⇒ Object



68
69
70
71
72
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 68

def escape_with_underscore_if_needed(name)
  return name if name.match(/^[A-Za-z_]/)

  '_' + name
end

#extract_translations(translation_keys, language) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 112

def extract_translations(translation_keys, language)
  translation_keys.each_with_object({}) do |translation_key, strings|
    key_name = translation_key.name
    next if key_name.nil?

    strings[key_name] = translation_key.clean_translation(language)
  end
end

#generate_locales(languages) ⇒ Object



80
81
82
83
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 80

def generate_locales(languages)
  language_locales = languages.each.map { |value| value['locale'].split('_').first }.join("', '")
  "['#{language_locales}']"
end

#generate_localization_keys(languages, translation_keys, mandatory_language) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 85

def generate_localization_keys(languages, translation_keys, mandatory_language)
  result_string = ''
  language = nil
  languages.each do |lang|
    if lang['locale'].split('_').first == mandatory_language
      language = lang
    end
  end

  dict = extract_translations(translation_keys, language)
  dict.each do |key, value|
    next if value.nil?

    clean_var_name = clean_variable_name(key)
    if value.match(/{(arg\d)}/)
      matches = value.scan(/{(arg\d)}/)
      first_arguments = matches.join(', dynamic ')
      second_arguments = matches.join(', ')
      result_string += "String #{clean_var_name}(dynamic #{first_arguments}) => Intl.message('', name: '#{clean_var_name}', args: <dynamic>[#{second_arguments}]);\n"
    else
      result_string += "String get #{clean_var_name} => Intl.message('#{clean_var_name}');\n"
    end
  end

  result_string
end

#indent(level = 0, initial = '') ⇒ Object



40
41
42
43
44
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 40

def indent(level = 0, initial = '')
  (1..level)
    .to_a.reduce('') { |result, _value| result + '    ' }
    .concat(initial)
end

#mandatory_languageObject



32
33
34
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 32

def mandatory_language
  @mandatory_language ||= config[:mandatory_language]
end

#programming_languageObject



28
29
30
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 28

def programming_language
  @programming_language ||= config[:language]
end

#project_configsObject



24
25
26
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 24

def project_configs
  @project_configs ||= config[:projects]
end

#tokenObject



11
12
13
14
15
16
17
18
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 11

def token
  @token ||= PolyglotFlutter::IO::Token.read
  if @token.to_s.empty?
    PolyglotFlutter::Command::Login.init
    @token = PolyglotFlutter::IO::Token.read
  end
  @token
end

#use_old_namingObject



36
37
38
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 36

def use_old_naming
  @useOldNaming ||= config[:useOldNaming]
end