translator
Version:
[ABANDONED] Translator for node and also for browser
205 lines (137 loc) • 5.67 kB
text/coffeescript
if typeof window != 'undefined'
throw new Error 'Translator API can not be used in browser.'
Loader = require './Loaders/Loader'
Translator = require './Translator'
fs = require 'fs'
path = require 'path'
callsite = require 'callsite'
Finder = require 'fs-finder'
class Api
configPath: null
config: null
translator: null
loader: null
language: 'en'
languages: null
constructor: (, = ) ->
if .charAt(0) == '.'
stack = callsite()
= path.join(path.dirname(stack[1].getFileName()), )
= path.normalize()
= JSON.parse(fs.readFileSync(, encoding: 'utf8'))
if typeof .path == 'undefined'
.path = '.'
if typeof .loader == 'undefined'
.loader = 'Json'
if typeof .languages == 'undefined'
.languages = []
if .path.charAt(0) == '.'
.path = path.join(path.dirname(), .path)
= new Translator()
= new (require './Loaders/' + .loader)(.path)
release: ->
= null
getLanguages: ->
if == null
= []
for file in Finder.from(.path).findFiles('.*.json')
language = path.basename(file).split('.')[0]
if .indexOf(language) == -1
.push(language)
for language in .languages
if .indexOf(language) == -1
.push(language)
return
hasLanguage: (language) ->
return ().indexOf(language) != -1
addLanguage: (language) ->
if !(language)
.push(language)
.languages.push(language)
fs.writeFileSync(, JSON.stringify(, null, '\t'))
getDictionaries: ->
result = []
for file in Finder.from(.path).findFiles( + '.*.json')
name = path.relative(.path, file)
dir = path.dirname(name)
name = path.basename(name, path.extname(name)).replace(new RegExp('^' + + '\.'), '')
result.push(path.join(dir, name))
return (result)
addDictionary: (dictionary) ->
info = .getMessageInfo(dictionary + '.buf')
_path = .getFileSystemPath(info.path, info.category, )
if fs.existsSync(_path)
throw new Error "Dictionary '#{dictionary}' already exists."
fs.writeFileSync(_path, '{}')
renameDictionary: (oldName, newName) ->
info = .getMessageInfo(oldName + '.buf')
_path = .getFileSystemPath(info.path, info.category, )
if !fs.existsSync(_path)
throw new Error "Dictionary '#{oldName}' does not exists."
newInfo = .getMessageInfo(newName + '.buf')
newPath = .getFileSystemPath(newInfo.path, newInfo.category, )
if fs.existsSync(newPath)
throw new Error "Dictionary '#{newName}' already exists."
fs.renameSync(_path, newPath)
removeDictionary: (dictionary) ->
info = .getMessageInfo(dictionary + '.buf')
_path = .getFileSystemPath(info.path, info.category, )
if !fs.existsSync(_path)
throw new Error "Dictionary '#{dictionary}' does not exists."
fs.unlinkSync(_path)
getTranslations: (dictionary) ->
info = .getMessageInfo(dictionary + '.buf')
files = .loadCategory(info.path, info.category, )
_path = .getFileSystemPath(info.path, info.category, )
delete require.cache[_path]
return files
addTranslation: (dictionary, name, translation) ->
info = .getMessageInfo(dictionary + '.' + name)
_path = .getFileSystemPath(info.path, info.category, )
data = (dictionary)
if typeof data[info.name] != 'undefined'
throw new Error "Translation '#{name}' already exists in '#{dictionary}' dictionary."
data[info.name] = translation
data = JSON.stringify(data, null, '\t')
fs.writeFileSync(_path, data)
editTranslation: (dictionary, name, translation) ->
info = .getMessageInfo(dictionary + '.' + name)
_path = .getFileSystemPath(info.path, info.category, )
data = (dictionary)
if typeof data[info.name] == 'undefined'
throw new Error "Translation '#{name}' does not exists in '#{dictionary}' dictionary."
data[info.name] = translation
data = JSON.stringify(data, null, '\t')
fs.writeFileSync(_path, data)
renameTranslation: (dictionary, oldName, newName) ->
info = .getMessageInfo(dictionary + '.' + oldName)
_path = .getFileSystemPath(info.path, info.category, )
data = (dictionary)
if typeof data[info.name] == 'undefined'
throw new Error "Translation '#{oldName}' does not exists in '#{dictionary}' dictionary."
if typeof data[newName] != 'undefined'
throw new Error "Translation '#{newName}' already exists in '#{dictionary}' dictionary."
data[newName] = data[oldName]
delete data[oldName]
data = JSON.stringify(data, null, '\t')
fs.writeFileSync(_path, data)
removeTranslation: (dictionary, name) ->
info = .getMessageInfo(dictionary + '.' + name)
_path = .getFileSystemPath(info.path, info.category, )
data = (dictionary)
delete data[name]
data = JSON.stringify(data, null, '\t')
fs.writeFileSync(_path, data)
createTree: (paths) ->
result = {}
count = 0
for _path in paths
parts = _path.split(path.sep)
buf = result
for part in parts
if typeof buf[part] == 'undefined'
buf[part] = {}
buf = buf[part]
count++
return result
module.exports = Api