Skip to content

Commit 323b90c

Browse files
authored
feat(i18n): Add Spanish translation and i18n setup (#91)
* 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
1 parent a962e98 commit 323b90c

File tree

12 files changed

+709
-25
lines changed

12 files changed

+709
-25
lines changed

Diff for: README.es.md

+605
Large diffs are not rendered by default.

Diff for: README.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ get corrupted or manipulated.
88
It was originally written to be used as [npm](https://npm.im)'s local cache, but
99
can just as easily be used on its own
1010

11+
_Translations: [español](README.es.md)_
12+
1113
## Install
1214

1315
`$ npm install --save cacache`
@@ -33,6 +35,7 @@ can just as easily be used on its own
3335
* [`rm.entry`](#rm-entry)
3436
* [`rm.content`](#rm-content)
3537
* Utilities
38+
* [`setLocale`](#set-locale)
3639
* [`clearMemoized`](#clear-memoized)
3740
* [`tmp.mkdir`](#tmp-mkdir)
3841
* [`tmp.withTmp`](#with-tmp)
@@ -105,7 +108,7 @@ Happy hacking!
105108

106109
### API
107110

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

110113
Lists info for all entries currently in the cache as a single large object. Each
111114
entry in the object will be keyed by the unique index key, with corresponding
@@ -301,14 +304,6 @@ Looks up a [Subresource Integrity hash](#integrity) in the cache. If content
301304
exists for this `integrity`, it will return an object, with the specific single integrity hash
302305
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`.
303306

304-
##### Fields
305-
306-
* `source` - The [Subresource Integrity hash](#integrity) that was provided as an
307-
argument and subsequently found in the cache.
308-
* `algorithm` - The algorithm used in the hash.
309-
* `digest` - The digest portion of the hash.
310-
* `options`
311-
312307
##### Example
313308

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

425-
Reading from existing memoized data can be forced by explicitly passing
426-
`memoize: false` to the reader functions, but their default will be to read from
427-
memory.
420+
Reading from disk data can be forced by explicitly passing `memoize: false` to
421+
the reader functions, but their default will be to read from memory.
428422

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

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

468+
#### <a name="set-locale"></a> `> cacache.setLocale(locale)`
469+
470+
Configure the language/locale used for messages and errors coming from cacache.
471+
The list of available locales is in the `./locales` directory in the project
472+
root.
473+
474+
_Interested in contributing more languages! [Submit a PR](CONTRIBUTING.md)!_
475+
474476
#### <a name="clear-memoized"></a> `> cacache.clearMemoized()`
475477

476478
Completely resets the in-memory entry cache.

Diff for: index.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = {
66
put: require('./put'),
77
rm: require('./rm'),
88
verify: require('./verify'),
9+
setLocale: require('./lib/util/y.js').setLocale,
910
clearMemoized: require('./lib/memoization').clearMemoized,
1011
tmp: require('./lib/util/tmp')
1112
}

Diff for: lib/content/read.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fs = require('graceful-fs')
77
const PassThrough = require('stream').PassThrough
88
const pipe = BB.promisify(require('mississippi').pipe)
99
const ssri = require('ssri')
10+
const Y = require('../util/y.js')
1011

1112
BB.promisifyAll(fs)
1213

@@ -86,15 +87,15 @@ function pickContentSri (cache, integrity) {
8687
}
8788

8889
function sizeError (expected, found) {
89-
var err = new Error('stream data size mismatch')
90+
var err = new Error(Y`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
9091
err.expected = expected
9192
err.found = found
9293
err.code = 'EBADSIZE'
9394
return err
9495
}
9596

9697
function integrityError (sri, path) {
97-
var err = new Error(`Integrity verification failed for ${sri} (${path})`)
98+
var err = new Error(Y`Integrity verification failed for ${sri} (${path})`)
9899
err.code = 'EINTEGRITY'
99100
err.sri = sri
100101
err.path = path

Diff for: lib/content/write.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const rimraf = BB.promisify(require('rimraf'))
1313
const ssri = require('ssri')
1414
const to = require('mississippi').to
1515
const uniqueFilename = require('unique-filename')
16+
const Y = require('../util/y.js')
1617

1718
const writeFileAsync = BB.promisify(fs.writeFile)
1819

@@ -21,7 +22,7 @@ function write (cache, data, opts) {
2122
opts = opts || {}
2223
if (opts.algorithms && opts.algorithms.length > 1) {
2324
throw new Error(
24-
'opts.algorithms only supports a single algorithm for now'
25+
Y`opts.algorithms only supports a single algorithm for now`
2526
)
2627
}
2728
if (typeof opts.size === 'number' && data.length !== opts.size) {
@@ -58,7 +59,7 @@ function writeStream (cache, opts) {
5859
}, cb => {
5960
inputStream.end(() => {
6061
if (!allDone) {
61-
const e = new Error('Input stream was empty')
62+
const e = new Error(Y`Cache input stream was empty`)
6263
e.code = 'ENODATA'
6364
return ret.emit('error', e)
6465
}
@@ -143,15 +144,17 @@ function moveToDestination (tmp, cache, sri, opts, errCheck) {
143144
}
144145

145146
function sizeError (expected, found) {
146-
var err = new Error('stream data size mismatch')
147+
var err = new Error(Y`Bad data size: expected inserted data to be ${expected} bytes, but got ${found} instead`)
147148
err.expected = expected
148149
err.found = found
149150
err.code = 'EBADSIZE'
150151
return err
151152
}
152153

153154
function checksumError (expected, found) {
154-
var err = new Error('checksum failed')
155+
var err = new Error(Y`Integrity check failed:
156+
Wanted: ${expected}
157+
Found: ${found}`)
155158
err.code = 'EINTEGRITY'
156159
err.expected = expected
157160
err.found = found

Diff for: lib/entry-index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const hashToSegments = require('./util/hash-to-segments')
1010
const ms = require('mississippi')
1111
const path = require('path')
1212
const ssri = require('ssri')
13+
const Y = require('./util/y.js')
1314

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

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

2223
module.exports.NotFoundError = class NotFoundError extends Error {
2324
constructor (cache, key) {
24-
super('content not found')
25+
super(Y`No cache entry for \`${key}\` found in \`${cache}\``)
2526
this.code = 'ENOENT'
2627
this.cache = cache
2728
this.key = key

Diff for: lib/util/y.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const y18n = require('y18n')({
5+
directory: path.join(__dirname, '../../locales'),
6+
locale: 'en',
7+
updateFiles: process.env.CACACHE_UPDATE_LOCALE_FILES === 'true'
8+
})
9+
10+
module.exports = yTag
11+
function yTag (parts) {
12+
let str = ''
13+
parts.forEach((part, i) => {
14+
const arg = arguments[i + 1]
15+
str += part
16+
if (arg) {
17+
str += '%s'
18+
}
19+
})
20+
return y18n.__.apply(null, [str].concat([].slice.call(arguments, 1)))
21+
}
22+
23+
module.exports.setLocale = locale => {
24+
y18n.setLocale(locale)
25+
}

Diff for: locales/en.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"No cache entry for `%s` found in `%s`": "No cache entry for %s found in %s",
3+
"Integrity verification failed for %s (%s)": "Integrity verification failed for %s (%s)",
4+
"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",
5+
"Cache input stream was empty": "Cache input stream was empty"
6+
}

Diff for: locales/es.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"No cache entry for `%s` found in `%s`": "No existe ninguna entrada para «%s» en «%s»",
3+
"Integrity verification failed for %s (%s)": "Verificación de integridad falló para «%s» (%s)",
4+
"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",
5+
"Cache input stream was empty": "El stream de entrada al caché estaba vacío"
6+
}

Diff for: package-lock.json

+33-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
"main": "index.js",
1010
"files": [
1111
"*.js",
12-
"lib"
12+
"lib",
13+
"locales"
1314
],
1415
"scripts": {
1516
"benchmarks": "node test/benchmarks",
1617
"prerelease": "npm t",
1718
"postrelease": "npm publish && git push --follow-tags",
1819
"pretest": "standard lib test *.js",
1920
"release": "standard-version -s",
20-
"test": "nyc --all -- tap -J test/*.js",
21+
"test": "cross-env CACACHE_UPDATE_LOCALE_FILES=true nyc --all -- tap -J test/*.js",
2122
"test-docker": "docker run -it --rm --name pacotest -v \"$PWD\":/tmp -w /tmp node:latest npm test",
2223
"update-coc": "weallbehave -o . && git add CODE_OF_CONDUCT.md && git commit -m 'docs(coc): updated CODE_OF_CONDUCT.md'",
2324
"update-contrib": "weallcontribute -o . && git add CONTRIBUTING.md && git commit -m 'docs(contributing): updated CONTRIBUTING.md'"
@@ -68,11 +69,13 @@
6869
"promise-inflight": "^1.0.1",
6970
"rimraf": "^2.6.1",
7071
"ssri": "^4.1.2",
71-
"unique-filename": "^1.1.0"
72+
"unique-filename": "^1.1.0",
73+
"y18n": "^3.2.1"
7274
},
7375
"devDependencies": {
7476
"benchmark": "^2.1.4",
7577
"chalk": "^1.1.3",
78+
"cross-env": "^5.0.0",
7679
"nyc": "^10.2.0",
7780
"require-inject": "^1.4.0",
7881
"safe-buffer": "^5.0.1",

Diff for: test/rm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test('rm.entry removes entries, not content', t => {
3838
}).then(res => {
3939
throw new Error('unexpected success')
4040
}).catch({code: 'ENOENT'}, err => {
41-
t.match(err.message, /not found/, 'entry no longer accessible')
41+
t.match(err.message, KEY, 'entry no longer accessible')
4242
}).then(() => {
4343
return fs.readFileAsync(contentPath(CACHE, INTEGRITY))
4444
}).then(data => {

0 commit comments

Comments
 (0)