Module: Slang

Extended by:
Helpers
Defined in:
lib/slang.rb,
lib/slang/railtie.rb,
lib/slang/version.rb,
lib/slang/internal.rb,
lib/slang/snapshot.rb,
lib/slang/snapshot/rules.rb,
lib/slang/snapshot/locale.rb,
lib/slang/updater/abstract.rb,
lib/slang/snapshot/template.rb,
lib/slang/snapshot/warnings.rb,
lib/slang/updater/production.rb,
lib/slang/updater/development.rb,
lib/slang/updater/squelchable.rb,
lib/slang/snapshot/translation.rb,
lib/slang/updater/http_helpers.rb,
lib/slang/updater/key_reporter.rb,
lib/slang/updater/shared_state.rb

Overview

slang-rb

The official Ruby client library for Slang(sm). getslang.com/

The MIT License (MIT)

Copyright © 2014-2015 Slang Tech Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: Helpers, Updater Classes: ConfigurationError, NetworkError, Railtie, SlangError, Snapshot, SnapshotError

Constant Summary collapse

VERSION =
"0.34.0"
USER_AGENT =
"slang-rb/#{VERSION}"
KEY_REGEX =
/\A[0-9a-zA-Z_\-]{20}\z/

Class Method Summary collapse

Methods included from Helpers

locale_code, locale_code=, t

Class Method Details

.envObject



49
50
51
# File 'lib/slang/internal.rb', line 49

def self.env
  @env
end

.env_var(var) ⇒ Object



81
82
83
84
# File 'lib/slang/internal.rb', line 81

def self.env_var(var)
  value = ENV[var]
  value if value && !value.empty?
end

.ify(config = {}) ⇒ Object

Initialize Slang. This must be called first before any other methods.

Parameters:

  • configuration (Hash{Symbol => String})

    keys and values.

Raises:



72
73
74
# File 'lib/slang.rb', line 72

def self.ify(config={})
  init(config)
end

.init(config = {}) ⇒ Object

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/slang/internal.rb', line 86

def self.init(config={})
  return false if @initialized

  api_base          = config[:api_base] ? config[:api_base].chomp("/") : nil
  data_dir          = config[:data_dir] || env_var("SLANG_DATA_DIR") || File.join(defined?(Rails) ? Rails.root : Dir.pwd, "slang_data")
  developer_key     = config[:developer_key] || env_var("SLANG_DEVELOPER_KEY")
  embedded_snapshot = config[:embedded_snapshot] || env_var("SLANG_EMBEDDED_SNAPSHOT")
  env               = config[:env] || env_var("SLANG_ENV") || (defined?(Rails) ? Rails.env : nil) || (config[:developer_key] ? "development" : "production")
  project_key       = config[:project_key] || env_var("SLANG_PROJECT_KEY")

  @env = env
  dev_mode = @env =~ /\Adev/i
  @logger ||= defined?(Rails) ? Rails.logger : nil

  Slang.log_info("Slang v#{Slang::VERSION} starting in #{dev_mode ? 'development' : 'production'} mode (#{env} environment)")

  begin
    FileUtils.mkdir_p(data_dir, mode: 0755)
  rescue
    raise ConfigurationError, "unable to create data dir - #{data_dir}"
  end
  raise ConfigurationError, "missing/malformed project key (project_key=#{project_key})" unless project_key =~ KEY_REGEX

  if dev_mode
    raise ConfigurationError, "missing/malformed developer key (developer_key=#{developer_key})" unless developer_key =~ KEY_REGEX
    api_base ||= "https://dev.getslang.com"
    @updater = Updater::Development.new(env, "#{api_base}/snapshot/#{project_key}", data_dir, embedded_snapshot)
    @updater.dev_key = developer_key
    @key_reporter = Updater::KeyReporter.new("#{api_base}/keys/#{project_key}", developer_key)
  else
    api_base ||= "https://slang-a.akamaihd.net"
    @updater = Updater::Production.new(env, "#{api_base}/m/#{project_key}", data_dir, embedded_snapshot)
  end

  Slang.log_info("Slang initialized. All systems go!")
  @initialized = true

  if puma_preload?
    Puma.cli_config.options[:before_worker_boot] << Proc.new { Slang.snapshot } # delay to worker start
    # TODO: unicorn check
  else
    Slang.snapshot # trigger background fetch immediately
  end
  nil
end

.internal_translate(key, variable_hash) ⇒ Object



36
37
38
# File 'lib/slang/internal.rb', line 36

def self.internal_translate(key, variable_hash)
  snapshot.translate(locale_code, key, variable_hash)
end

.key_reporterObject



45
46
47
# File 'lib/slang/internal.rb', line 45

def self.key_reporter
  @key_reporter
end

.keysArray<String>

Return all the keys in the current snapshot.

Returns:

  • (Array<String>)

    all keys in the current snapshot.

Raises:



90
91
92
# File 'lib/slang.rb', line 90

def self.keys
  snapshot.keys
end

.locale_codesArray<Symbol>

Return all the locale codes in the current snapshot.

Returns:

  • (Array<Symbol>)

    all locale codes in the current snapshot.

Raises:



81
82
83
# File 'lib/slang.rb', line 81

def self.locale_codes
  snapshot.locales.keys
end

.log_debug(message) ⇒ Object



75
76
77
78
79
# File 'lib/slang/internal.rb', line 75

def self.log_debug(message)
  if @logger
    @logger.debug("[SLANG] #{message}")
  end
end

.log_error(message) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/slang/internal.rb', line 53

def self.log_error(message)
  if @logger
    @logger.error("[SLANG] #{message}")
  elsif !defined?(Slang::TEST)
    warn(message)
  end
end

.log_info(message) ⇒ Object



69
70
71
72
73
# File 'lib/slang/internal.rb', line 69

def self.log_info(message)
  if @logger
    @logger.info("[SLANG] #{message}")
  end
end

.log_warn(message) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/slang/internal.rb', line 61

def self.log_warn(message)
  if @logger
    @logger.warn("[SLANG] #{message}")
  elsif !defined?(Slang::TEST)
    warn(message)
  end
end

.loggerObject

Returns Slang’s logger. If Rails is detected when Slang is initialized, this logger will default to Rails.logger.



99
100
101
# File 'lib/slang.rb', line 99

def self.logger
  @logger
end

.logger=(logger) ⇒ Object

Set the Logger-like instance Slang should use for logging.

Parameters:

  • logger (Logger, nil)


107
108
109
# File 'lib/slang.rb', line 107

def self.logger=(logger)
  @logger = logger
end

.snapshotObject

Raises:



40
41
42
43
# File 'lib/slang/internal.rb', line 40

def self.snapshot
  raise SlangError, "not initialized." unless @initialized
  @updater.snapshot
end