diff --git a/.gitignore b/.gitignore index d5e2f1d..a488e9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules vendor +build diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..d3902a4 --- /dev/null +++ b/build.sh @@ -0,0 +1,50 @@ +#!/bin/bash + + +PORT=8452 +echo "Trying to start a PHP server on port $PORT" +php -S 127.0.0.1:$PORT -t ./public & +phpPID=$! +echo "PHP server running with PID $phpPID" + +sleep 0.3 +mkdir ./build +cd build + +locales=('fr' 'en') +for locale in "${locales[@]}"; do + echo "Downloading $locale..." + mkdir $locale + cd $locale + + # wget with custom header to not render link to change language because wget will follow them + wget -k -K -E -r -l 10 -p -N -F -nH http://127.0.0.1:$PORT/$locale --header="Enable-Locale-Switch: no" + + mv $locale.html index.html + + # Multiple sed commands to clean up the mess made by wget replacing stuff + # sed -E -i 's/\"([a-z]{2})\.html/\"\/\1/g' *.html + + # for the main page, remove broken link to the same page (because we renamed to index.html) + sed -E -i "s/\"$locale\.html/\"/g" index.html + + # for each file other than index.html, put a proper link back to the main page + find ./ -not -name "index.html" -name "*.html" -exec sed -E -i "s/\"(\.\.\/)?$locale\.html/\"\1index.html/g" {} \+ + + # repair the links of the footer locale switcher + # for each line with data-lang-switch="{locale}" replace the href with the link to /{locale}/path... + # attempt to do with pipe failed, so I use a non optimal for loop + # find ./ -name "*.html" -exec sh -c 'sed -E -i "s/data-lang-switch=\"([a-z]{2})\".+href\=\".*\"/href\=\"\/\1\/{}\"/g" {}' \; + for path in $(find ./ -name "*.html"); do + pathM=`echo $path | cut -c 3-` + sed -E -i "s%data-lang-switch=\"([a-z]{2})\".+href\=\".*\"%href\=\"/\1/$pathM\"%g" $path + done + + cd .. +done + + +kill $phpPID + +echo "Done." + diff --git a/src/Middlewares/TwigLocalizationMiddleware.php b/src/Middlewares/TwigLocalizationMiddleware.php index 81375cf..e283238 100644 --- a/src/Middlewares/TwigLocalizationMiddleware.php +++ b/src/Middlewares/TwigLocalizationMiddleware.php @@ -25,8 +25,11 @@ class TwigLocalizationMiddleware { public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { - $this->container->get(Twig::class)->getEnvironment()->addGlobal('locale', $request->getAttribute('locale')); + $env = $this->container->get(Twig::class)->getEnvironment(); + $env->addGlobal('locale', $request->getAttribute('locale')); + $env->addGlobal('enableLocaleSwitch', $request->hasHeader('Enable-Locale-Switch') ? ($request->getHeader('Enable-Locale-Switch')[0] === 'yes') : true); + return $handler->handle($request); } -} \ No newline at end of file +} diff --git a/templates/layout.html.twig b/templates/layout.html.twig index cb55b97..dc93205 100644 --- a/templates/layout.html.twig +++ b/templates/layout.html.twig @@ -38,14 +38,14 @@