Module: Translatomatic::PathUtils

Included in:
ResourceFile::Base
Defined in:
lib/translatomatic/path_utils.rb

Overview

Utilities for locales and paths

Instance Method Summary collapse

Instance Method Details

#detect_path_locale(path) ⇒ Object

detect locale from path



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/translatomatic/path_utils.rb', line 41

def detect_path_locale(path)
  return nil unless path
  tag = nil
  basename = path.sub_ext('').basename.to_s
  directory = path.dirname.basename.to_s
  extlist = extension_list(path)

  if basename.match(/_([-\w]{2,})$/) &&
     valid_locale?(Regexp.last_match(1))
    # locale after underscore in filename
    tag = Regexp.last_match(1)
  elsif directory =~ /^([-\w]+)\.lproj$/
    # xcode localized strings
    tag = Regexp.last_match(1)
  elsif extlist.length >= 2 && (loc_idx = find_extension_locale(extlist))
    # multiple parts to extension, e.g. index.html.en
    tag = extlist[loc_idx]
  elsif valid_locale?(basename)
    # try to match on entire basename
    # (support for rails en.yml)
    tag = basename
  elsif valid_locale?(path.parent.basename)
    # try to match on parent directory, e.g. strings/en-US/text.resw
    tag = path.parent.basename
  elsif path.parent.basename.to_s.match(/-([-\w]+)/) &&
        valid_locale?(Regexp.last_match(1))
    # try to match on trailing part of parent directory,
    # e.g. res/values-en/strings.xml
    tag = Regexp.last_match(1)
  end

  tag ? Translatomatic::Locale.parse(tag, true) : nil
end

#modify_path_locale(path, target_locale, preferred_separator = '.') ⇒ Pathname

Find a new path representing the given path with a new locale

Parameters:

  • path (Pathname)

    The current path

  • target_locale (String)

    The target locale

Returns:

  • (Pathname)

    The new path for the given locale



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/translatomatic/path_utils.rb', line 8

def modify_path_locale(path, target_locale, preferred_separator = '.')
  basename = basename_stripped(path)

  extlist = extension_list(path)
  if extlist.length >= 2 && (loc_idx = find_extension_locale(extlist))
    # extension(s) contains locale, replace it
    extlist[loc_idx] = target_locale.to_s
    filename = basename + '.' + extlist.join('.')
    path.dirname + filename
  elsif valid_locale?(basename)
    # basename is a locale name, replace it
    path.dirname + (target_locale.to_s + path.extname)
  elsif basename.match(/_([-\w]+)\Z/) &&
        valid_locale?(Regexp.last_match(1))
    # basename contains locale, e.g. basename_$locale.ext
    add_basename_locale(path, target_locale, '_') # retain _
  elsif valid_locale?(path.parent.basename(path.parent.extname)) ||
        path.parent.basename.to_s == 'Base.lproj'
    # parent directory contains locale, e.g. strings/en-US/text.resw
    # or project/en.lproj/Strings.strings
    path.parent.parent + (target_locale.to_s +
      path.parent.extname) + path.basename
  elsif path.to_s =~ %r{\bres/values([-\w]+)?/.+$}
    # android strings
    filename = path.basename
    path.parent.parent + ('values-' + target_locale.to_s) + filename
  else
    # default behaviour, add locale after separator in basename
    add_basename_locale(path, target_locale, preferred_separator)
  end
end