<commit_before>﻿@using System.Globalization
@using System.Web.Hosting
@using jQuery.Validation.Unobtrusive.Native.Demos.Models
@model GlobalizeModel
@functions{
    /// <summary>
    /// Identifies and returns the default locale to use by mapping as close as possible from ASP.Nets culture to Globalize's locales
    /// </summary>
    /// <returns>The default locale to use for the current culture; eg "de"</returns>
    public string GetDefaultLocale()
    {
        const string localePattern = "~/bower_components/cldr-data/main/{0}"; // where cldr-data lives on disk
        var currentCulture = CultureInfo.CurrentCulture;
        var cultureToUse = "en-GB"; //Default regionalisation to use

        //Try to pick a more appropriate regionalisation
        if (Directory.Exists(HostingEnvironment.MapPath(string.Format(localePattern, currentCulture.Name)))) //First try for a en-GB style directory
            cultureToUse = currentCulture.Name;
        else if (Directory.Exists(HostingEnvironment.MapPath(string.Format(localePattern, currentCulture.TwoLetterISOLanguageName)))) //That failed; now try for a en style directory
            cultureToUse = currentCulture.TwoLetterISOLanguageName;

        return cultureToUse;
    }
}
@{
    var defaultLocale = GetDefaultLocale();
}
@section metatags{
<meta name="Description" content="A demo of internationalized / locale-specific validation using Globalize.">
}
@section scripts
{
    @Scripts.Render(
        "~/bundles/jquery-validation",
        "~/bower_components/cldrjs/dist/cldr.js",
        "~/bower_components/cldrjs/dist/cldr/event.js",
        "~/bower_components/cldrjs/dist/cldr/supplemental.js",
        "~/bower_components/globalize/dist/globalize.js",
        "~/bower_components/globalize/dist/globalize/number.js",
        "~/bower_components/globalize/dist/globalize/date.js",
        "~/Scripts/jquery.validate.globalize.js")
<script>
    function getJsonFilenames(locale, loadedData) {

        return [
            "supplemental/likelySubtags.json",
            "main/{locale}/numbers.json",
            "supplemental/numberingSystems.json",
            "main/{locale}/ca-gregorian.json",
            "main/{locale}/timeZoneNames.json",
            "supplemental/timeData.json",
            "supplemental/weekData.json",
            "main/{locale}/languages.json"
        ].filter(function (f) {
            var noDataLoaded = loadedData.length === 0;
            if (noDataLoaded) {
                return true;
            }
            var localeNotYetLoaded = loadedData.indexOf(locale) === -1;
            if (localeNotYetLoaded) {
                var fileIsCommonAndSoAlreadyLoaded = f.indexOf("supplemental/" !== -1);
                return fileIsCommonAndSoAlreadyLoaded;
            }
            return false;
        }).map(function (f) { return "../bower_components/cldr-data/" + f.replace("{locale}", locale); });
    }

    function loadLocale(locale, loadedData) {

        var promise = $.Deferred();
        var jsonToLoad = getJsonFilenames(locale, loadedData);
        if (jsonToLoad.length === 0) {
            Globalize.locale(locale); // Presumably already loaded
            promise.resolve(locale + " selected...")
            return promise;
        }

        return $.when.apply(
            this, jsonToLoad.map($.get)
        ).then(function () {
            // Normalize $.get results, we only need the JSON, not the request statuses.
            return [].slice.apply(arguments, [0]).map(function (result) {
                return result[0];
            });
        }).then(Globalize.load).then(function () {
            loadedData.push(locale);
            Globalize.locale(locale);
            return locale + " loaded and selected..."
        });
    }

    var localesLoaded = [];

    $("form").validate({
        submitHandler: function (form) {
            alert("This is a valid form!");
        }
    });

    $("#localeSelector").change(function () {
        var $this = $(this);
        var $exampleFormats = $("#exampleFormats");
        var $loading = $("#loading");

        $this.prop("disabled", true);
        $loading.show();
        $exampleFormats.hide();
        
        var localeToLoad = $this.val();

        loadLocale(localeToLoad, localesLoaded)
            .then(function (result) {
                $this.prop("disabled", false);
                $loading.hide();
                var languageName = Globalize.locale().main('localeDisplayNames/languages/' + locale);
                var exampleFormats = languageName + ": A number may look like this: " + Globalize.numberFormatter()(11.7) +
                    " and a date may look like this: " + Globalize.dateFormatter()(new Date());
                $exampleFormats.text(exampleFormats).show();
            }, function (error) {
                $exampleFormats.text("Problem loading locale: " + error).show();
                $this.prop("disabled", false);
                $loading.hide();
            });
    }).change();

    // Show warning if this is a static page
    if (window.location.href.toLowerCase().indexOf("globalize.html") !== -1) {
        $(".static-warning").show();
    }
</script>
}

<h3>@ViewBag.Title</h3>

<p>If you want to patch the jQuery Validation date and number validation to be locale specific then you can using <code><a href="https://github.com/johnnyreilly/jquery-validation-globalize" target="_blank">jquery-validate-globalize.js</a></code>. This demo is not strictly related to jVUN - it's really more about Globalize and jquery-validation-globalize. It's built using jVUN but if you're here strictly for information about jquery-validation-globalize then just ignore the "Model" and "ASP.Net and Globalize" tabs below.  Everything else is just normal JavaScript and jQuery Validation code.</p>

<p>jquery-validation-globalize is available on Bower.  To install it you should first ensure you have a <code>.bowerrc</code> file in place which looks like this:</p>

<pre class="prettyprint js">
{
  "scripts": {
    "preinstall": "npm install cldr-data-downloader@0.2.x",
    "postinstall": "node ./node_modules/cldr-data-downloader/bin/download.js -i bower_components/cldr-data/index.json -o bower_components/cldr-data/"<commit_msg>Update Globalize.cshtml<commit_after>﻿@using System.Globalization
@using System.Web.Hosting
@using jQuery.Validation.Unobtrusive.Native.Demos.Models
@model GlobalizeModel
@functions{
    /// <summary>
    /// Identifies and returns the default locale to use by mapping as close as possible from ASP.Nets culture to Globalize's locales
    /// </summary>
    /// <returns>The default locale to use for the current culture; eg "de"</returns>
    public string GetDefaultLocale()
    {
        const string localePattern = "~/bower_components/cldr-data/main/{0}"; // where cldr-data lives on disk
        var currentCulture = CultureInfo.CurrentCulture;
        var cultureToUse = "en-GB"; //Default regionalisation to use

        //Try to pick a more appropriate regionalisation
        if (Directory.Exists(HostingEnvironment.MapPath(string.Format(localePattern, currentCulture.Name)))) //First try for a en-GB style directory
            cultureToUse = currentCulture.Name;
        else if (Directory.Exists(HostingEnvironment.MapPath(string.Format(localePattern, currentCulture.TwoLetterISOLanguageName)))) //That failed; now try for a en style directory
            cultureToUse = currentCulture.TwoLetterISOLanguageName;

        return cultureToUse;
    }
}
@{
    var defaultLocale = GetDefaultLocale();
}
@section metatags{
<meta name="Description" content="A demo of internationalized / locale-specific validation using Globalize.">
}
@section scripts
{
    @Scripts.Render(
        "~/bundles/jquery-validation",
        "~/bower_components/cldrjs/dist/cldr.js",
        "~/bower_components/cldrjs/dist/cldr/event.js",
        "~/bower_components/cldrjs/dist/cldr/supplemental.js",
        "~/bower_components/globalize/dist/globalize.js",
        "~/bower_components/globalize/dist/globalize/number.js",
        "~/bower_components/globalize/dist/globalize/date.js",
        "~/Scripts/jquery.validate.globalize.js")
<script>
    function getJsonFilenames(locale, loadedData) {

        return [
            "supplemental/likelySubtags.json",
            "main/{locale}/numbers.json",
            "supplemental/numberingSystems.json",
            "main/{locale}/ca-gregorian.json",
            "main/{locale}/timeZoneNames.json",
            "supplemental/timeData.json",
            "supplemental/weekData.json",
            "main/{locale}/languages.json"
        ].filter(function (f) {
            var noDataLoaded = loadedData.length === 0;
            if (noDataLoaded) {
                return true;
            }
            var localeNotYetLoaded = loadedData.indexOf(locale) === -1;
            if (localeNotYetLoaded) {
                var fileIsCommonAndSoAlreadyLoaded = f.indexOf("supplemental/" !== -1);
                return fileIsCommonAndSoAlreadyLoaded;
            }
            return false;
        }).map(function (f) { return "../bower_components/cldr-data/" + f.replace("{locale}", locale); });
    }

    function loadLocale(locale, loadedData) {

        var promise = $.Deferred();
        var jsonToLoad = getJsonFilenames(locale, loadedData);
        if (jsonToLoad.length === 0) {
            Globalize.locale(locale); // Presumably already loaded
            promise.resolve(locale + " selected...")
            return promise;
        }

        return $.when.apply(
            this, jsonToLoad.map($.get)
        ).then(function () {
            // Normalize $.get results, we only need the JSON, not the request statuses.
            return [].slice.apply(arguments, [0]).map(function (result) {
                return result[0];
            });
        }).then(Globalize.load).then(function () {
            loadedData.push(locale);
            Globalize.locale(locale);
            return locale + " loaded and selected..."
        });
    }

    var localesLoaded = [];

    $("form").validate({
        submitHandler: function (form) {
            alert("This is a valid form!");
        }
    });

    $("#localeSelector").change(function () {
        var $this = $(this);
        var $exampleFormats = $("#exampleFormats");
        var $loading = $("#loading");

        $this.prop("disabled", true);
        $loading.show();
        $exampleFormats.hide();
        
        var localeToLoad = $this.val();

        loadLocale(localeToLoad, localesLoaded)
            .then(function (result) {
                $this.prop("disabled", false);
                $loading.hide();
                var languageName = Globalize.locale().main('localeDisplayNames/languages/' + locale);
                languageName = (languageName ? languageName + ': ' : '');
                var exampleFormats = languageName + "A number may look like this: " + Globalize.numberFormatter()(11.7) +
                    " and a date may look like this: " + Globalize.dateFormatter()(new Date());
                $exampleFormats.text(exampleFormats).show();
            }, function (error) {
                $exampleFormats.text("Problem loading locale: " + error).show();
                $this.prop("disabled", false);
                $loading.hide();
            });
    }).change();

    // Show warning if this is a static page
    if (window.location.href.toLowerCase().indexOf("globalize.html") !== -1) {
        $(".static-warning").show();
    }
</script>
}

<h3>@ViewBag.Title</h3>

<p>If you want to patch the jQuery Validation date and number validation to be locale specific then you can using <code><a href="https://github.com/johnnyreilly/jquery-validation-globalize" target="_blank">jquery-validate-globalize.js</a></code>. This demo is not strictly related to jVUN - it's really more about Globalize and jquery-validation-globalize. It's built using jVUN but if you're here strictly for information about jquery-validation-globalize then just ignore the "Model" and "ASP.Net and Globalize" tabs below.  Everything else is just normal JavaScript and jQuery Validation code.</p>

<p>jquery-validation-globalize is available on Bower.  To install it you should first ensure you have a <code>.bowerrc</code> file in place which looks like this:</p>

<pre class="prettyprint js">
{
  "scripts": {
    "preinstall": "npm install cldr-data-downloader@0.2.x",
    "postinstall": "node ./node_modules/cldr-data-downloader/bin/download.js -i bower_components/cldr-data/index.json -o bower_components/cldr-data/"
{'fraction_non_alphanumeric': 0.12811557390061915, 'fraction_numerical': 0.002381330369899984, 'mean_word_length': 2.5590395480225987, 'pattern_counts': {'":': 7, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3621294', 'n_tokens_mistral': 3559, 'n_tokens_neox': 3430, 'n_words': 831}