-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 'feature/direct-import-translations' from 'develop'
Import directly from translations and use new tree-house-errors version
- Loading branch information
Showing
5 changed files
with
77 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as icappsTranslation from 'tree-house-translations'; | ||
import { existsSync, mkdirSync } from 'fs'; | ||
|
||
const defaultOptions = <any> { | ||
destination: process.cwd() + '/locales', | ||
clean: true, | ||
seperateCategories: false, | ||
exportType: 'json', | ||
}; | ||
|
||
|
||
/** | ||
* Import translations from the icapps translations service | ||
*/ | ||
export async function importTranslations(url: string, token: string, options?: TranslationOptions) { | ||
const allOptions = Object.assign({}, defaultOptions, options); | ||
|
||
// if locales location does not exists, make directory | ||
if (!existsSync(allOptions.destination)) { | ||
mkdirSync(allOptions.destination); | ||
} | ||
|
||
return icappsTranslation.import(url, token, allOptions); | ||
} | ||
|
||
export interface TranslationOptions { | ||
destination?: string; | ||
clean?: boolean; | ||
verbose?: boolean; | ||
seperateCategories?: boolean; | ||
exportType?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { importTranslations } from '../../src/lib/translator'; | ||
import { existsSync, rmdirSync, mkdirSync } from 'fs'; | ||
import * as icappsTranslation from 'tree-house-translations'; | ||
import * as rimraf from 'rimraf'; | ||
|
||
describe('importTranslations', () => { | ||
let icappsTranslationMock; | ||
const newDestination = './tests/locales'; | ||
|
||
beforeEach(() => { | ||
icappsTranslationMock = jest.spyOn(icappsTranslation, 'import').mockImplementation(() => { }); | ||
}); | ||
|
||
afterEach((done) => { | ||
jest.clearAllMocks(); | ||
rimraf(newDestination, done); // cleanup | ||
}); | ||
|
||
it('should import the translations', async () => { | ||
await importTranslations('http://test.be', 'randomToken', { destination: newDestination }); | ||
expect(icappsTranslationMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should use the existing locales directory', async () => { | ||
mkdirSync(newDestination); | ||
expect(existsSync(newDestination)).toEqual(true); | ||
|
||
await importTranslations('http://test.be', 'randomToken', { destination: newDestination }); | ||
expect(icappsTranslationMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should create the locales directory if not exists', async () => { | ||
const newDestination = './tests/locales'; | ||
|
||
await importTranslations('http://test.be', 'randomToken', { destination: newDestination }); | ||
expect(icappsTranslationMock).toHaveBeenCalledTimes(1); | ||
expect(existsSync(newDestination)).toEqual(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters