Sha256: ed51ce923c5707f9faa3a29434b57905340c501ebe14eed4abca9b13456e5808

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'fileutils'

module FileManager
  def mkdir_locales
    FileUtils.mkdir_p(LokaliseManager::GlobalConfig.locales_path) unless File.directory?(LokaliseManager::GlobalConfig.locales_path)
  end

  def rm_translation_files
    FileUtils.rm_rf locales_dir
  end

  def locales_dir
    Dir["#{LokaliseManager::GlobalConfig.locales_path}/**/*"]
  end

  def count_translations
    locales_dir.count { |file| File.file?(file) }
  end

  def add_translation_files!(with_ru: false, additional: nil)
    FileUtils.mkdir_p "#{Dir.getwd}/locales/nested"
    open_and_write('locales/nested/en.yml') { |f| f.write en_data }

    return unless with_ru

    open_and_write('locales/ru.yml') { |f| f.write ru_data }

    return unless additional

    additional.times do |i|
      data = {'en' => {"key_#{i}" => "value #{i}"}}

      open_and_write("locales/en_#{i}.yml") { |f| f.write data.to_yaml }
    end
  end

  def open_and_write(rel_path, &block)
    return unless block

    File.open("#{Dir.getwd}/#{rel_path}", 'w+:UTF-8', &block)
  end

  private

  def en_data
    <<~DATA
      en:
        my_key: "My value"
        nested:
          key: "Value 2"
    DATA
  end

  def ru_data
    <<~DATA
      ru_RU:
        my_key: "Моё значение"
        nested:
          key: "Значение 2"
    DATA
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lokalise_manager-2.2.1 spec/support/file_manager.rb
lokalise_manager-2.2.0 spec/support/file_manager.rb
lokalise_manager-2.1.0 spec/support/file_manager.rb
lokalise_manager-2.0.0 spec/support/file_manager.rb
lokalise_manager-1.2.1 spec/support/file_manager.rb
lokalise_manager-1.2.0 spec/support/file_manager.rb
lokalise_manager-1.1.0 spec/support/file_manager.rb
lokalise_manager-1.0.0 spec/support/file_manager.rb