Skip to content

Commit

Permalink
Added languages #243
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Jun 24, 2016
1 parent 2ec27dc commit bcdb612
Show file tree
Hide file tree
Showing 16 changed files with 700 additions and 50 deletions.
18 changes: 0 additions & 18 deletions PlexRequests.Resources/PlexRequests.Resources.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UI.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>UI.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="UI.da.resx" />
<EmbeddedResource Include="UI.de.resx" />
<EmbeddedResource Include="UI.es.resx" />
<EmbeddedResource Include="UI.fr.resx" />
<EmbeddedResource Include="UI.it.resx" />
<EmbeddedResource Include="UI.pt.resx" />
<EmbeddedResource Include="UI.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>UI.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="UI.sv.resx" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
46 changes: 45 additions & 1 deletion PlexRequests.UI/Modules/BaseModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

using Nancy;

using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.UI.Helpers;
using PlexRequests.UI.Models;

namespace PlexRequests.UI.Modules
{
public abstract class BaseModule : NancyModule
{
protected string BaseUrl { get; set; }


protected BaseModule(ISettingsService<PlexRequestSettings> settingsService)
{

var settings = settingsService.GetSettings();
var baseUrl = settings.BaseUrl;
BaseUrl = baseUrl;

var modulePath = string.IsNullOrEmpty(baseUrl) ? string.Empty : baseUrl;

ModulePath = modulePath;

Before += (ctx) => SetCookie();
}

protected BaseModule(string modulePath, ISettingsService<PlexRequestSettings> settingsService)
{

var settings = settingsService.GetSettings();
var baseUrl = settings.BaseUrl;
BaseUrl = baseUrl;

var settingModulePath = string.IsNullOrEmpty(baseUrl) ? modulePath : $"{baseUrl}/{modulePath}";

ModulePath = settingModulePath;

Before += (ctx) => SetCookie();
}

private int _dateTimeOffset = -1;
Expand Down Expand Up @@ -96,7 +105,7 @@ protected string Username
}
}

protected IDictionary<string, string> Cookies => Request.Cookies;
protected IDictionary<string, string> Cookies => Request?.Cookies;

protected bool IsAdmin
{
Expand All @@ -110,6 +119,41 @@ protected bool IsAdmin
return claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser);
}
}
protected string Culture { get; set; }
protected const string CultureCookieName = "_culture";
protected Response SetCookie()
{
try
{
string cultureName;

// Attempt to read the culture cookie from Request
var outCookie = string.Empty;
if (Cookies.TryGetValue(CultureCookieName, out outCookie))
{
cultureName = outCookie;
}
else
{
cultureName = Request.Headers?.AcceptLanguage?.FirstOrDefault()?.Item1;
}

// Validate culture name
cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe


// Modify current thread's cultures
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

Culture = Thread.CurrentThread.CurrentCulture.Name;
}
catch (Exception)
{
// Couldn't Set the culture
}
return null;
}

}
}
17 changes: 13 additions & 4 deletions PlexRequests.UI/Modules/CultureModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,27 @@ public class CultureModule : BaseModule
private const string CookieName = "_culture";
public CultureModule(ISettingsService<PlexRequestSettings> pr) : base("culture",pr)
{
Get["/culture"] = x => SetCulture();
Get["/"] = x => SetCulture();
}

public RedirectResponse SetCulture()
{
var culture = (string)Request.Query["culture"];
var returnUrl = (string)Request.Query["returnUrl"];
var culture = (string)Request.Query["l"];
var returnUrl = (string)Request.Query["u"];

// Validate
culture = CultureHelper.GetImplementedCulture(culture);

var cookie = Request.Cookies["_culture"];
var outCookie = string.Empty;
if (Cookies.TryGetValue(CookieName, out outCookie))
{
Cookies[CookieName] = culture;
}
else
{
Cookies.Add(CookieName, culture);
}
var cookie = Cookies["_culture"];
var response = Context.GetRedirect(returnUrl);

response.WithCookie(CookieName, cookie ?? culture, DateTime.Now.AddYears(1));
Expand Down
23 changes: 18 additions & 5 deletions PlexRequests.UI/PlexRequests.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@
<Compile Include="Modules\IssuesModule.cs" />
<Compile Include="Modules\LandingPageModule.cs" />
<Compile Include="Modules\UpdateCheckerModule.cs" />
<Compile Include="Resources\UI.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>UI.resx</DependentUpon>
</Compile>
<Compile Include="Start\StartupOptions.cs" />
<Compile Include="Start\UpdateValue.cs" />
<Compile Include="Validators\SlackSettingsValidator.cs" />
Expand Down Expand Up @@ -571,7 +576,19 @@
<DependentUpon>web.config</DependentUpon>
</None>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<EmbeddedResource Include="Resources\UI.da.resx" />
<EmbeddedResource Include="Resources\UI.de.resx" />
<EmbeddedResource Include="Resources\UI.es.resx" />
<EmbeddedResource Include="Resources\UI.fr.resx" />
<EmbeddedResource Include="Resources\UI.it.resx" />
<EmbeddedResource Include="Resources\UI.pt.resx" />
<EmbeddedResource Include="Resources\UI.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>UI.Designer.cs</LastGenOutput>
</EmbeddedResource>
<EmbeddedResource Include="Resources\UI.sv.resx" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PlexRequests.Api.Interfaces\PlexRequests.Api.Interfaces.csproj">
<Project>{95834072-A675-415D-AA8F-877C91623810}</Project>
Expand All @@ -593,10 +610,6 @@
<Project>{1252336D-42A3-482A-804C-836E60173DFA}</Project>
<Name>PlexRequests.Helpers</Name>
</ProjectReference>
<ProjectReference Include="..\PlexRequests.Resources\PlexRequests.Resources.csproj">
<Project>{9c266462-be82-461a-87a2-9edcfb95d732}</Project>
<Name>PlexRequests.Resources</Name>
</ProjectReference>
<ProjectReference Include="..\PlexRequests.Services\PlexRequests.Services.csproj">
<Project>{566EFA49-68F8-4716-9693-A6B3F2624DEA}</Project>
<Name>PlexRequests.Services</Name>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bcdb612

Please sign in to comment.