From 33aeb53f7a14018070fec3185653e5e20ee05398 Mon Sep 17 00:00:00 2001 From: Thomas Hallgren Date: Tue, 26 Nov 2019 15:27:02 +0100 Subject: [PATCH] PGFService: userLanguage now defaults to English, if present in the grammar The userLangauge is the name of the concrete syntax that has a languageCode that matches the user's preferred language, as reported by the web browser. If no matching language code is found, the PGF service now sets userLanguage to the concrete syntax for English (e.g. FoodsEng) if present, and defaults to the first concrete syntax (e.g. FoodsAfr) only if English is not present in the grammar. --- src/server/PGFService.hs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/server/PGFService.hs b/src/server/PGFService.hs index 8a2857289..927d58310 100644 --- a/src/server/PGFService.hs +++ b/src/server/PGFService.hs @@ -39,7 +39,7 @@ import Control.Monad.State(State,evalState,get,put) import Control.Monad.Catch(bracket_) import Data.Char --import Data.Function (on) -import Data.List ({-sortBy,-}intersperse,mapAccumL,nub,isSuffixOf,nubBy) +import Data.List ({-sortBy,-}intersperse,mapAccumL,nub,isSuffixOf,nubBy,stripPrefix) import qualified Data.Map as Map import Data.Maybe import System.Random @@ -1048,16 +1048,23 @@ linearizeAndUnlex pgf (mto,unlex) tree = langs = if null mto then PGF.languages pgf else mto selectLanguage :: PGF -> Maybe (Accept Language) -> PGF.Language -selectLanguage pgf macc = case acceptable of - [] -> case PGF.languages pgf of - [] -> error "No concrete syntaxes in PGF grammar." - l:_ -> l - Language c:_ -> fromJust (langCodeLanguage pgf c) +selectLanguage pgf macc = + case acceptable of + [] -> case PGF.languages pgf of + [] -> error "No concrete syntaxes in PGF grammar." + ls@(l1:_) -> case [l | l<-ls, langPart pgf l==Just "Eng"] of + eng:_ -> eng + _ -> l1 + Language c:_ -> fromJust (langCodeLanguage pgf c) where langCodes = mapMaybe (PGF.languageCode pgf) (PGF.languages pgf) acceptable = negotiate (map Language langCodes) macc langCodeLanguage :: PGF -> String -> Maybe PGF.Language -langCodeLanguage pgf code = listToMaybe [l | l <- PGF.languages pgf, PGF.languageCode pgf l == Just code] +langCodeLanguage pgf code = + listToMaybe [l | l <- PGF.languages pgf, PGF.languageCode pgf l == Just code] + +langPart pgf lang = + stripPrefix (PGF.showCId (PGF.abstractName pgf)) (PGF.showCId lang) -- * General utilities