class Fastlane::Helper::Config

Attributes

breaks[R]
charset[R]
directory[R]
fallback[R]
filter[R]
format[R]
index[R]
key[R]
locales[R]
namespace[R]
no_comments[R]
no_folding[R]
order[R]
platform[R]
printf[R]
source[R]
status[R]

Public Class Methods

new(platform:, directory:, locales:, key:, format: "", filter: [], index: "", source: "", namespace: "", fallback: "", order: "", status: "", printf: "", charset: "", breaks: "", no_comments: "", no_folding: "", custom_extension: "", custom_file_name: "") click to toggle source
# File lib/fastlane/plugin/simple_loco/helper/simple_loco_helper.rb, line 15
def initialize(platform:,
               directory:,
               locales:,
               key:,
               format: "",
               filter: [],
               index: "",
               source: "",
               namespace: "",
               fallback: "",
               order: "",
               status: "",
               printf: "",
               charset: "",
               breaks: "",
               no_comments: "",
               no_folding: "",
               custom_extension: "",
               custom_file_name: "")

  # Check for required fields
  missing_fields = []
  if platform.to_s.empty?
    missing_fields.push 'platform'
  end
  if directory.to_s.empty?
    missing_fields.push 'directory'
  end
  if key.to_s.empty?
    missing_fields.push 'key'
  end
  if locales.to_s.empty?
    missing_fields.push 'locales'
  end

  if !missing_fields.empty?
    raise "Not all required fields are filled: #{missing_fields.join(", ")}"
  end

  @platform = platform
  @directory = directory
  @locales = locales
  @key = key
  @format = format
  @filter = filter
  @index = index
  @source = source
  @namespace = namespace
  @fallback = fallback
  @order = order
  @status = status
  @printf = printf
  @charset = charset
  @breaks = breaks
  @no_comments = no_comments
  @no_folding = no_folding
  
  if platform == PLATFORM_ANDROID
    @adapter = AndroidAdapter.new
  elsif platform == PLATFORM_IOS
    @adapter = CocoaAdapter.new
  elsif platform == PLATFORM_FLUTTER
    @adapter = FlutterAdapter.new
  elsif platform == PLATFORM_XAMARIN
    @adapter = XamarinAdapter.new
  elsif platform == PLATFORM_CUSTOM
    @adapter = CustomAdapter.new(
      custom_extension: custom_extension,
      custom_file_name: custom_file_name)
  else
    raise "Unsupported platform '#{platform}'"
  end
end
read_conf(path) click to toggle source

Static method used to read a config file

# File lib/fastlane/plugin/simple_loco/helper/simple_loco_helper.rb, line 222
def self.read_conf(path)
  accepted_formats = [".json", ".yaml", ".yml"]
  extension = File.extname(path)

  if !accepted_formats.include?(extension)
    raise "Unsupported config file format #{extension}, only JSON and YAML files are supported."
  end

  if extension == ".json"
    json = JSON.parse(File.read(path))        
    return Config.new(json.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; })
  end

  yaml = YAML.safe_load(File.read(path))
  return Config.new(yaml.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; })
end

Public Instance Methods

export(locale, extension) click to toggle source

Exports the locale for the corresponding extension

@param [String] the locale @param [String] the extension, must include the `.`

# File lib/fastlane/plugin/simple_loco/helper/simple_loco_helper.rb, line 142
def export(locale, extension)

  optional_query_params = {}
  if !@format.to_s.empty?
    optional_query_params["format"] = @format 
  end
  if @filter != nil && !filter.empty?
    optional_query_params["filter"] = @filter.join(',') 
  end
  if !@index.to_s.empty?
    optional_query_params["index"] = @index 
  end
  if !@source.to_s.empty?
    optional_query_params["source"] = @source 
  end
  if !@namespace.to_s.empty?
    optional_query_params["namespace"] = @namespace 
  end
  if !@fallback.to_s.empty?
    optional_query_params["fallback"] = @fallback 
  end
  if !@order.to_s.empty?
    optional_query_params["order"] = @order 
  end
  if !@status.to_s.empty?
    optional_query_params["status"] = @status 
  end
  if !@printf.to_s.empty?
    optional_query_params["printf"] = @printf 
  end
  if !@charset.to_s.empty?
    optional_query_params["charset"] = @charset 
  end
  if !@breaks.to_s.empty?
    optional_query_params["breaks"] = @breaks 
  end
  if !@no_comments.to_s.empty?
    optional_query_params["no-comments"] = @no_comments 
  end
  if !@no_folding.to_s.empty?
    optional_query_params["no-folding"] = @no_folding 
  end

  uri = URI::HTTPS.build(scheme: 'https',
                         host: 'localise.biz',
                         path: "/api/export/locale/#{locale}#{extension}",
                         query: URI.encode_www_form(optional_query_params)
                        )

  res = Net::HTTP.start(uri.host, uri.port,
    :use_ssl => uri.scheme == 'https') do |http|
      req = Net::HTTP::Get.new(uri)
      req['Authorization'] = "Loco #{@key}"
      http.request req
  end
  
  if res.code == '200'
    body = res.body
  
    # Extracting charset because Net does not do it
    content_type = res['Content-Type']
    charset = /charset=([^ ;]*)/.match content_type
    unless charset.nil?
      body = body.force_encoding(charset.captures[0])
                 .encode('utf-8')
    end
  
    return body
  end
  
  warn 'URL failed: ' + uri.to_s
  nil
end
export_locales() click to toggle source
# File lib/fastlane/plugin/simple_loco/helper/simple_loco_helper.rb, line 107
def export_locales

  FileUtils.mkdir_p @directory unless File.directory? @directory

  @locales.each_with_index do |locale, index|

    @adapter.allowed_extensions.each do |extension|

      # Download
  
      result = export locale, extension
  
      if result.nil?
        raise "Could not export locale #{locale} with extension #{extension}"
      end
  
      # Write
  
      result_directory = locale_directory locale, index.zero?
      FileUtils.mkdir_p result_directory unless File.directory? result_directory

      @adapter.write_locale(result_directory,
                            result,
                            locale,
                            extension,
                            index.zero?)

    end
  end  
end
locale_directory(locale, is_default) click to toggle source
# File lib/fastlane/plugin/simple_loco/helper/simple_loco_helper.rb, line 216
def locale_directory(locale, is_default)  
  return File.join(@directory,
            @adapter.directory(locale, is_default))
end