Module: Translator::Utils

Included in:
SmartlingAPI, FileTranslator, SmartlingDownloader
Defined in:
lib/nexmo_developer/app/services/translator/utils.rb

Instance Method Summary collapse

Instance Method Details

#file_path(filename, locale) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nexmo_developer/app/services/translator/utils.rb', line 47

def file_path(filename, locale)
  folder = storage_folder(filename, locale)
  file_name = if filename.starts_with? '_documentation'
                Pathname.new(file_uri(filename)).basename.to_s
              elsif filename.starts_with? 'config/locales'
                "#{locale}#{Pathname.new(file_uri(filename)).extname}"
              else
                Pathname.new(file_uri(filename)).basename.to_s
              end
  "#{folder}/#{file_name}"
end

#file_uri(filename) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/nexmo_developer/app/services/translator/utils.rb', line 59

def file_uri(filename)
  if filename.starts_with? '_documentation'
    filename.gsub(%r{_documentation/[a-z]{2}/}, '')
  else
    filename.gsub(%r{(_use_cases|_tutorials|config/tutorials)/#{I18n.default_locale}/(.*)}) do |_|
      "#{$1}/#{$2}"
    end
  end
end

#locale_with_region(locale) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/nexmo_developer/app/services/translator/utils.rb', line 14

def locale_with_region(locale)
  case locale.to_s
  when 'ja', 'ja-JP'
    'ja-JP'
  when 'cn', 'zh-CN'
    'zh-CN'
  else
    locale.to_s
  end
end

#locale_without_region(locale) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/nexmo_developer/app/services/translator/utils.rb', line 3

def locale_without_region(locale)
  case locale
  when 'zh-CN', 'cn'
    :cn
  when 'ja', 'ja-JP'
    :ja
  else
    :en
  end
end

#storage_folder(filename, locale) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nexmo_developer/app/services/translator/utils.rb', line 25

def storage_folder(filename, locale)
  if filename.starts_with? '_documentation'
    dir_path = Pathname.new(file_uri(filename)).dirname.to_s
    "#{Rails.configuration.docs_base_path}/_documentation/#{locale}/#{dir_path}"
  elsif filename.starts_with? '_use_cases'
    "#{Rails.configuration.docs_base_path}/_use_cases/#{locale}"
  elsif filename.starts_with? 'config/locales'
    Pathname.new(file_uri(filename)).dirname.to_s
  elsif filename.starts_with? 'config/tutorials'
    pathname = Pathname.new(filename.gsub("config/tutorials/#{I18n.default_locale}/", ''))
    dir_path = pathname.dirname.to_s == '.' ? '' : "/#{pathname.dirname}"
    "#{Rails.configuration.docs_base_path}/config/tutorials/#{locale}#{dir_path}"
  elsif filename.starts_with? '_tutorials'
    pathname = Pathname.new(filename.gsub('_tutorials/', ''))
    dir_path = pathname.dirname.to_s == '.' ? '' : "/#{pathname.dirname}"
    "#{Rails.configuration.docs_base_path}/_tutorials/#{locale}#{dir_path}"
  else
    dir_path = Pathname.new(file_uri(filename)).dirname.to_s
    "#{Rails.configuration.docs_base_path}/_documentation/#{locale}/#{dir_path}"
  end
end