Commit 3d2f546d authored by Luc Didry's avatar Luc Didry
Browse files

Better language detection

parent cf58613d
......@@ -45,6 +45,14 @@
<a class="dropdown-item" href="https://tipeee.com/fiat-tux">Tipeee</a>
</div>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="i18n-dpdn" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Language</a>
<div class="dropdown-menu" aria-labelledby="i18n-dpdn">
<a class="dropdown-item active" href="#" data-i18n="en">English</a>
<a class="dropdown-item" href="#" data-i18n="fr">Français</a>
<a class="dropdown-item" href="#" data-i18n="oc">Occitan</a>
</div>
</li>
</ul>
</div>
</nav>
......@@ -107,7 +115,7 @@
<div class="row d-none" id="set-corner-shape">
<div class="col-12">
<div class="row">
<label for="corners" class="col-sm-12 col-md-6">Corner shape</label>
<label for="corners" class="col-sm-12 col-md-6">Corners shape</label>
<div class="col-sm-12 col-md-6">
<select id="corners" class="form-control">
<option value="20" selected>Rounded corners</option>
......
// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
var svg = new C2S(800,400);
var showSettings = 'Show advanced settings';
var hideSettings = 'Hide advanced settings';
var workCanvas = 'Work with canvas';
var workSvg = 'Work with SVG';
var customImgText = 'Custom image';
......@@ -37,6 +35,7 @@ var fonts = {
var loadedFonts = {};
$(document).ready(function() {
detectLang();
translate();
getParameters();
bindEvents();
......@@ -48,13 +47,37 @@ $(document).ready(function() {
});
});
function translate() {
function detectLang() {
var languages = [];
var clang = readCookie('lang');
if (clang !== undefined && clang !== null) {
languages.push(clang);
}
var nl = navigator.languages;
for (var i = 0; i < nl.length; i++) {
languages.push(nl[i]);
}
if (languages.length !== 0) {
translate(languages.shift(), languages);
} else {
console.log('Using old style language detection.');
var userLang = navigator.language || navigator.userLanguage;
translate(userLang);
}
}
function translate(userLang, languages) {
// Try to get a translation
var userLang = navigator.language || navigator.userLanguage;
$.get({
url: 'locales/'+userLang+'.json',
dataType: 'json',
error: function() {
if (languages !== undefined && languages !== null && languages.length !== 0) {
translate(languages.shift(), languages);
}
},
success: function(locale) {
toggleI18n(userLang);
['get-source', 'donate-dpdn', 'license', 'author', 'wemake', 'porn', 'rectangle', 'color', 'transparency', 'x-position', 'y-position', 'font', 'style', 'text-size'].forEach(function(e) {
if (locale[e] !== undefined) {
$('.'+e).text(locale[e]);
......@@ -84,17 +107,11 @@ function translate() {
$('option[value="'+e+'"]').text(locale[e]);
}
});
['donate-dpdn', 'settings-dpdn', 'toggle-svg', 'download', 'reset', 'switch-colors'].forEach(function(e) {
['donate-dpdn', 'i18n-dpdn', 'settings-dpdn', 'toggle-svg', 'download', 'reset', 'switch-colors'].forEach(function(e) {
if (locale[e] !== undefined) {
$('#'+e).text(locale[e]);
}
});
if (locale['settings-show'] !== undefined) {
showSettings = locale['settings-show'];
}
if (locale['settings-hide'] !== undefined) {
hideSettings = locale['settings-hide'];
}
if (locale['toggle-canvas'] !== undefined) {
workCanvas = locale['toggle-canvas'];
}
......@@ -302,6 +319,13 @@ function bindEvents() {
if (customImg === undefined || customImg === null) {
$('#custom-img').val(null);
}
// Choose language
$('[data-i18n]').click(function(e) {
e.preventDefault();
createCookie('lang', $(this).data('i18n'), 30);
translate($(this).data('i18n'));
});
}
function toggleBgSettings() {
......@@ -383,6 +407,11 @@ function waitForfont(id, cb) {
}
}
function toggleI18n(lang) {
$('[data-i18n]').removeClass('active');
$('[data-i18n="'+lang+'"]').addClass('active');
}
function handleImage(e) {
var reader = new FileReader();
reader.onload = function(event) {
......@@ -711,3 +740,32 @@ function colorToRGBA(color, alpha) {
b = parseInt(''+a[4]+a[6], 16);
return 'rgba('+r+','+g+','+b+','+(100 - alpha/100)+')';
}
// Cookies
function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
}
else {
var expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
This diff is collapsed.
{
"get-source": "Get the sources",
"donate-dpdn": "Donate",
"license": "License",
"author": "Author",
"hide-settings": "Home",
"settings-dpdn": "Settings",
"set-corner-shape": "Sticker shape",
"set-colors": "Coulors",
"set-fonts": "Fonts",
"set-bg": "Background",
"set-positions": "Positions",
"wemake": "First line",
"porn": "Second line",
"filename": "Filename",
"corners": "Corners shape",
"corners-rounded": "Rounded corners",
"corners-square": "Square corners",
"color": "Color",
"font": "Font",
"transparency": "Transparency",
"switch-colors": "Switch texts' colors",
"add-glitter": "Add glitter",
"bg-type": "Background type",
"plain": "Plain color",
"gradient": "Gradient color",
"rainbow": "Rainbow gradient",
"img": "Image",
"bgu-color": "Background color",
"bgr-color": "Begin gradient color",
"egr-color": "End gradient color",
"gr-orientation": "Gradient orientation",
"lr": "Left to right",
"rl": "Right to left",
"tb": "Top to bottom",
"bt": "Bottom to top",
"r": "Radial",
"bg-img": "Choose the background image",
"beer": "Beer",
"glitter": "Glitter",
"rainbow-flag": "Rainbow flag",
"sky": "Sky",
"field": "Field",
"bridge": "Bridge",
"x-position": "x position",
"y-position": "y position",
"text-size": "Text size",
"custom-img": "Upload a background image",
"custom-img-text": "",
"custom-x": "Custom image x position",
"custom-y": "Custom image y position",
"custom-size": "Custom image size",
"toggle-svg": "Work with SVG",
"toggle-canvas": "Work with Canvas",
"download": "Download",
"reset": "Reset to default",
"regular": "Regular",
"bold": "Bold",
"italic": "Italic",
"italic bold": "Italic bold",
"i18n-dpdn": "Language"
}
......@@ -54,10 +54,9 @@
"toggle-canvas": "Travailler en Canvas",
"download": "Télécharger",
"reset": "Réinitialiser les paramètres",
"settings-show": "Afficher les paramètres",
"settings-hide": "Cacher les paramètres",
"regular": "Normal",
"bold": "Gras",
"italic": "Italique",
"italic bold": "Italique gras"
"italic bold": "Italique gras",
"i18n-dpdn": "Langage"
}
......@@ -54,10 +54,9 @@
"toggle-canvas": "Travailler en Canvas",
"download": "Telecargar",
"reset": "Reïnicializar los paramètres",
"settings-show": "Mostrar los paramètres",
"settings-hide": "Amagar los paramètres",
"regular": "Normal",
"bold": "Gras",
"italic": "Italique",
"italic bold": "Italique gras"
"italic bold": "Italique gras",
"i18n-dpdn": "Lengatge"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment