karaxnim / karax

Karax. Single page applications for Nim.
MIT License
1.05k stars 90 forks source link

We can now use karax/languages from the native backend #265

Closed planetis-m closed 11 months ago

planetis-m commented 11 months ago

There is no reason not to share the Language enum between backend and frontend. With this change this is possible. You can have something like this in your server:

const
  defaultLanguage = Language.enUS

proc parseLanguage(s: string): Language =
  let i = binarySearch(languageToCode, s)
  if i >= 0: result = Language(i)
  else: result = defaultLanguage

proc getLanguage(url: Uri): Language =
  var lang = ""
  for key, val in decodeQuery(url.query):
    if key == "lang":
      lang = val
      break
  result = parseLanguage(lang)

The lang=key is send from the frontend like this:

 ajaxGet("/route?lang=" & languageToCode[getCurrentLanguage()],
                  {cstring"Accept": cstring"application/json"}, onRoute)