Skip to content

Instantly share code, notes, and snippets.

@jhauge
Created December 12, 2012 11:33
Show Gist options
  • Save jhauge/4267119 to your computer and use it in GitHub Desktop.
Save jhauge/4267119 to your computer and use it in GitHub Desktop.
Making current Umbraco/ASP.NET locale/language available in javascript
<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true"
CodeBehind="Master.master.cs" Inherits="Web.masterpages.Master" %>
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolderDefault"><!doctype html>
<html class="no-js" lang="en">
<head>
</head>
<body>
</body>
<!--
Script to load a variable telling about the current language
Insert before other scripts
-->
<script>
// This script will create a variable with the two-letter language code
// The value will allways be available as UI.currentUiLanguage
var UI = UI || {}; // Namespacing
UI.currentUiLanguage = '<%= CurrentUiLanguage %>'; // Will be written out from Masters codebehind
</script>
</html>
</asp:Content>
using System;
using System.Web.UI;
using umbraco.NodeFactory;
namespace Web.masterpages
{
public partial class Master : MasterPage
{
private readonly Node _currentNode = Node.GetCurrent();
public Master()
{
Init += PageInit;
}
public string CurrentUiLanguage { get; set; }
private void PageInit(object sender, EventArgs e)
{
// Set the value of CurrentUiLanguae, using default ASP.NET access method
// This property is what the Umbraco API, sets when determining the language from
// assigned domains
CurrentUiLanguage = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment