Created
November 19, 2018 15:31
-
-
Save mikaa123/bcd68b2e704e28c393dfd5e02c18fe7b to your computer and use it in GitHub Desktop.
Detect language
This file contains hidden or 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
const LanguageDetect = require('languagedetect'); | |
const lngDetector = new LanguageDetect(); | |
const AlgoliaIndexTransform = require('algolia-index-transform'); | |
const algoliaIndexTransform = new AlgoliaIndexTransform({ | |
sourceApplicationID: 'source_app_id', | |
sourceApiKey: 'source_key_here', | |
sourceIndexName: 'site-search', | |
destinationApplicationID: 'dest_app_id', | |
destinationApiKey: 'dest_key_here', | |
destinationIndexName: 'site-search', | |
}); | |
algoliaIndexTransform.map(item => { | |
if (item.type !== 'PDF') { | |
return { | |
...item, | |
}; | |
} | |
if (!item.content.length) { | |
return { | |
...item, | |
}; | |
} | |
const lang = lngDetector.detect(item.content); | |
if (!lang || !lang.length) { | |
return { | |
...item, | |
}; | |
} | |
return { | |
...item, | |
country: lang[0][0] === 'french' ? 'fr' : 'other', | |
language: lang[0][0] === 'french' ? 'fr' : 'other', | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment