src/i18n/i18n.coffee |
|
|---|---|
This file contains the class that manage the loading, parsing and retrieving of the localized strings. Table Of Content |
yaml = require 'js-yaml' {findSync, readFilesSync} = require '../utils/files' |
I18n |
|
The |
class I18n
|
I18n::constructor |
|
The paths into which looking for files are provided in the constructor.
|
constructor: (@paths=[], @defaultLanguage='en') -> |
I18n::get |
|
Returns a string from the locales. That function can be called either with or without a language:
If the path lead to a dead end, the function return the last element in the path as a capitalized sentence.
|
get: (language, path) -> [language, path] = [@defaultLanguage, language] unless path? lang = @locales[language] throw new Error "Language #{language} not found" unless lang? els = path.split('.') (lang = lang[v]; break unless lang?) for v in els lang = els.last().replace(/[-_]/g, ' ').capitalizeAll() unless lang? lang |
I18n::getHelper |
|
Returns a helper function bound to the current instance that allow
to retrieve localized string from the
|
getHelper: -> (path, tokens) => @get(path).replace /\#\{([^\}]+)\}/g, (token, key) -> return token unless tokens[key]? tokens[key] |
I18n::load |
|
Search and loads locales files in the given paths, parse their content
and fill the |
load: -> @locales = {} docs = readFilesSync findSync 'yml', @paths @deepMerge @locales, yaml.load content.toString() for path, content of docs @languages = @locales.sortedKeys() |
I18n::deepMerge |
|
Merge two tree structures formed by nested objects into one tree structure.
|
deepMerge: (target, source) -> for k,v of source switch typeof v when 'object' if Array.isArray v target[k] ||= [] target[k] = target[k].concat v else target[k] ||= {} @deepMerge target[k], v else target[k] = v module.exports = I18n |