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 _]

Instance Method Summary collapse

Instance Method Details

#clean_enum_name(name) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 34

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



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

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



62
63
64
65
66
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 62

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

  "#{name}_"
end

#escape_with_underscore_if_needed(name) ⇒ Object



56
57
58
59
60
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 56

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

  '_' + name
end

#extract_translations(translation_keys, language) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 95

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

#find_app_language(languages, mandatory_language) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 130

def find_app_language(languages, mandatory_language)
  languages.each do |lang|
    if lang['locale'].split('_').first == mandatory_language
      return lang
    end
  end
end

#generate_locales(languages) ⇒ Object



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

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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 73

def generate_localization_keys(languages, translation_keys, mandatory_language)
  result_string = ''
  language = find_app_language(languages, mandatory_language)

  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

#generate_static_locales(languages) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 104

def generate_static_locales(languages)
  result_string = ''
  language_locales = languages.each.map { |value| value['locale'].split('_').first }
  locale_names = language_locales.each.map { |value| clean_variable_name("locale_#{value}") }

  locale_names.each_with_index { |locale, index| result_string += "static Locale get #{locale} => const Locale('#{language_locales[index]}', '');\n\t" }

  locale_list = locale_names.each.map { |value| "AppLocales.#{value}" }.join(', ')
  result_string += "\n\tstatic List<Locale> get values => <Locale>[#{locale_list}];"
end

#generate_strings(languages, translation_keys, mandatory_language) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/flutter_polyglot_cli/helpers/general.rb', line 115

def generate_strings(languages, translation_keys, mandatory_language)
  result_string = ''
  language = find_app_language(languages, mandatory_language)

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

    clean_var_name = clean_variable_name(key)
    result_string += "\t#{clean_var_name},\n"
  end

  result_string
end

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



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

def indent(level = 0, initial = '')
  (1..level)
    .to_a.reduce('') { |result, _value| result + '    ' }
    .concat(initial)
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