Module: Zen::Language::SingletonMethods

Defined in:
lib/zen/language.rb

Overview

:nodoc:

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Mixed) lang(key, lang = nil)

Retrieves a single language string.

Examples:

lang('users.titles.index')

Parameters:

  • key (String)

    The language key to retrieve.

  • lang (String) (defaults to: nil)

    The language for which to retrieve the key, overwrites the language set in the session.

Returns:

  • (Mixed)

Raises:

Since:

  • 0.2



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/zen/language.rb', line 388

def lang(key, lang = nil)
  unless key =~ Zen::Language::Translation::KEY_REGEX
    raise(
      Zen::LanguageError,
      "The format of the language key \"#{key}\" is invalid"
    )
  end

  lang   = Zen::Language.current.name if lang.nil?
  key    = key.to_s  unless key.is_a?(String)
  lang   = lang.to_s unless lang.is_a?(String)

  unless Zen::Language::REGISTERED.key?(lang)
    raise(Zen::LanguageError, "The language \"#{lang}\" doesn't exist")
  end

  group     = key.split('.')[0]
  cache_key = [lang, key].join('.')
  got       = Ramaze::Cache.translations.fetch(cache_key)

  return got unless got.nil?

  # It seems the language item couldn't be fetched. This can happen when
  # the user change the language but hasn't loaded a specific language set
  # yet. Lets load it and try again.
  Zen::Language.load(group, lang)

  got = Ramaze::Cache.translations.fetch(cache_key)

  return got unless got.nil?

  # Last step: check if the cache has been cleared. If this is the case
  # the language pack should be reloaded, otherwise an error will be
  # raised.
  REGISTERED[lang].collections[group].load

  got = Ramaze::Cache.translations.fetch(cache_key)

  unless got.nil?
    Ramaze::Log.warn(
      "The language item \"#{key}\" has been added to the cache as it " \
        'appeared to be missing'
    )

    return got
  end

  raise(
    Zen::LanguageError,
    "The specified language item \"#{key}\" does not exist"
  )
end