Skip to content

Commit

Permalink
Added examples for console applications; Minor bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkLiKally committed Oct 10, 2018
1 parent 77f0001 commit e4a1324
Show file tree
Hide file tree
Showing 69 changed files with 317 additions and 9 deletions.
14 changes: 13 additions & 1 deletion I18Next.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "I18Next.Net.Tests", "tests\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "I18Next.Net.AspNetCore", "src\I18Next.Net.AspNetCore\I18Next.Net.AspNetCore.csproj", "{AC1CFDD9-1DBC-4E21-A1CF-F9C154BBF177}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.WebApp", "examples\Example.WebApp\Example.WebApp.csproj", "{7A9BDD90-BC7A-4B7D-A3F0-42A83D04E6C3}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.WebApp", "samples\Example.WebApp\Example.WebApp.csproj", "{7A9BDD90-BC7A-4B7D-A3F0-42A83D04E6C3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "I18Next.Net.Abstractions", "src\I18Next.Net.Abstractions\I18Next.Net.Abstractions.csproj", "{42AB2562-8DA6-41E1-ABD5-152DDD622E6D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.ConsoleApp.NetCore", "samples\Example.ConsoleApp.NetCore\Example.ConsoleApp.NetCore.csproj", "{4F2A9939-3351-465F-9E4C-12616F64A0CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.ConsoleApp.NetFramework", "samples\Example.ConsoleApp.NetFramework\Example.ConsoleApp.NetFramework.csproj", "{15DAA359-C831-4260-9F6F-6255B7A45110}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -54,5 +58,13 @@ Global
{42AB2562-8DA6-41E1-ABD5-152DDD622E6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{42AB2562-8DA6-41E1-ABD5-152DDD622E6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{42AB2562-8DA6-41E1-ABD5-152DDD622E6D}.Release|Any CPU.Build.0 = Release|Any CPU
{4F2A9939-3351-465F-9E4C-12616F64A0CD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F2A9939-3351-465F-9E4C-12616F64A0CD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F2A9939-3351-465F-9E4C-12616F64A0CD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F2A9939-3351-465F-9E4C-12616F64A0CD}.Release|Any CPU.Build.0 = Release|Any CPU
{15DAA359-C831-4260-9F6F-6255B7A45110}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15DAA359-C831-4260-9F6F-6255B7A45110}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15DAA359-C831-4260-9F6F-6255B7A45110}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15DAA359-C831-4260-9F6F-6255B7A45110}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\I18Next.Net.Extensions\I18Next.Net.Extensions.csproj" />
<ProjectReference Include="..\..\src\I18Next.Net\I18Next.Net.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
</ItemGroup>

</Project>
114 changes: 114 additions & 0 deletions samples/Example.ConsoleApp.NetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using I18Next.Net;
using I18Next.Net.Backends;
using I18Next.Net.Extensions;
using I18Next.Net.Plugins;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Console = System.Console;

namespace Example.ConsoleApp.NetCore
{
class Program
{
private static ITranslationBackend _backend;

static void Main(string[] args)
{
SetupBackend();

SampleOne();

SampleTwo();

Console.ReadKey();
}

private static void SetupBackend()
{
var backend = new InMemoryBackend();

backend.AddTranslation("en", "translation", "exampleKey", "My English text.");
backend.AddTranslation("de", "translation", "exampleKey", "Mein deutscher text.");

_backend = backend;
}

private static void SampleOne()
{
Console.WriteLine("Sample one: Without Microsoft.Extensions.DependencyInjection");

var translator = new DefaultTranslator(_backend);

var i18next = new I18NextNet(_backend, translator);

Console.WriteLine("English translation:");
i18next.Language = "en";
Console.WriteLine(i18next.T("exampleKey"));

Console.WriteLine("German translation:");
i18next.Language = "de";
Console.WriteLine(i18next.T("exampleKey"));

Console.WriteLine();
}

private static void SampleTwo()
{
Console.WriteLine("Sample one: Using Microsoft.Extensions.DependencyInjection");

var services = new ServiceCollection();

// Register I18Next.Net
services.AddI18NextLocalization(builder => builder.AddBackend(_backend));

using(var serviceProvider = services.BuildServiceProvider())
using (var scope = serviceProvider.CreateScope())
{
var scopeProvider = scope.ServiceProvider;

Console.WriteLine("The first example uses the II18Next interface for direct access to I18Next");

var i18next = scopeProvider.GetService<II18Next>();

Console.WriteLine("English translation:");
i18next.Language = "en";
Console.WriteLine(i18next.T("exampleKey"));

Console.WriteLine("German translation:");
i18next.Language = "de";
Console.WriteLine(i18next.T("exampleKey"));


Console.WriteLine();
Console.WriteLine("The second example uses Microsofts IStringLocalizer interface for translations.");

var localizer = scopeProvider.GetService<IStringLocalizer>();

Console.WriteLine("English translation:");
i18next.Language = "en";
Console.WriteLine(localizer["exampleKey"]);

Console.WriteLine("German translation:");
i18next.Language = "de";
Console.WriteLine(localizer["exampleKey"]);


Console.WriteLine();
Console.WriteLine("It is also possible to use Microsofts IStringLocalizer<T> interface for translations.");

var localizerGeneric = scopeProvider.GetService<IStringLocalizer<Program>>();

Console.WriteLine("English translation:");
i18next.Language = "en";
Console.WriteLine(localizerGeneric["exampleKey"]);

Console.WriteLine("German translation:");
i18next.Language = "de";
Console.WriteLine(localizerGeneric["exampleKey"]);

Console.WriteLine();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{15DAA359-C831-4260-9F6F-6255B7A45110}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Example.ConsoleApp.NetFramework</RootNamespace>
<AssemblyName>Example.ConsoleApp.NetFramework</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\I18Next.Net.Abstractions\I18Next.Net.Abstractions.csproj">
<Project>{42ab2562-8da6-41e1-abd5-152ddd622e6d}</Project>
<Name>I18Next.Net.Abstractions</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\I18Next.Net\I18Next.Net.csproj">
<Project>{71f47bec-c9fb-4393-aa78-8d7ad2d51863}</Project>
<Name>I18Next.Net</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
41 changes: 41 additions & 0 deletions samples/Example.ConsoleApp.NetFramework/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using I18Next.Net;
using I18Next.Net.Backends;
using I18Next.Net.Plugins;

namespace Example.ConsoleApp.NetFramework
{
internal class Program
{
private static ITranslationBackend _backend;

public static void Main(string[] args)
{
SetupBackend();

var translator = new DefaultTranslator(_backend);

var i18next = new I18NextNet(_backend, translator);

Console.WriteLine("English translation:");
i18next.Language = "en";
Console.WriteLine(i18next.T("exampleKey"));

Console.WriteLine("German translation:");
i18next.Language = "de";
Console.WriteLine(i18next.T("exampleKey"));

Console.ReadKey();
}

private static void SetupBackend()
{
var backend = new InMemoryBackend();

backend.AddTranslation("en", "translation", "exampleKey", "My English text.");
backend.AddTranslation("de", "translation", "exampleKey", "Mein deutscher text.");

_backend = backend;
}
}
}
35 changes: 35 additions & 0 deletions samples/Example.ConsoleApp.NetFramework/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Example.ConsoleApp.NetFramework")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Example.ConsoleApp.NetFramework")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("15DAA359-C831-4260-9F6F-6255B7A45110")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 18 additions & 7 deletions src/I18Next.Net.Extensions/Builder/I18NextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using I18Next.Net.Backends;
using I18Next.Net.Plugins;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -205,18 +206,19 @@ public I18NextBuilder AddTranslator<T>(Func<IServiceProvider, T> factory)

public void Build()
{
AddSingletonIfNotPresent<ILogger, DefaultExtensionsLogger>();
AddSingletonIfNotPresent(DefaultLoggerFactory);
AddSingletonIfNotPresent<IPluralResolver, DefaultPluralResolver>();
AddSingletonIfNotPresent<ILanguageDetector, DefaultLanguageDetector>(DefaultLanguageDetectorFactory);
AddSingletonIfNotPresent<ILanguageDetector>(DefaultLanguageDetectorFactory);
AddSingletonIfNotPresent<ITranslationBackend, JsonFileBackend>();
AddSingletonIfNotPresent<ITranslator, DefaultTranslator>(DefaultTranslatorFactory);
AddSingletonIfNotPresent<IInterpolator, DefaultInterpolator>(DefaultInterpolatorFactory);
AddSingletonIfNotPresent<ITranslator>(DefaultTranslatorFactory);
AddSingletonIfNotPresent<IInterpolator>(DefaultInterpolatorFactory);

Services.AddSingleton<II18NextFactory, I18NextFactory>();
Services.AddSingleton(c => c.GetRequiredService<II18NextFactory>().CreateInstance());

Services.AddSingleton<IStringLocalizerFactory, I18NextStringLocalizerFactory>();
Services.AddTransient(typeof(IStringLocalizer<>), typeof(StringLocalizer<>));
Services.TryAddTransient(typeof(IStringLocalizer), c => c.GetRequiredService<IStringLocalizerFactory>().Create(null));
}

public I18NextBuilder Configure(Action<I18NextOptions> configure)
Expand Down Expand Up @@ -254,12 +256,11 @@ private void AddSingletonIfNotPresent<TService, TImplementation>()
Services.AddSingleton<TService, TImplementation>();
}

private void AddSingletonIfNotPresent<TService, TImplementation>(Func<IServiceProvider, TImplementation> factory)
where TImplementation : class, TService
private void AddSingletonIfNotPresent<TService>(Func<IServiceProvider, TService> factory)
where TService : class
{
if (Services.All(s => s.ServiceType != typeof(TService)))
Services.AddSingleton<TService, TImplementation>(factory);
Services.AddSingleton(factory);
}

private DefaultInterpolator DefaultInterpolatorFactory(IServiceProvider c)
Expand All @@ -280,6 +281,16 @@ private DefaultLanguageDetector DefaultLanguageDetectorFactory(IServiceProvider
return new DefaultLanguageDetector(options.Value.DefaultLanguage);
}

private ILogger DefaultLoggerFactory(IServiceProvider c)
{
var msLogger = c.GetService<Microsoft.Extensions.Logging.ILogger>();

if (msLogger != null)
return new DefaultExtensionsLogger(msLogger);

return new TraceLogger();
}

private DefaultTranslator DefaultTranslatorFactory(IServiceProvider c)
{
var backend = c.GetRequiredService<ITranslationBackend>();
Expand Down
2 changes: 1 addition & 1 deletion src/I18Next.Net/Backends/InMemoryBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Task<ITranslationTree> LoadNamespaceAsync(string language, string @namesp
treeKey = BackendUtilities.GetLanguagePart(language) + "_" + @namespace;

if (!_namespaces.TryGetValue(treeKey, out tree))
return null;
return Task.FromResult(default(ITranslationTree));

return Task.FromResult(tree as ITranslationTree);
}
Expand Down
Loading

0 comments on commit e4a1324

Please sign in to comment.