Class: PolyglotFlutter::Serializer::Translation

Inherits:
Object
  • Object
show all
Extended by:
Helper::General
Defined in:
lib/flutter_polyglot_cli/serializers/translations/translations_serializer.rb

Constant Summary

Constants included from Helper::General

Helper::General::ESCAPE_KEYWORDS

Class Method Summary collapse

Methods included from Helper::General

clean_enum_name, clean_variable_name, config, escape_keyword_if_needed, escape_with_underscore_if_needed, extract_translations, find_app_language, generate_locales, generate_localization_keys, generate_static_locales, generate_strings, indent, project_configs, token

Class Method Details

.create_trainslation(key, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/flutter_polyglot_cli/serializers/translations/translations_serializer.rb', line 7

def create_trainslation(key, value)
key = clean_variable_name(key)
value = value.gsub(/\n/, '\\n').gsub(/"/, '\\"')
args = value.scan(/{arg\d+}/).map { |arg| %{"#{arg[1..-2]}": {}} }
args_part = ''
unless args.empty? 
  args_part = args.join ', '
  args_part = %{, "placeholders": {#{args_part} }}
end
return %{
      "#{key}": "#{value}",
      "@#{key}": { "type": "text"#{args_part}}}
end

.write(translation_keys, language, translations_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flutter_polyglot_cli/serializers/translations/translations_serializer.rb', line 21

def write(translation_keys, language, translations_path)
  translations = extract_translations(translation_keys, language)

  data = translations
         .sort
         .map { |key, value| create_trainslation(key, value) }
         .join(",\n")
         .concat("\n")
  data = "{\n\t\"@@locale\": \"#{language['locale'].split('_').first}\",\n#{data}}"
  unless File.exist? translations_path
    FileUtils.mkdir_p translations_path
  end
  locale_code = language.code(true)
  output_path = File.join(translations_path, "intl_#{locale_code}.arb")
  File.write(output_path, data)
end