Module: Localeapp::HandlebarsI18n

Defined in:
lib/localeapp-handlebars_i18n.rb,
lib/localeapp-handlebars_i18n/version.rb

Overview

A singleton module that reports missing translations found in handebars templates to Localeapp. Once you have set everything up with configure it is a simple matter of calling Localeapp::HandlebarsI18n.send_missing_translations and you are done.

Constant Summary collapse

VERSION =

The current version

'0.0.1'

Class Method Summary collapse

Class Method Details

.configure(output = $stdout, &block) ⇒ Object

configures HanldebarsI18n

Examples:

Localeapp::HandlebarsI18.configure do |config|
  config.localeapp_api_key = ENV['LOCALEAPP_API_KEY']
  config.hbs_helper = 't'
  config.hbs_load_path = Dir[File.expand_path '../support/**.hbs', __FILE__]
  config.yml_load_path = File.expand_path '../support/', __FILE__
  config.default_locale = :ru
end

Parameters:

  • output (Object) (defaults to: $stdout)

    You can pass in an object that accepts calls to puts for logging. By default $stdout will be used.

  • block (Proc)

    A configuration block



23
24
25
26
27
# File 'lib/localeapp-handlebars_i18n.rb', line 23

def configure(output = $stdout, &block)
  @output = output
  instance_eval &block if block_given?
  register_missing_translations
end

.default_localeObject

See Also:



59
60
61
# File 'lib/localeapp-handlebars_i18n.rb', line 59

def default_locale
  @default_locale ||= :en
end

.default_locale=(locale) ⇒ Object

The default locale to load when comparing handlebar translation keys and I18n translation keys. This is used when loading YAML data into I18n’s simple backend

Parameters:

  • locale (Symbol)

    the locale to load.



54
55
56
# File 'lib/localeapp-handlebars_i18n.rb', line 54

def default_locale=(locale)
  @default_locale = locale
end

.hbs_helperObject

See Also:



47
48
49
# File 'lib/localeapp-handlebars_i18n.rb', line 47

def hbs_helper
  @hbs_helper ||= 't'
end

.hbs_helper=(helper) ⇒ Object

The string name of your handlebars helper function for translation. Here is an example coffeescript handlebars helper registration that creates a helper named ‘t’ that uses I18n-js for javascript localizations. If you had a helper like the one below, you would pass ‘t’ into this method. ‘t’ is also the default so if you are already using a helper named ‘t’ you do not need to configure this.

This helper is interpolated into the regular expression used to scan for translation keys: “{{#{hbs_helper} (.*?)}}”

Examples:

Handlebars.registerHelper 't', (key) ->
  safe I18n.t(key)

Parameters:

  • helper (String)

    The name of your handlebars helper used for localization.



42
43
44
# File 'lib/localeapp-handlebars_i18n.rb', line 42

def hbs_helper=(helper)
  @hbs_helper
end

.hbs_load_pathArray

Returns the files you specified or an empty array

Returns:

  • (Array)


85
86
87
# File 'lib/localeapp-handlebars_i18n.rb', line 85

def hbs_load_path
  @hbs_load_path ||= []
end

.hbs_load_path=(files) ⇒ Object

Sets the array of handlebars templates that will be searched for localization helpers. Dir.glob is pretty dang handy here.

Parameters:

  • files (Array)

    The files to search.

See Also:



79
80
81
# File 'lib/localeapp-handlebars_i18n.rb', line 79

def hbs_load_path=(files)
  @hbs_load_path = files.flatten
end

.localeapp_api_key=(api_key) ⇒ Object

Configures localeapp to use the api key you specify when reporting missing translations.

Parameters:

  • api_key (String)

    The localeapp api key.



91
92
93
94
95
# File 'lib/localeapp-handlebars_i18n.rb', line 91

def localeapp_api_key=(api_key)
  Localeapp.configure do |config|
    config.api_key= api_key
  end
end

.send_missing_translationsObject

Note:

If you have not configured Localeapp::HandlebarsI18n you will recieve an error with an example on how to do so.

Sends any missing translations to Localeapp.



99
100
101
102
103
104
# File 'lib/localeapp-handlebars_i18n.rb', line 99

def send_missing_translations
  ensure_configured
  return if Localeapp.missing_translations[default_locale].empty?
  @output.puts "sending missing translations to localeapp"
  Localeapp::sender.post_missing_translations
end

.yml_load_pathString

This defines the yml file to load for I18n. It retuns an interpolated string of the yml_load_path value you configured and the default locale.

Returns:

  • (String)


72
73
74
# File 'lib/localeapp-handlebars_i18n.rb', line 72

def yml_load_path
  "#{@yml_load_path}/#{default_locale}.yml"
end

.yml_load_path=(dir) ⇒ Object

The directory where your locale .yml files live.

Parameters:

  • dir (String)

    The directory to search for the default locale’s YAML data



65
66
67
# File 'lib/localeapp-handlebars_i18n.rb', line 65

def yml_load_path=(dir)
  @yml_load_path = dir
end