Skip to content

Commit

Permalink
better language support
Browse files Browse the repository at this point in the history
  • Loading branch information
on committed Aug 27, 2024
1 parent cc1af53 commit 5e52189
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions yr.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name YR.no - Hotkeys
// @namespace http://tampermonkey.net/
// @version 1.2.1
// @version 1.2.2
// @description Navigate the yr.no navbar using `Ctrl` + `←`/`→` (For MAC: `Control` + `Option` + `←`/`→`). Navigate to 21-day forecast, radar map or daily table view using `Alt` + `L`, `Alt` + `R` or `Alt` + `V`.
// @author Øyvind Nilsen (on@ntnu.no)
// @match https://www.yr.no/*
Expand All @@ -13,6 +13,34 @@
(function() {
'use strict';

// Language Code
var lang_code = 'en'; // Default language code
if (typeof self.__LOCALE_CODE__ !== 'undefined' && self.__LOCALE_CODE__ !== null) {
lang_code = self.__LOCALE_CODE__;
}

// Translations
const translations = {
"21-day-forecast": {
"en": "21-day-forecast",
"nb": "21-dagersvarsel",
"nn": "21-dagarsvarsel",
"sme": "21-beaivvedieđáhus"
},
"daily-table": {
"en": "forecast/daily-table",
"nb": "værvarsel/daglig-tabell",
"nn": "vêrvarsel/dagleg-tabell",
"sme": "dálkedieđáhus/beaivválaš-tabealla"
},
"radar": {
"en": "map/radar",
"nb": "kart/radar",
"nn": "kart/radar",
"sme": "kárta/rádár"
}
};

// Function to navigate to the next or previous menu item
function navigateNavbar(direction) {
const navbar = document.querySelector('#location-header__list');
Expand Down Expand Up @@ -50,44 +78,20 @@
var view = '';

if (match) {
console.log("Full match:", match[0]);
console.log("Language code:", match[1]);
console.log("Language code:", lang_code);
console.log("Path:", match[2]);
console.log("ID:", match[3]);

if (match[1] === 'nb') {
console.log("This is the Norwegian site.");
if (event.key === 'l') {
view = `21-dagersvarsel`;
} else if (event.key === 'r') {
view = `kart/radar`;
} else if (event.key === 'v') {
view = `værvarsel/daglig-tabell`;
}
} else if (match[1] === 'en') {
console.log("This is the English site.");
if (event.key === 'l') {
view = `21-day-forecast`;
} else if (event.key === 'r') {
view = `map/radar`;
} else if (event.key === 'v') {
view = `forecast/daily-table`;
}
} else if (match[1] === 'nn') {
console.log("This is the Nynorsk site.");
if (event.key === 'l') {
view = `21-dagarsvarsel`;
} else if (event.key === 'r') {
view = `kart/radar`;
} else if (event.key === 'v') {
view = `vêrvarsel/dagleg-tabell`;
}
} else {
console.log("This is another language site.");
if (event.key === 'l') {
view = translations['21-day-forecast'][lang_code];
} else if (event.key === 'r') {
view = translations['radar'][lang_code];
} else if (event.key === 'v') {
view = translations['daily-table'][lang_code];
}

if (view) {
const newUrl = `https://www.yr.no/${match[1]}/${view}/${match[3]}/`;
const newUrl = `https://www.yr.no/${lang_code}/${view}/${match[3]}/`;
window.location.href = newUrl;
}
}
Expand Down

0 comments on commit 5e52189

Please sign in to comment.