-- | Raises 'CSLLocaleException' on error.
getLocale :: String -> IO L.ByteString
getLocale s = do
#ifdef EMBED_DATA_FILES
  let toLazy x = L.fromChunks [x]
  case length s of
      0 -> maybe (E.throwIO $ CSLLocaleNotFound "en-US")
                 (return . toLazy)
           $ lookup "locales-en-US.xml" localeFiles
      2 -> let fn = ("locales-" ++ fromMaybe s (lookup s langBase) ++ ".xml")
           in case lookup fn localeFiles of
                Just x' -> return $ toLazy x'
                _       -> E.throwIO $ CSLLocaleNotFound s
      _ -> case lookup ("locales-" ++ take 5 s ++ ".xml") localeFiles of
                 Just x' -> return $ toLazy x'
                 _       -> -- try again with 2-letter locale
                    let s' = take 2 s in
                    case lookup ("locales-" ++ fromMaybe s'
                           (lookup s' langBase) ++ ".xml") localeFiles of
                          Just x'' -> return $ toLazy x''
                          _        -> E.throwIO $ CSLLocaleNotFound s
#else
  f <- case length s of
             0 -> return "locales/locales-en-US.xml"
             2 -> getDataFileName ("locales/locales-" ++
                                maybe s id (lookup s langBase) ++ ".xml")
             _ -> getDataFileName ("locales/locales-" ++ take 5 s ++ ".xml")
  exists <- doesFileExist f
  if not exists && length s > 2
     then getLocale $ take 2 s  -- try again with base locale
     else E.handle (\e -> E.throwIO (CSLLocaleReadError e)) $ L.readFile f
#endif
getLocale :: String -> IO L.ByteString
getLocale s = do
#ifdef EMBED_DATA_FILES
  let toLazy x = L.fromChunks [x]
  case length s of
      0 -> maybe (E.throwIO $ CSLLocaleNotFound "en-US")
                 (return . toLazy)
           $ lookup "locales-en-US.xml" localeFiles
      2 -> let fn = ("locales-" ++ fromMaybe s (lookup s langBase) ++ ".xml")
           in case lookup fn localeFiles of
                Just x' -> return $ toLazy x'
                _       -> E.throwIO $ CSLLocaleNotFound s
      _ -> case lookup ("locales-" ++ take 5 s ++ ".xml") localeFiles of
                 Just x' -> return $ toLazy x'
                 _       -> -- try again with 2-letter locale
                    let s' = take 2 s in
                    case lookup ("locales-" ++ fromMaybe s'
                           (lookup s' langBase) ++ ".xml") localeFiles of
                          Just x'' -> return $ toLazy x''
                          _        -> E.throwIO $ CSLLocaleNotFound s
#else
  f <- case length s of
             0 -> return "locales/locales-en-US.xml"
             2 -> getDataFileName ("locales/locales-" ++
                                maybe s id (lookup s langBase) ++ ".xml")
             _ -> getDataFileName ("locales/locales-" ++ take 5 s ++ ".xml")
  exists <- doesFileExist f
  if not exists && length s > 2
     then getLocale $ take 2 s  -- try again with base locale
     else E.handle (\e -> E.throwIO (CSLLocaleReadError e)) $ L.readFile f
#endif
getLocale s = do
#ifdef EMBED_DATA_FILES
  let toLazy x = L.fromChunks [x]
  case length s of
      0 -> maybe (E.throwIO $ CSLLocaleNotFound "en-US")
                 (return . toLazy)
           $ lookup "locales-en-US.xml" localeFiles
      2 -> let fn = ("locales-" ++ fromMaybe s (lookup s langBase) ++ ".xml")
           in case lookup fn localeFiles of
                Just x' -> return $ toLazy x'
                _       -> E.throwIO $ CSLLocaleNotFound s
      _ -> case lookup ("locales-" ++ take 5 s ++ ".xml") localeFiles of
                 Just x' -> return $ toLazy x'
                 _       -> -- try again with 2-letter locale
                    let s' = take 2 s in
                    case lookup ("locales-" ++ fromMaybe s'
                           (lookup s' langBase) ++ ".xml") localeFiles of
                          Just x'' -> return $ toLazy x''
                          _        -> E.throwIO $ CSLLocaleNotFound s
#else
  f <- case length s of
             0 -> return "locales/locales-en-US.xml"
             2 -> getDataFileName ("locales/locales-" ++
                                maybe s id (lookup s langBase) ++ ".xml")
             _ -> getDataFileName ("locales/locales-" ++ take 5 s ++ ".xml")
  exists <- doesFileExist f
  if not exists && length s > 2
     then getLocale $ take 2 s  -- try again with base locale
     else E.handle (\e -> E.throwIO (CSLLocaleReadError e)) $ L.readFile f
#endif