Module: Jekyll::Msgcat

Defined in:
lib/jekyll/msgcat.rb,
lib/jekyll/msgcat/version.rb

Constant Summary collapse

MSGCAT_DB =
'_msgcat.yaml'
VERSION =
"1.0.0"
@@__msgcat =
nil

Instance Method Summary collapse

Instance Method Details

#__get_localeObject

Return a string (‘lt’, for example) or nil.



31
32
33
# File 'lib/jekyll/msgcat.rb', line 31

def __get_locale
  __get_site.andand['msgcat'].andand['locale']
end

#__get_msgcatObject

Return a hash of parsed messge catalog or nil.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/jekyll/msgcat.rb', line 12

def __get_msgcat
  return @@__msgcat if @@__msgcat

  begin
    file = File.join (__get_site["source"] || "./"), MSGCAT_DB
    @@__msgcat = YAML.load_file file, :safe => false
  rescue
    $stderr.puts "msgcat warning: `#{file}` not found: #{$!}"
    return nil
  end

  @@__msgcat
end

#__get_siteObject



26
27
28
# File 'lib/jekyll/msgcat.rb', line 26

def __get_site
  @context.registers[:site].config
end

#cur_page_in_another_locale(targetLocale, label = nil, cssClass = nil) ⇒ Object



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
74
75
76
77
78
79
80
81
82
# File 'lib/jekyll/msgcat.rb', line 49

def cur_page_in_another_locale targetLocale, label = nil, cssClass = nil
  raise 'target locale requred' if targetLocale =~ /^\s*$/

  site = __get_site
  pattern = "<a href='%s' class='#{cssClass} %s'>#{label || targetLocale}</a>"
  locale = __get_locale || 'en'

  # current site
  return pattern % ['#', 'disabled'] if locale == targetLocale

  deploy = site.andand['msgcat'].andand['deploy'] || 'domain'

  if deploy == 'domain'
    begin
      uri = URI site['url']
    rescue
      raise "invalid 'url' property in _config.yml: #{$!}"
    end
    host = uri.host.split '.'
    host[0] = targetLocale
    uri.host = host.join '.'
  else
    raise "no 'baseurl' property in _config.yml" unless site['baseurl']
    uri = site['baseurl'].split '/'
    if uri.size == 0
      uri = ['', targetLocale]
    else
      uri[uri.size-1] = targetLocale
    end
    uri = uri.join '/'
  end

  return pattern % [uri + @context.registers[:page]['url'], ""]
end

#mc(input) ⇒ Object

Extract localized version of ‘input’ from message catalog or return the original if no localization was found.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jekyll/msgcat.rb', line 37

def mc input
  locale = __get_locale || (return input)
  msgcat = __get_msgcat || (return input)

  if !msgcat[locale]
    $stderr.puts "msgcat warning: '#{locale}' wasn't found in #{MSGCAT_DB}"
    return input
  end

  (msg = msgcat[locale][input]) ? msg : input
end