Skip to content

Instantly share code, notes, and snippets.

@mikaa123
Created November 19, 2018 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikaa123/bcd68b2e704e28c393dfd5e02c18fe7b to your computer and use it in GitHub Desktop.
Save mikaa123/bcd68b2e704e28c393dfd5e02c18fe7b to your computer and use it in GitHub Desktop.
Detect language
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