Class: Showoff::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/showoff/locale.rb

Constant Summary collapse

@@contentLocale =
nil

Class Method Summary collapse

Class Method Details

.contentLanguagesObject

Generates a hash of all language codes available and the long name description of each



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/showoff/locale.rb', line 79

def self.contentLanguages
  root = Showoff::Config.root

  strings = JSON.parse(File.read("#{root}/locales/strings.json")) rescue {}
  locales = Dir.glob("#{root}/locales/*")
               .select {|f| File.directory?(f) }
               .map    {|f| File.basename(f)   }

  (strings.keys + locales).inject({}) do |memo, locale|
    memo.update(locale => languageName(locale))
  end
end

.contentLocaleObject



37
38
39
# File 'lib/showoff/locale.rb', line 37

def self.contentLocale
  @@contentLocale
end

.contentPathObject



67
68
69
70
71
72
73
74
# File 'lib/showoff/locale.rb', line 67

def self.contentPath
  root = Showoff::Config.root

  with_locale(contentLocale) do |str|
    path = "#{root}/locales/#{str}"
    return path if File.directory?(path)
  end || root
end

.languageName(locale = contentLocale) ⇒ Object

Turns a locale code into a string name

Parameters:

  • locale (String, Symbol) (defaults to: contentLocale)

    The code of the locale to translate



55
56
57
58
59
60
# File 'lib/showoff/locale.rb', line 55

def self.languageName(locale = contentLocale)
  with_locale(locale) do |str|
    result = ISO_639.find(str)
    result[3] unless result.nil?
  end
end

.resolve(items) ⇒ Symbol

Find the closest match to current locale in an array of possibilities

Parameters:

  • items (Array)

    An array of possibilities to check

Returns:

  • (Symbol)

    The closest match to the current locale.



45
46
47
48
49
# File 'lib/showoff/locale.rb', line 45

def self.resolve(items)
  with_locale(contentLocale) do |str|
    str.to_sym if items.include? str
  end
end

.setContentLocale(user_locale = nil) ⇒ Object

Set the minimized canonical version of the specified content locale, selecting the nearest match to whatever exists in the presentation’s locales directory. If the locale doesn’t exist on disk, it will just default to no translation

@todo: I don’t think this is right at all – it doesn’t autoselect content

languages, just built in Showoff languages. It only worked by accident before

Parameters:

  • user_locale (String, Symbol) (defaults to: nil)

    The locale to select.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/showoff/locale.rb', line 23

def self.setContentLocale(user_locale = nil)
  if [nil, '', 'auto'].include? user_locale
    languages = I18n.available_locales
    @@contentLocale = I18n.fallbacks[I18n.locale].select { |f| languages.include? f }.first
  else
    locales = Dir.glob("#{Showoff::Config.root}/locales/*").map {|e| File.basename e }
    locales.delete 'strings.json'

    @@contentLocale = with_locale(user_locale) do |str|
      str.to_sym if locales.include? str
    end
  end
end

.translationsObject

Generates a hash of all translations for the current language. This is used for the javascript half of the UI translations



97
98
99
100
101
# File 'lib/showoff/locale.rb', line 97

def self.translations
  languages = I18n.backend.send(:translations)
  fallback  = I18n.fallbacks[I18n.locale].select { |f| languages.keys.include? f }.first
  languages[fallback]
end

.userTranslationsObject

Finds the language key from strings.json and returns the strings hash. This is used for user translations in the presentation content, e.g. SVG translations.



107
108
109
110
111
112
113
114
115
116
# File 'lib/showoff/locale.rb', line 107

def self.userTranslations
  path = "#{Showoff::Config.root}/locales/strings.json"
  return {} unless File.file? path
  strings = JSON.parse(File.read(path)) rescue {}

  with_locale(contentLocale) do |key|
    return strings[key] if strings.include? key
  end
  {}
end