Created
May 27, 2018 16:25
-
-
Save cagcak/1e32ad51d00434ae554a5f97dbb845dd to your computer and use it in GitHub Desktop.
Slider switch functionality for jquery-i18n localization in a html partial view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="./node_modules/jquery/dist/jquery.min.js"></script> | |
<input id="i18n-switch" type="checkbox" data-toggle="toggle" data-on="EN" data-off="TR"> | |
<script type="text/javascript"> | |
const getRoot = function () { | |
return window.document.location.origin.toString() | |
} | |
const CONF = { | |
prefix: '?locale=', | |
lang: ['en', 'tr'], | |
ext: '.json', | |
tmp: '', | |
path: '/js/i18n/', | |
root: getRoot() | |
} | |
if (window.name === '') { | |
window.name = CONF.lang[1] | |
} | |
if (window.location.search === CONF.prefix + CONF.lang[1]) { | |
$(window).ready(function () { // eslint-disable-line | |
window.name = CONF.lang[1] | |
$('#i18n-switch').prop('checked', true).change() // eslint-disable-line | |
}) | |
} else if (window.location.search === CONF.prefix + CONF.lang[0]) { | |
$(window).ready(function () { // eslint-disable-line | |
window.name = CONF.lang[0] | |
$('#i18n-switch').prop('checked', false).change() // eslint-disable-line | |
}) | |
} | |
var SetLocaleTo = function (locale) { | |
if (locale) { | |
$.i18n().locale = locale // eslint-disable-line | |
} | |
$('body').i18n() // eslint-disable-line | |
$('head').i18n() // eslint-disable-line | |
} | |
jQuery(function () { // eslint-disable-line | |
$.i18n().load({ // eslint-disable-line | |
'en': CONF.root + CONF.tmp + CONF.path + CONF.lang[0] + CONF.ext, | |
'tr': CONF.root + CONF.tmp + CONF.path + CONF.lang[1] + CONF.ext | |
}).done(function () { | |
SetLocaleTo(url(CONF.prefix.substring(0, 7))) // eslint-disable-line | |
History.Adapter.bind(window, 'statechange', function () { // eslint-disable-line | |
SetLocaleTo(url(CONF.prefix.substring(0, 7))) // eslint-disable-line | |
}) | |
if (window.name === CONF.lang[1]) { | |
History.pushState(null, null, CONF.prefix + CONF.lang[1]) // eslint-disable-line | |
$('#i18n-switch').prop('checked', true).change() // eslint-disable-line | |
} else if (window.name === CONF.lang[0]) { | |
History.pushState(null, null, CONF.prefix + CONF.lang[0]) // eslint-disable-line | |
$('#i18n-switch').prop('checked', false).change() // eslint-disable-line | |
} | |
$('#i18n-switch').change(function () { // eslint-disable-line | |
if ($(this).is(':checked')) { // eslint-disable-line | |
History.pushState(null, null, CONF.prefix + CONF.lang[1]) // eslint-disable-line | |
window.name = CONF.lang[1] | |
$.getJSON(CONF.root + CONF.tmp + CONF.path + CONF.lang[1] + CONF.ext, function (data) { // eslint-disable-line | |
document.title = data.SiteName | |
}) | |
} else { | |
History.pushState(null, null, CONF.prefix + CONF.lang[0]) // eslint-disable-line | |
window.name = CONF.lang[0] | |
$.getJSON(CONF.root + CONF.tmp + CONF.path + CONF.lang[0] + CONF.ext, function (data) { // eslint-disable-line | |
document.title = data.SiteName | |
}) | |
} | |
}) | |
}) | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment