Lang.purs 3.42 KB
Newer Older
1
module Gargantext.Components.Lang where
2

3 4
import Gargantext.Prelude

5
import Data.Argonaut (class EncodeJson, encodeJson)
6
import Data.Array as A
7 8
import Data.Generic.Rep (class Generic)
import Data.Lens.Lens.Product (_1)
9
import Data.Maybe (Maybe(..))
10
import Data.Newtype (class Newtype)
11
import Data.Show.Generic (genericShow)
12
import Foreign as F
13
import Gargantext.Utils.Reactix as R2
14
import Gargantext.Utils.Show as GUS
15
import Gargantext.Utils.SimpleJSON as GJSON
16 17
import Reactix as R
import Reactix.DOM.HTML as H
18
import Simple.JSON as JSON
19 20 21 22
import Toestand as T

here :: R2.Here
here = R2.here "Gargantext.Components.Lang"
23 24 25

-- Language used for search
allLangs :: Array Lang
Alexandre Delanoë's avatar
Alexandre Delanoë committed
26 27
allLangs = [ Universal
           , No_extraction
28
           , DE
Alexandre Delanoë's avatar
Alexandre Delanoë committed
29 30
           , EL
           , EN
31
           , ES
Alexandre Delanoë's avatar
Alexandre Delanoë committed
32
           , FR
33 34
           , IT
           , PL
Alexandre Delanoë's avatar
Alexandre Delanoë committed
35 36 37 38
           , PT
           , RU
           , UK
           , ZH
39 40
           ]

Alexandre Delanoë's avatar
Alexandre Delanoë committed
41 42 43 44 45 46 47 48 49 50 51 52
data Lang = Universal | No_extraction
          | DE
          | EL
          | EN
          | ES
          | FR
          | IT
          | PL
          | PT
          | RU
          | UK
          | ZH
53
derive instance Generic Lang _
54
derive instance Ord Lang
55

56
instance Show Lang where
57
  show Universal     = "All"
58
  show No_extraction = "Nothing"
59
  show s             = genericShow s
60

61 62
langReader :: String -> Maybe Lang
langReader = GUS.reader allLangs
63

64
derive instance Eq Lang
65

66 67
-- instance EncodeJson Lang where
--   encodeJson a = encodeJson (show a)
68

69 70 71 72 73 74
instance JSON.ReadForeign Lang where
  readImpl f = do
    f' <- JSON.readImpl f
    case langReader f' of
      Nothing -> GJSON.throwJSONError $ F.ForeignError $ "Unknown language: " <> f'
      Just l  -> pure l
75 76 77
instance JSON.WriteForeign Lang where
  writeImpl l = JSON.writeImpl $ show l

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

data ServerType = CoreNLP | Spacy | JohnSnow
derive instance Generic ServerType _
instance Show ServerType where
  show = genericShow
instance JSON.ReadForeign ServerType where
  readImpl f = do
    f' <- JSON.readImpl f
    case f' of
      "CoreNLP"        -> pure CoreNLP
      "Spacy"          -> pure Spacy
      "JohnSnowServer" -> pure JohnSnow
      _                -> GJSON.throwJSONError $ F.ForeignError $ "Unknown server: " <> f'


93 94
-- Language used for the landing page
data LandingLang = LL_EN | LL_FR
95

96 97 98 99 100 101
derive instance Eq LandingLang

instance Show LandingLang where
  show LL_EN = "EN"
  show LL_FR = "FR"

102 103 104
-- @TODO a possible method/class that a real i18n logic could later replace
class Show t <= Translate t where
  translate :: Lang -> t -> String
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

allFeLangs :: Array LandingLang
allFeLangs = [ LL_EN, LL_FR ]

type LangSwitcherProps = (
    lang  :: T.Box LandingLang
  , langs :: Array LandingLang
)

langSwitcher :: R2.Component LangSwitcherProps
langSwitcher = R.createElement langSwitcherCpt
langSwitcherCpt :: R.Component LangSwitcherProps
langSwitcherCpt = here.component "langSwitcher" cpt
  where
    cpt { lang, langs} _ = do
      currentLang <- T.useLive T.unequal lang
      let option l = H.option { value: show l} [ H.text $ show l]
      let options = map option langs

      pure $ R2.select { className: "form-control"
125
                       , defaultValue: show currentLang
126 127 128 129 130 131 132 133 134
                       , on: {change: onChange lang } } options
      where
        onChange box e = do
          let value = R.unsafeEventValue e
          let mLang = A.head $ A.filter (\l -> value == show l) langs

          case mLang of
            Nothing -> pure unit
            Just l  -> do
135
              T.write_ l box