Module: Jekyll::Msgcat

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#__get_localeObject

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



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

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
# File 'lib/jekyll/msgcat.rb', line 12

def __get_msgcat
  return @@__msgcat if @@__msgcat

  begin
    @@__msgcat = YAML.load_file MSGCAT_DB, :safe => false
  rescue
    $stderr.puts "msgcat warning: #{MSGCAT_DB} not found"
    return nil
  end

  @@__msgcat
end

#__get_siteObject



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

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

#cur_page_in_another_locale(targetLocale, cssClass = 'btn btn-primary btn-xs') ⇒ Object



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

def cur_page_in_another_locale targetLocale, cssClass = 'btn btn-primary btn-xs'
  raise 'target locale requred' if targetLocale =~ /^\s*$/

  site = __get_site
  pattern = "<a href='%s' class='#{cssClass} %s'>#{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.



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

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