class PolyglotFlutter::Serializer::Translation

Public Class Methods

create_trainslation(key, value) click to toggle source
# 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) click to toggle source
# 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