Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
feat(i18n): Add Spanish translation and i18n setup (#91)
Browse files Browse the repository at this point in the history
* deps: added y18n

* docs: a few readme tweaks + documenting setLocale

* docs: translate README to Spanish

* deps: add cross-env devDep

* feat(i18n): add Spanish localisation for messages

* fixup for docs
  • Loading branch information
zkat committed May 14, 2017
1 parent a962e98 commit 323b90c
Show file tree
Hide file tree
Showing 12 changed files with 709 additions and 25 deletions.
605 changes: 605 additions & 0 deletions README.es.md

Large diffs are not rendered by default.

26 changes: 14 additions & 12 deletions README.md
Expand Up @@ -8,6 +8,8 @@ get corrupted or manipulated.
It was originally written to be used as [npm](https://npm.im)'s local cache, but
can just as easily be used on its own

_Translations: [español](README.es.md)_

## Install

`$ npm install --save cacache`
Expand All @@ -33,6 +35,7 @@ can just as easily be used on its own
* [`rm.entry`](#rm-entry)
* [`rm.content`](#rm-content)
* Utilities
* [`setLocale`](#set-locale)
* [`clearMemoized`](#clear-memoized)
* [`tmp.mkdir`](#tmp-mkdir)
* [`tmp.withTmp`](#with-tmp)
Expand Down Expand Up @@ -105,7 +108,7 @@ Happy hacking!

### API

#### <a name="ls"></a> `> cacache.ls(cache) -> Promise`
#### <a name="ls"></a> `> cacache.ls(cache) -> Promise<Object>`

Lists info for all entries currently in the cache as a single large object. Each
entry in the object will be keyed by the unique index key, with corresponding
Expand Down Expand Up @@ -301,14 +304,6 @@ Looks up a [Subresource Integrity hash](#integrity) in the cache. If content
exists for this `integrity`, it will return an object, with the specific single integrity hash
that was found in `sri` key, and the size of the found content as `size`. If no content exists for this integrity, it will return `false`.

##### Fields

* `source` - The [Subresource Integrity hash](#integrity) that was provided as an
argument and subsequently found in the cache.
* `algorithm` - The algorithm used in the hash.
* `digest` - The digest portion of the hash.
* `options`

##### Example

```javascript
Expand Down Expand Up @@ -422,9 +417,8 @@ If `opts.memoize` is an object or a `Map`-like (that is, an object with `get`
and `set` methods), it will be written to instead of the global memoization
cache.

Reading from existing memoized data can be forced by explicitly passing
`memoize: false` to the reader functions, but their default will be to read from
memory.
Reading from disk data can be forced by explicitly passing `memoize: false` to
the reader functions, but their default will be to read from memory.

#### <a name="rm-all"></a> `> cacache.rm.all(cache) -> Promise`

Expand Down Expand Up @@ -471,6 +465,14 @@ cacache.rm.content(cachePath, 'sha512-SoMeDIGest/IN+BaSE64==').then(() => {
})
```

#### <a name="set-locale"></a> `> cacache.setLocale(locale)`

Configure the language/locale used for messages and errors coming from cacache.
The list of available locales is in the `./locales` directory in the project
root.

_Interested in contributing more languages! [Submit a PR](CONTRIBUTING.md)!_

#### <a name="clear-memoized"></a> `> cacache.clearMemoized()`

Completely resets the in-memory entry cache.
Expand Down
1 change: 1 addition & 0 deletions index.js
Expand Up @@ -6,6 +6,7 @@ module.exports = {
put: require('./put'),
rm: require('./rm'),
verify: require('./verify'),
setLocale: require('./lib/util/y.js').setLocale,
clearMemoized: require('./lib/memoization').clearMemoized,
tmp: require('./lib/util/tmp')
}
5 changes: 3 additions & 2 deletions lib/content/read.js
Expand Up @@ -7,6 +7,7 @@ const fs = require('graceful-fs')
const PassThrough = require('stream').PassThrough
const pipe = BB.promisify(require('mississippi').pipe)
const ssri = require('ssri')
const Y = require('../util/y.js')

BB.promisifyAll(fs)

Expand Down Expand Up @@ -86,15 +87,15 @@ function pickContentSri (cache, integrity) {
}

function sizeError (expected, found) {
var err = new Error('stream data size mismatch')
var err = new Error(Y`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
err.expected = expected
err.found = found
err.code = 'EBADSIZE'
return err
}

function integrityError (sri, path) {
var err = new Error(`Integrity verification failed for ${sri} (${path})`)
var err = new Error(Y`Integrity verification failed for ${sri} (${path})`)
err.code = 'EINTEGRITY'
err.sri = sri
err.path = path
Expand Down
11 changes: 7 additions & 4 deletions lib/content/write.js
Expand Up @@ -13,6 +13,7 @@ const rimraf = BB.promisify(require('rimraf'))
const ssri = require('ssri')
const to = require('mississippi').to
const uniqueFilename = require('unique-filename')
const Y = require('../util/y.js')

const writeFileAsync = BB.promisify(fs.writeFile)

Expand All @@ -21,7 +22,7 @@ function write (cache, data, opts) {
opts = opts || {}
if (opts.algorithms && opts.algorithms.length > 1) {
throw new Error(
'opts.algorithms only supports a single algorithm for now'
Y`opts.algorithms only supports a single algorithm for now`
)
}
if (typeof opts.size === 'number' && data.length !== opts.size) {
Expand Down Expand Up @@ -58,7 +59,7 @@ function writeStream (cache, opts) {
}, cb => {
inputStream.end(() => {
if (!allDone) {
const e = new Error('Input stream was empty')
const e = new Error(Y`Cache input stream was empty`)
e.code = 'ENODATA'
return ret.emit('error', e)
}
Expand Down Expand Up @@ -143,15 +144,17 @@ function moveToDestination (tmp, cache, sri, opts, errCheck) {
}

function sizeError (expected, found) {
var err = new Error('stream data size mismatch')
var err = new Error(Y`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
err.expected = expected
err.found = found
err.code = 'EBADSIZE'
return err
}

function checksumError (expected, found) {
var err = new Error('checksum failed')
var err = new Error(Y`Integrity check failed:
Wanted: ${expected}
Found: ${found}`)
err.code = 'EINTEGRITY'
err.expected = expected
err.found = found
Expand Down
3 changes: 2 additions & 1 deletion lib/entry-index.js
Expand Up @@ -10,6 +10,7 @@ const hashToSegments = require('./util/hash-to-segments')
const ms = require('mississippi')
const path = require('path')
const ssri = require('ssri')
const Y = require('./util/y.js')

const indexV = require('../package.json')['cache-version'].index

Expand All @@ -21,7 +22,7 @@ const from = ms.from

module.exports.NotFoundError = class NotFoundError extends Error {
constructor (cache, key) {
super('content not found')
super(Y`No cache entry for \`${key}\` found in \`${cache}\``)
this.code = 'ENOENT'
this.cache = cache
this.key = key
Expand Down
25 changes: 25 additions & 0 deletions lib/util/y.js
@@ -0,0 +1,25 @@
'use strict'

const path = require('path')
const y18n = require('y18n')({
directory: path.join(__dirname, '../../locales'),
locale: 'en',
updateFiles: process.env.CACACHE_UPDATE_LOCALE_FILES === 'true'
})

module.exports = yTag
function yTag (parts) {
let str = ''
parts.forEach((part, i) => {
const arg = arguments[i + 1]
str += part
if (arg) {
str += '%s'
}
})
return y18n.__.apply(null, [str].concat([].slice.call(arguments, 1)))
}

module.exports.setLocale = locale => {
y18n.setLocale(locale)
}
6 changes: 6 additions & 0 deletions locales/en.json
@@ -0,0 +1,6 @@
{
"No cache entry for `%s` found in `%s`": "No cache entry for %s found in %s",
"Integrity verification failed for %s (%s)": "Integrity verification failed for %s (%s)",
"Bad data size: expected inserted data to be %s bytes, but got %s instead": "Bad data size: expected inserted data to be %s bytes, but got %s instead",
"Cache input stream was empty": "Cache input stream was empty"
}
6 changes: 6 additions & 0 deletions locales/es.json
@@ -0,0 +1,6 @@
{
"No cache entry for `%s` found in `%s`": "No existe ninguna entrada para «%s» en «%s»",
"Integrity verification failed for %s (%s)": "Verificación de integridad falló para «%s» (%s)",
"Bad data size: expected inserted data to be %s bytes, but got %s instead": "Tamaño incorrecto de datos: los datos insertados debieron haber sido %s octetos, pero fueron %s",
"Cache input stream was empty": "El stream de entrada al caché estaba vacío"
}
35 changes: 33 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -9,15 +9,16 @@
"main": "index.js",
"files": [
"*.js",
"lib"
"lib",
"locales"
],
"scripts": {
"benchmarks": "node test/benchmarks",
"prerelease": "npm t",
"postrelease": "npm publish && git push --follow-tags",
"pretest": "standard lib test *.js",
"release": "standard-version -s",
"test": "nyc --all -- tap -J test/*.js",
"test": "cross-env CACACHE_UPDATE_LOCALE_FILES=true nyc --all -- tap -J test/*.js",
"test-docker": "docker run -it --rm --name pacotest -v \"$PWD\":/tmp -w /tmp node:latest npm test",
"update-coc": "weallbehave -o . && git add CODE_OF_CONDUCT.md && git commit -m 'docs(coc): updated CODE_OF_CONDUCT.md'",
"update-contrib": "weallcontribute -o . && git add CONTRIBUTING.md && git commit -m 'docs(contributing): updated CONTRIBUTING.md'"
Expand Down Expand Up @@ -68,11 +69,13 @@
"promise-inflight": "^1.0.1",
"rimraf": "^2.6.1",
"ssri": "^4.1.2",
"unique-filename": "^1.1.0"
"unique-filename": "^1.1.0",
"y18n": "^3.2.1"
},
"devDependencies": {
"benchmark": "^2.1.4",
"chalk": "^1.1.3",
"cross-env": "^5.0.0",
"nyc": "^10.2.0",
"require-inject": "^1.4.0",
"safe-buffer": "^5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion test/rm.js
Expand Up @@ -38,7 +38,7 @@ test('rm.entry removes entries, not content', t => {
}).then(res => {
throw new Error('unexpected success')
}).catch({code: 'ENOENT'}, err => {
t.match(err.message, /not found/, 'entry no longer accessible')
t.match(err.message, KEY, 'entry no longer accessible')
}).then(() => {
return fs.readFileAsync(contentPath(CACHE, INTEGRITY))
}).then(data => {
Expand Down

0 comments on commit 323b90c

Please sign in to comment.