Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class Glossary {
this._entries[term] = description
}

fromFile (filename) {
let glossary = require(filename)
assert(Object.keys(glossary).length, 'The file seems to be empty')
glossary.forEach(term => {
assert(term, 'Term or description not found')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert is not necessary. This is already handled by the add method.

this.add(term[0], term[1])
})
this._entries = glossary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._entries = glossary is not necessary. This line can be removed.

}

get entries () {
return this._entries
}
Expand Down
22 changes: 19 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# crowdin-glossary
# crowdin-glossary

Create and upload translation glossaries using the Crowdin API

Expand All @@ -17,7 +17,6 @@ and
npm install crowdin-glossary --save
```


## Usage

```js
Expand Down Expand Up @@ -51,6 +50,13 @@ call `glossary.upload()`
- `term` String (required)
- `description` String (required)

### `glossary.fromFile(filename)`

Uploads entries from the file provided. Entries only exist in memory until you
call `glossary.upload()`

- `filename` String (required)(should be the relative path to the .json file)

### `glossary.upload()`

Async function that uploads all the added terms to Crowdin.
Expand All @@ -67,9 +73,19 @@ A getter that returns the web URL of your project's glossary on crowdin.com

### `glossary.csv`

A getter that converts your entries into a valid CSV string for upload to
A getter that converts your entries into a valid CSV string for upload to
Crowdin. Used for internal purposes.

#### `.json` file template

```json
[
[$term, $description]
]
```

A 2D Array of term and description

## License

MIT
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ describe('glossary.add()', () => {
})
})

describe('glossary.fromFile()', () => {
const glossary = Glossary({project: 'bar', crowdinKey: 'xyz'})

test('is empty', () => {
expect(() => {
glossary.fromFile('./test/empty-file.json')
}).toThrow('The file seems to be empty')
})

test('upload from a file', () => {
expect(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be something like:

glossary.fromFile('./test/glossary_test.json')
expect(glossary.entries.length).toEq(2)

glossary.fromFile('./test/glossary_test.json')
})
})
})

describe('glossary.webpage', () => {
test('is a crowdin URL', () => {
const glossary = Glossary({project: 'foo-bar', crowdinKey: 'xyz'})
Expand Down
3 changes: 3 additions & 0 deletions test/empty-file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs and the implementation expect an array of arrays, not an object.


}
4 changes: 4 additions & 0 deletions test/glossary_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be an array, not an object.

"something": "this is something",
"other": "this is other"
}
Loading