import i18next from 'i18next'
import t from './t'

// Formats a pluralized string
const tPlural = (messages, options = {}) => {
  if (!Number.isInteger(options.count)) {
    console.error('[react-globe] tPlural requires a integer "count" option')
    return null
  }

  if (!messages.one || !messages.many) {
    console.error('[react-globe] tPlural requires a "one" and "many" strings')
    return null
  }

  // Manually generate english pluralisation based on gettext style
  if (i18next.language === 'en' || !i18next.exists(messages.one)) {
    return englishPlural(messages, options)
  }

  // Return an extra string for 0 counts
  if (options.count === 0) {
    return t(messages.zero || messages.one, options)
  }

  // The translation function figures out plurals for us
  return t(messages.one, options)
}

const englishPlural = (messages, options) => {
  if (options.count === 0) {
    return t(messages.zero || messages.many, options)
  }

  if (options.count === 1) {
    return t(messages.one, options)
  }

  return t(messages.many, options)
}

export default tPlural

@@ -398,24 +398,99 @@
 n null%0A  %7D%0A%0A
+  if (options.count === 0) %7B%0A    return handleZero(messages, options)%0A  %7D%0A%0A
   // Manuall
@@ -583,9 +583,9 @@
 ' %7C%7C
-%C2%A0
+ 
 !i18
@@ -608,16 +608,25 @@
 ages.one
+, options
 )) %7B%0A   
@@ -680,75 +680,60 @@
  // 
-Return an extra string for 0 counts%0A  if (options.c
+The translation function figures 
 ou
-n
 t 
-=== 0) %7B%0A  
+plurals for us%0A
   re
@@ -748,32 +748,61 @@
 essages.
-z
+one, options)%0A%7D%0A%0Aconst handleZ
 ero 
-%7C%7C 
+= (
 messages
 .one, op
@@ -785,36 +785,32 @@
 Zero = (messages
-.one
 , options)%0A  %7D%0A%0A
@@ -807,222 +807,368 @@
 ons)
-%0A  %7D%0A%0A  // The translation function figures out plurals for us%0A  return t(messages.one, options)%0A%7D%0A%0Aconst englishPlural = (messages, options) =%3E %7B%0A  if (options.count === 0) %7B%0A    return t(messages.zero %7C%7C 
+ =%3E %7B%0A  // When the language is no english, fallback to the 'one' string,%0A  // because the %22t%22 translation function will pick the correct plural that way%0A  var fallback = i18next.language === 'en' %7C%7C !i18next.exists(messages.one, options)%0A    ? messages.many%0A    : messages.one%0A%0A  return t(messages.zero %7C%7C fallback, options)%0A%7D%0A%0Aconst englishPlural = (
 messages
 .man
@@ -1155,37 +1155,32 @@
 ural = (messages
-.many
 , options)%0A  %7D%0A%0A
@@ -1173,21 +1173,21 @@
 options)
-%0A  %7D%0A
+ =%3E %7B
 %0A  if (o

Fix pluralisation for missing keys