Fuse: Searching in localized (locale) data

Created on 4 Jun 2020  路  1Comment  路  Source: krisk/Fuse

Is it possible to specify a localized (e.g. Spanish or German or any other language) input json and get search results with localized data?

question

Most helpful comment

A few options which would work right now:

(a) have distinct lists as input (one for each locale), or
(b) use logical query operators to search the appropriate locale.

For (b):

const list = [
  {
    i18n: {
      greeting: 'Hello World'
    },
    lang: 'en-US'
  },
  {
    i18n: {
      greeting: 'Hola Mundo'
    },
    lang: 'es'
  }
]

const fuse = new Fuse(list, {
  useExtendedSearch: true,
  keys: ['i18n.greeting', 'lang']
})

// Search for item where language is English *and* `greeting` is like "helo wold"
fuse.search({
  lang: "'en-US", // search exact string
  'i18.greeting': 'helo wold'
})

>All comments

A few options which would work right now:

(a) have distinct lists as input (one for each locale), or
(b) use logical query operators to search the appropriate locale.

For (b):

const list = [
  {
    i18n: {
      greeting: 'Hello World'
    },
    lang: 'en-US'
  },
  {
    i18n: {
      greeting: 'Hola Mundo'
    },
    lang: 'es'
  }
]

const fuse = new Fuse(list, {
  useExtendedSearch: true,
  keys: ['i18n.greeting', 'lang']
})

// Search for item where language is English *and* `greeting` is like "helo wold"
fuse.search({
  lang: "'en-US", // search exact string
  'i18.greeting': 'helo wold'
})
Was this page helpful?
0 / 5 - 0 ratings