{"id":15079742,"url":"https://github.com/w45p85/langchoose","last_synced_at":"2026-02-16T06:31:23.999Z","repository":{"id":251908216,"uuid":"838799233","full_name":"W45P85/langChoose","owner":"W45P85","description":"A skript that automatically redirects users to localized pages based on their browser language.","archived":false,"fork":false,"pushed_at":"2025-07-13T10:37:04.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-21T18:58:40.308Z","etag":null,"topics":["browserlanguage","chrome","css","edge","firefox","html","javascript","javascriptredirect","languageredirection","webdesign","webdevelopment"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/W45P85.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-06T11:15:42.000Z","updated_at":"2025-07-13T10:37:07.000Z","dependencies_parsed_at":"2025-02-17T23:32:26.705Z","dependency_job_id":"46ea339d-39c4-4afa-8ae3-7f1f3adc7e75","html_url":"https://github.com/W45P85/langChoose","commit_stats":null,"previous_names":["w45p85/langchoose"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/W45P85/langChoose","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/W45P85%2FlangChoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/W45P85%2FlangChoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/W45P85%2FlangChoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/W45P85%2FlangChoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/W45P85","download_url":"https://codeload.github.com/W45P85/langChoose/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/W45P85%2FlangChoose/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278861031,"owners_count":26058632,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["browserlanguage","chrome","css","edge","firefox","html","javascript","javascriptredirect","languageredirection","webdesign","webdevelopment"],"created_at":"2024-09-25T05:01:31.608Z","updated_at":"2025-10-07T22:47:45.917Z","avatar_url":"https://github.com/W45P85.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\u003cpre\u003e\n _                     ___ _                          \n| | __ _ _ __   __ _  / __\\ |__   ___   ___  ___  ___ \n| |/ _` | '_ \\ / _` |/ /  | '_ \\ / _ \\ / _ \\/ __|/ _ \\\n| | (_| | | | | (_| / /___| | | | (_) | (_) \\__ \\  __/\n|_|\\__,_|_| |_|\\__, \\____/|_| |_|\\___/ \\___/|___/\\___|\n               |___/                                  \n\u003cbr\u003e\nNote: This code needs to be tested in a server-client environment to avoid the following error: \nCross-Origin Request Blocked: The Same-Origin Policy disallows reading the external resource. \n(Reason: CORS request not http). This is a security error.\n\u003c/pre\u003e\n\u003c/div\u003e\n\n\n# Language Redirect\nThis repository contains a simple example of language redirection based on the browser's language settings. The redirection is implemented using JavaScript and works in Chrome, Firefox, and Edge.\n\n## Files\n\n### index.html\nThis is the main HTML file that loads the JavaScript script `main.js`.\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003ctitle\u003eLanguage Redirect\u003c/title\u003e\n    \u003cscript src=\"main.js\" defer\u003e\u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ch1\u003eWelcome\u003c/h1\u003e\n    \u003cp\u003eIf you are not redirected, please choose your language manually.\u003c/p\u003e\n    \u003cul\u003e\n        \u003cli\u003e\u003ca href=\"./de/index_de.html\"\u003eDeutsch\u003c/a\u003e\u003c/li\u003e\n        \u003cli\u003e\u003ca href=\"./en/index_en.html\"\u003eEnglish\u003c/a\u003e\u003c/li\u003e\n    \u003c/ul\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n\n```\n\n### main.js\nThis JavaScript script reads the browser's language and redirects accordingly.\n\n```js\n// Mapping of language codes to the respective language pages (relative paths)\nconst languagePaths = {\n  de: './de/index.html',\n  en: './en-us/index.html',\n  nl: './nl/index.html',\n  fr: './fr/index.html',\n  hu: './hu/index.html',\n  pt: './pt/index.html',\n  it: './it/index.html',\n  es: './es/index.html',\n  // ... more languages\n};\n\nfunction getBrowserLanguage() {\n  const lang = navigator.language || navigator.userLanguage;\n  return lang.split('-')[0];\n}\n\n// Executes a redirect to the appropriate language page, if available\nfunction redirectBasedOnLanguage(language) {\n  const fallbackUrl = languagePaths.de;\n\n  // Check whether the language is available in the mapping\n  if (!languagePaths.hasOwnProperty(language)) {\n    console.warn(`Language ${language} not supported. Redirecting to default.`);\n    window.location.href = fallbackUrl;\n    return;\n  }\n\n  const testUrl = languagePaths[language];\n\n  // HEAD request to verify if file exists\n  fetch(testUrl, { method: 'HEAD' })\n    .then(response =\u003e {\n      if (response.ok) {\n        // Page exists - forwarding\n        window.location.href = testUrl;\n      } else {\n        // Page does not exist - fallback\n        console.warn(`Page for ${language} not found. Redirecting to default.`);\n        window.location.href = fallbackUrl;\n      }\n    })\n    .catch(error =\u003e {\n      // Error retrieving the page - fallback\n      console.error(`Fetch error for ${language}: ${error.message}`);\n      window.location.href = fallbackUrl;\n    });\n}\n\n// On page load: automatic redirection based on the browser language\nwindow.onload = () =\u003e {\n  const initialLanguage = getBrowserLanguage();\n  redirectBasedOnLanguage(initialLanguage);\n};\n\n// Event listener for a manual language dropdown\ndocument.addEventListener('DOMContentLoaded', () =\u003e {\n  const languageSelect = document.getElementById('languageSelect');\n  if (languageSelect) {\n    languageSelect.addEventListener('change', () =\u003e {\n      const selectedLanguage = languageSelect.value;\n      redirectBasedOnLanguage(selectedLanguage);\n    });\n  }\n});\n```\n\n### Usage\n1. Create the directories and files as described above.\n2. Ensure your live server is running and accessing the folder.\n3. Open index.html in your browser to test the language redirection.\n\n## How it works\nThe code is a JavaScript file that implements a function for redirecting based on the selected language.\n\n1. `languagePaths`: An object that contains paths for different languages.\n2. `getBrowserLanguage()`: A function that returns the primary language code without regional specifications.\n3. `redirectBasedOnLanguage(language)`: A function that performs redirection based on the selected language.\n4. The code uses the Fetch API to check if the language URL exists.\n5. A timeout of 3 seconds is set to control the redirection.\n6. An event listener is added to monitor changes in the language selection.\n7. Upon page load, the initial language is determined, and the redirection is carried out accordingly.\n\nThe code ensures that users are redirected to the appropriate language page based on their language selection. If the selected language is not available, a redirection to the default page (German) occurs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fw45p85%2Flangchoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fw45p85%2Flangchoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fw45p85%2Flangchoose/lists"}