Skip to content

Commit 99453db

Browse files
r8n5nr8n5n
authored andcommitted
2 parents dbd19a7 + 6b9de2f commit 99453db

17 files changed

+554
-60
lines changed

src/ConfiguredSilex.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
<?php
2-
3-
/**
4-
* @file
5-
* Configured Silex application.
6-
*/
7-
8-
namespace Sheffugees;
9-
10-
use Silex\Application;
11-
12-
/**
13-
* @class
14-
* ConfiguredSilex.
15-
*/
16-
class ConfiguredSilex extends Application
17-
{
18-
/**
19-
* @inheritDoc
20-
*
21-
* Configure routes, services etc.
22-
*/
23-
public function __construct(array $values = [])
24-
{
25-
parent::__construct($values);
26-
27-
$this->get('/', 'Sheffugees\\Controllers\\All::index');
28-
29-
$this->register(new \Silex\Provider\TwigServiceProvider(), [
30-
'twig.path' => __DIR__ . '/../views',
31-
]);
32-
}
33-
}
1+
<?php
2+
3+
/**
4+
* @file
5+
* Configured Silex application.
6+
*/
7+
8+
namespace Sheffugees;
9+
10+
use Silex\Application;
11+
12+
/**
13+
* @class
14+
* ConfiguredSilex.
15+
*/
16+
class ConfiguredSilex extends Application
17+
{
18+
/**
19+
* @inheritDoc
20+
*
21+
* Configure routes, services etc.
22+
*/
23+
public function __construct(array $values = [])
24+
{
25+
parent::__construct($values);
26+
27+
$this->get('/', 'Sheffugees\\Controllers\\All::index');
28+
$this->get('/{lang}', 'Sheffugees\\Controllers\\All::location');
29+
$this->get('/{lang}/choose', 'Sheffugees\\Controllers\\All::choose');
30+
$this->get('/{lang}/need/{need}', 'Sheffugees\\Controllers\\All::need');
31+
$this->get('/{lang}/results/{find}', 'Sheffugees\\Controllers\\All::results');
32+
33+
$this->register(new \Silex\Provider\TwigServiceProvider(), [
34+
'twig.path' => __DIR__ . '/../views',
35+
]);
36+
}
37+
}

src/Controllers/All.php

Lines changed: 84 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,84 @@
1-
<?php
2-
3-
/**
4-
* @file
5-
* Controller for all pages in prototype.
6-
*/
7-
8-
namespace Sheffugees\Controllers;
9-
10-
use Silex\Application;
11-
use Symfony\Component\HttpFoundation\Request;
12-
13-
/**
14-
* @class
15-
* All.
16-
*/
17-
class All
18-
{
19-
/**
20-
* Callback: index, trying to choose language.
21-
*/
22-
public function index(Request $request, Application $app)
23-
{
24-
return $app['twig']->render('index.twig');
25-
}
26-
}
1+
<?php
2+
3+
/**
4+
* @file
5+
* Controller for all pages in prototype.
6+
*/
7+
8+
namespace Sheffugees\Controllers;
9+
10+
use Silex\Application;
11+
use Symfony\Component\HttpFoundation\Request;
12+
13+
/**
14+
* @class
15+
* All.
16+
*/
17+
class All
18+
{
19+
/**
20+
* Page callback: welcome and pick language.
21+
*/
22+
public function index(Request $request, Application $app)
23+
{
24+
return $app['twig']->render('start.twig');
25+
}
26+
27+
/**
28+
* Page callback: set location.
29+
*/
30+
public function location(Request $request, Application $app, $lang)
31+
{
32+
if (!preg_match("/^[a-z]{2}\$/", $lang)) {
33+
return $app->redirect('/');
34+
}
35+
36+
return $app['twig']->render("location-$lang.twig");
37+
}
38+
39+
/**
40+
* Page callback: choose a need.
41+
*/
42+
public function choose(Request $request, Application $app, $lang)
43+
{
44+
if (!preg_match("/^[a-z]{2}\$/", $lang)) {
45+
return $app->redirect('/');
46+
}
47+
48+
return $app['twig']->render("choose-$lang.twig");
49+
}
50+
51+
/**
52+
* Page callback: define a need.
53+
*/
54+
public function need(Request $request, Application $app, $lang, $need)
55+
{
56+
if (!preg_match("/^[a-z]{2}\$/", $lang)) {
57+
return $app->redirect('/');
58+
}
59+
60+
if (!preg_match("/^[a-z]+\$/", $need)) {
61+
return $app->redirect('/');
62+
}
63+
64+
65+
return $app['twig']->render("need-$lang-$need.twig");
66+
}
67+
68+
/**
69+
* Page callback: return some results.
70+
*/
71+
public function results(Request $request, Application $app, $lang, $find)
72+
{
73+
if (!preg_match("/^[a-z]{2}\$/", $lang)) {
74+
return $app->redirect('/');
75+
}
76+
77+
if (!preg_match("/^[a-z]+\$/", $find)) {
78+
return $app->redirect('/');
79+
}
80+
81+
return $app['twig']->render("results-$lang-$find.twig");
82+
}
83+
84+
}

translate-demo/translation-demo

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<html>
2+
<head>
3+
4+
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
5+
<script>
6+
$(document).ready(function() {
7+
$('#languages').data('pre', $(this).val());
8+
$('#languages').bind('change', runTranslation);
9+
10+
function runTranslation(evt) {
11+
var before_change = $(this).data('pre');//get the pre data
12+
var langCode = $("#languages option:selected").val();
13+
14+
// Empty string returned on first interaction with select?
15+
if (!before_change) {
16+
before_change = document.getElementById('languages').options[0].value;
17+
}
18+
19+
// Hardcoded for now, just pass originalText in as a parameter and it will replace the spaces and send to the api
20+
var originalText = "Hello! Welcome to our new website";
21+
var replacedText = originalText.split(' ').join('+');
22+
23+
var langCode = $("#languages option:selected").val();
24+
var xhr = new XMLHttpRequest();
25+
26+
xhr.onreadystatechange = function() {
27+
if (xhr.readyState == 4 && xhr.status == 200) {
28+
var jsonObject = JSON.parse(xhr.responseText);
29+
$('#textArea').text(jsonObject.text[0]);
30+
}
31+
};
32+
33+
// Had a problem translating from certain languages to others (encoding issues I think). But our source content will always be in English
34+
// so we can just use that and translate to the selected languages
35+
36+
// API Key hardcoded for now
37+
//xhr.open("GET", "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20160206T123230Z.37bca81167793559.4914b0cdb53f30907bf278a6726aee775e04cfb1&text=" + replacedText +"&lang=" + before_change + "-" + langCode, false);
38+
xhr.open("GET", "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20160206T123230Z.37bca81167793559.4914b0cdb53f30907bf278a6726aee775e04cfb1&text=" + replacedText +"&lang=" + langCode, false);
39+
xhr.send();
40+
41+
$(this).data('pre', $(this).val());
42+
}
43+
});
44+
</script>
45+
46+
</head>
47+
48+
<body>
49+
50+
<!--Harcoded in a few language codes as examples, full supported list available here: https://tech.yandex.com/translate/doc/dg/concepts/langs-docpage/ -->
51+
<select id="languages">
52+
<option value="en">English</option>
53+
<option value="ar">Arabic</option>
54+
<option value="hy">Armenian</option>
55+
<option value="bs">Bosnian</option>
56+
</select>
57+
<br/>
58+
<br/>
59+
<div id="textArea">
60+
Hello! Welcome to our new website
61+
</div>
62+
</body>
63+
64+
</html>

translate-demo/yandex-api-key

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trnsl.1.1.20160206T123230Z.37bca81167793559.4914b0cdb53f30907bf278a6726aee775e04cfb1

views/base.twig

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!doctype html>
2+
<html class="no-js" lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="x-ua-compatible" content="ie=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Foundation for Sites</title>
8+
<link rel="stylesheet" href="/css/app.css">
9+
</head>
10+
<body>
11+
<div class="row">
12+
<div class="large-12 columns">
13+
{% block languages %}
14+
<a href="/en">English</a>|<a href="/ar" dir="rtl">العربية</a>
15+
{% endblock %}
16+
</div>
17+
</div>
18+
19+
<div class="row">
20+
<div class="large-12 columns">
21+
<h1>{% block title %}{% endblock %}</h1>
22+
23+
{% block standfirst %}{% endblock %}
24+
</div>
25+
</div>
26+
27+
<div class="row">
28+
{% block content %}{% endblock %}
29+
</div>
30+
31+
<script src="/bower_components/jquery/dist/jquery.js"></script>
32+
<script src="/bower_components/what-input/what-input.js"></script>
33+
<script src="/bower_components/foundation-sites/dist/foundation.js"></script>
34+
<script src="/js/app.js"></script>
35+
</body>
36+
</html>

views/choose-ar.twig

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{% extends "base.twig" %}
2+
3+
{% block title %}What do you need? (Arabic){% endblock %}
4+
5+
{% block content %}
6+
<div>
7+
<ul class="small-up-2 medium-up-4 large-up-4 button-list">
8+
<li class="column">
9+
<a href="/ar/need/money" class="button expand">
10+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
11+
<p>Money</p>
12+
</a>
13+
</li>
14+
<li class="column">
15+
<a href="#" class="button expand">
16+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
17+
<p>Food</p>
18+
</a>
19+
</li>
20+
<li class="column">
21+
<a href="#" class="button expand">
22+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
23+
<p>Language help</p>
24+
</a>
25+
</li>
26+
<li class="column">
27+
<a href="#" class="button expand">
28+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
29+
<p>Places for help</p>
30+
</a>
31+
</li>
32+
<li class="column">
33+
<a href="#" class="button expand">
34+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
35+
<p>Government Information</p>
36+
</a>
37+
</li>
38+
<li class="column">
39+
<a href="#" class="button expand">
40+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
41+
<p>Home office centre</p>
42+
</a>
43+
</li>
44+
<li class="column">
45+
<a href="#" class="button expand">
46+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
47+
<p>Health</p>
48+
</a>
49+
</li>
50+
<li class="column">
51+
<a href="#" class="button expand">
52+
<img src="//placehold.it/64x64" class="thumbnail" alt="">
53+
<p>Transport</p>
54+
</a>
55+
</li>
56+
</ul>
57+
</div>
58+
{% endblock %}

0 commit comments

Comments
 (0)