Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. using System.Web;
  2. using FairlyLocal;
  3.  
  4. namespace lib.i18n
  5. {
  6.  
  7.     public interface I18NSupport
  8.     {
  9.         string _(string text);
  10.         string LanguageCode { get; set; }
  11.     }
  12.  
  13.     public class I18NHelper : lib.i18n.I18NSupport
  14.     {
  15.  
  16.         #region internationalization
  17.  
  18.         string _languageCode;
  19.  
  20.         /// <summary>
  21.         /// Get the current user's language preference.
  22.         /// First looks for stored preference in the Session.
  23.         /// Falls back on the best match from the browser's language collection.
  24.         /// </summary>
  25.         public string LanguageCode
  26.         {
  27.             get
  28.             {
  29.                 if (_languageCode == null)
  30.                 {
  31.                     if (HttpContext.Current.Session[Internationalization.Settings.LanguageCodeSessionKey] != null)
  32.                     {
  33.                         _languageCode = HttpContext.Current.Session[Internationalization.Settings.LanguageCodeSessionKey].ToString();
  34.                     }
  35.                     else
  36.                     {
  37.                         _languageCode = Internationalization.GetBestLanguage(HttpContext.Current.Request, Internationalization.Settings.WorkingLanguage);
  38.                         HttpContext.Current.Session[Internationalization.Settings.LanguageCodeSessionKey] = _languageCode;
  39.                     }
  40.                 }
  41.                 return _languageCode;
  42.             }
  43.             set
  44.             {
  45.                 _languageCode = value;
  46.                 HttpContext.Current.Session[Internationalization.Settings.LanguageCodeSessionKey] = _languageCode;
  47.             }
  48.         }
  49.  
  50.         /// <summary>
  51.         /// Returns the local translation of this piece of text.
  52.         /// Shorthand for Internationalization.GetText.
  53.         /// </summary>
  54.         /// <param name="text"></param>
  55.         /// <returns></returns>
  56.         public string _(string text)
  57.         {
  58.             return Internationalization.GetText(text, LanguageCode);
  59.         }
  60.  
  61.         #endregion
  62.     }
  63. }
  64.  
  65.