Skip to content

Commit

Permalink
Merge pull request #5 from gardon/multilanguage
Browse files Browse the repository at this point in the history
Multilanguage
  • Loading branch information
gardon authored Dec 10, 2019
2 parents 5b73ba5 + 326cb42 commit c5d424e
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 57 deletions.
22 changes: 22 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9000
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
3 changes: 2 additions & 1 deletion app/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
# This catch-all rule only rewrites requests for existing routes.
RewriteRule ^(|about|chapters(\/[0-9]*)?)$ index.html [L]
# Added optional language segment.
RewriteRule ^([a-z\-]+(/|$)|)(|about|chapters(\/[0-9a-z\-]*)?)$ index.html [L]

# Rules to correctly serve gzip compressed CSS and JS files.
# Requires both mod_rewrite and mod_headers to be enabled.
Expand Down
28 changes: 22 additions & 6 deletions app/css/nvel.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,41 @@ span.loading-icon {
}

ul.navbar-list,
ul.social-links {
ul.social-links,
ul.language-switcher {
list-style: none;
margin: 0;
margin: 0 1em 0 0;
}

@media (min-width: 400px) {
ul.language-switcher {
margin-right: 0;
}
}

ul.navbar-list li,
ul.social-links li {
ul.social-links li,
ul.language-switcher li {
display: inline;
margin: 0 2em 0 0;
}

ul.navbar-list li a {
ul.social-links li:last-child,
ul.language-switcher li:last-child {
margin-right: 0;
}

ul.navbar-list li a,
ul.language-switcher li a {
text-decoration: none;
text-transform: uppercase;
color: #777;
font-size:1.2em;
}

@media (min-width: 750px) {
ul.navbar-list li a {
ul.navbar-list li a,
ul.language-switcher li a {
font-size:1em;
}
}
Expand All @@ -126,7 +141,8 @@ ul.navbar-list li.active a:hover {
}


ul.social-links {
ul.social-links,
ul.language-switcher {
position: absolute;
right: 0;
top: 0.5em;
Expand Down
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
for (var i = 0; i < images.length; i++) {
var image = images[i];
if (image.offsetTop < scroll + 1000) {
var ids = image.id.split("-");
var ids = image.id.split(":");
app.ports.lazyLoad.send({chapter: ids[1], section: Number(ids[2])})
}
}
Expand Down
67 changes: 62 additions & 5 deletions app/static_get.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@
require_once "static_config.php";
$config = static_config();

$path = explode('/', $_GET['q']);
list($lang, $path_raw) = parse_path($_GET['q']);
$path = explode('/', $path_raw);
$lang = empty($lang) ? $lang : '/' . $lang;

if (empty($path[0]) && !empty($lang)) {
$path[0] = 'index';
}

switch ($path[0]) {
case 'index':
case 'about':
$url = $config['backend_url'];
$url = $config['backend_url'] . $lang;
break;
case 'chapters':
if (count($path) == 1) {
$url = $config['backend_url'];
$url = $config['backend_url'] . $lang;
break;
}
elseif (count($path) == 2 && is_numeric($path[1])) {
$url = $config['backend_url'] . '/node/' . $path[1];
elseif (count($path) == 2 && $path[1]) {
$url = $config['backend_url'] . $lang . '/chapters/' . $path[1];
break;
}
// Intentionally fallback to default fom here
Expand All @@ -27,6 +34,56 @@
redirect($url);

function redirect($url) {
// Todo: find a way to respect Content-language header.
readfile($url);
die();
}

function parse_path($query) {
$matches = [];
if (preg_match('#(|index|about|chapters(?:/[0-9a-z\-]+)?)/?$#', $query, $matches)) {
list(, $path) = $matches;
$prefix = explode('/', str_replace($path, '', $query));
if (count($prefix) == 1 || (count($prefix) == 2 && empty($prefix[1]))) {
$lang = preg_replace('@[^a-z\-]@', '', $prefix[0]);
return [$lang, $path];
}
return FALSE;
}
else {
return FALSE;
}
}

function test_parse_path() {
$test = [
'index' => ['', 'index'],
'en' => ['en', ''],
'pt-br/' => ['pt-br', ''],
'chapters' => ['', 'chapters'],
'en/about' => ['en', 'about'],
'pt-br/chapters/4' => ['pt-br', 'chapters/4'],
'pt-br/chapters/4-fuga' => ['pt-br', 'chapters/4-fuga'],
'en/chapters/5-another' => ['en', 'chapters/5-another'],
'en/chapters/5-another/invalid' => FALSE,
'en/chapters/5-another_invalid' => FALSE,
'chapters/94' => ['', 'chapters/94'],
'en/other/' => FALSE,
'pt-br/about/us' => FALSE,
'en/user/login' => FALSE,
'user/login' => FALSE,
// any loose string is parsed as a language.
'something' => ['something', ''],
// because we don't validate language, should then filter out special chars.
'some!@#34?="specialstring"<a href=' => ['somespecialstringahref', '' ],
'some!@#34?="specialstring"<a href=/chapters' => ['somespecialstringahref', 'chapters' ],
];
foreach ($test as $input => $result) {
if (parse_path($input) == $result) {
print "Passed.\n";
}
else {
print "Failed $input: expected [" . implode (', ', $result) . "] got [" . implode(', ', parse_path($input)) . "]\n";
}
}
}
31 changes: 25 additions & 6 deletions src/App.elm
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ init flags location key =
switchBackend

lang =
Config.getLanguage
Config.getLanguage <| parseLanguage location

langs =
Config.getLanguages

pageData =
{ title = translate lang Loading, lang = Language.toString lang }
Expand All @@ -69,7 +72,7 @@ init flags location key =
parseLocation location

model =
Model chapters siteInformation pageData backendConfig menu route key lang True location
Model chapters siteInformation pageData backendConfig menu route key lang langs True location
in
( model, getSiteInformation model )

Expand Down Expand Up @@ -137,18 +140,34 @@ update msg model =
-- ScrollTop (Err x) ->
-- ( model, Cmd.none )

OnLocationChange location ->
OnLocationChange newlocation ->
let
newRoute =
parseLocation location
parseLocation newlocation

newlang =
Config.getLanguage <| parseLanguage newlocation

cmd =
if newlang == model.language then
updatePageData updatedModel.pageData
else
getSiteInformation updatedModel

chapters = if newlang == model.language then model.chapters else Nothing

newmodel =
{ model | route = newRoute, location = location }
{ model | chapters = chapters, route = newRoute, language = newlang, location = newlocation }

updatedModel =
{ newmodel | pageData = pageData newmodel }
in
( updatedModel, Cmd.batch [ updatePageData updatedModel.pageData, pageChange () ] )
( updatedModel
, Cmd.batch
[ cmd
, pageChange ()
]
)

Navbar action ->
let
Expand Down
5 changes: 3 additions & 2 deletions src/Chapters.elm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import List exposing (..)
import Models exposing (..)
import Msgs exposing (..)
import Resources exposing (..)

import Language exposing (..)


-- Http
Expand All @@ -18,7 +18,7 @@ getChapters : Model -> Cmd Msg
getChapters model =
let
url =
model.backendConfig.backendURL ++ chapterListEndpoint
model.backendConfig.backendURL ++ Language.toString model.language ++ "/" ++ chapterListEndpoint
in
Http.get
{ url = url
Expand Down Expand Up @@ -55,6 +55,7 @@ chapterDecoder =
|> required "authors" (Decode.list Decode.string)
|> required "publication_date_unix" dateDecoder
|> required "featured_image" imageDecoder
|> required "path" Decode.string


decodeChapters : Decode.Decoder (Dict String Chapter)
Expand Down
10 changes: 5 additions & 5 deletions src/Chapters/Chapter.elm
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ replaceChapter : Model -> Chapter -> Model
replaceChapter model newchapter =
case model.chapters of
Nothing ->
{ model | chapters = Just (Dict.singleton newchapter.nid newchapter) }
{ model | chapters = Just (Dict.singleton newchapter.path newchapter) }

Just chapters ->
{ model | chapters = Just (Dict.insert newchapter.nid newchapter chapters) }
{ model | chapters = Just (Dict.insert newchapter.path newchapter chapters) }


viewChapter : Chapter -> Html Msg
Expand All @@ -62,7 +62,7 @@ viewSection model =
, ( "not-loaded", not model.image.load )
]
in
skeletonRow [ classList classes, "section-" ++ model.chapter ++ "-" ++ String.fromInt model.id |> id ]
skeletonRow [ classList classes, "section:" ++ model.chapter ++ ":" ++ String.fromInt model.id |> id ]
[ viewImage
[ class "u-full-width"
, sizes [ "100w" ]
Expand All @@ -80,7 +80,7 @@ viewSection model =
, ( "not-loaded", not model.image.load )
]
in
skeletonRowFullWidth [ classList classes, "section-" ++ model.chapter ++ "-" ++ String.fromInt model.id |> id ]
skeletonRowFullWidth [ classList classes, "section:" ++ model.chapter ++ ":" ++ String.fromInt model.id |> id ]
[ viewImage
[ class "u-full-width"
, sizes [ "100w" ]
Expand All @@ -101,7 +101,7 @@ viewSection model =
]

elementid =
"section-" ++ model.chapter ++ "-" ++ String.fromInt model.id
"section:" ++ model.chapter ++ ":" ++ String.fromInt model.id
in
skeletonRow [ classList classes, id elementid ]
[ viewImage [] model.image
Expand Down
18 changes: 13 additions & 5 deletions src/Config.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Config exposing (chapterData, getChapterFromId, getLanguage, getSiteInformation, pageData, siteInformation, switchBackend)
module Config exposing (chapterData, getChapterFromId, getLanguage, getLanguages, getSiteInformation, pageData, siteInformation, switchBackend)

import Config.Environment exposing (..)
import Config.Site exposing (..)
Expand All @@ -16,9 +16,17 @@ switchBackend =
backend


getLanguage : Language
getLanguage =
language
getLanguage : Maybe Language -> Language
getLanguage maybeLanguage =
case maybeLanguage of
Just lang ->
lang
Nothing ->
language

getLanguages : List Language
getLanguages =
Config.Site.languages


siteInformation : SiteInformation
Expand Down Expand Up @@ -89,7 +97,7 @@ getSiteInformation : Model -> Cmd Msg
getSiteInformation model =
let
url =
model.backendConfig.backendURL ++ siteInformationEndpoint
model.backendConfig.backendURL ++ Language.toString model.language ++ "/" ++ siteInformationEndpoint
in
Http.get
{ url = url
Expand Down
6 changes: 5 additions & 1 deletion src/Config/Site.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Config.Site exposing (aboutData, chaptersListData, homeData, language, notFoundData, siteInformation)
module Config.Site exposing (aboutData, chaptersListData, homeData, language, languages, notFoundData, siteInformation)

import Language exposing (..)
import Models exposing (..)
Expand Down Expand Up @@ -50,3 +50,7 @@ notFoundData lang =
language : Language
language =
Pt_Br

languages : List Language
languages =
[ Pt_Br, En ]
Loading

0 comments on commit c5d424e

Please sign in to comment.