Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0;</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion Examples/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<TargetFrameworks>net8.0;net9.0;</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
Expand Down
4 changes: 4 additions & 0 deletions Examples/Slang.Console/Features/Feature2/Feature2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Slang.Console.Features.Feature2;

[Translations(InputFileName = "feature2")]
internal partial class Feature2;
4 changes: 4 additions & 0 deletions Examples/Slang.Console/Features/Feature3/Feature3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Slang.Console.Features.Feature3;

[Translations(InputFileName = "feature3", StartCharacter = "{{", EndCharacter = "}}")]
internal partial class Feature3;
15 changes: 15 additions & 0 deletions Examples/Slang.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Slang.Console;
using Slang.Console.Features.Feature1;
using Slang.Console.Features.Feature2;
using Slang.Console.Features.Feature3;

foreach (var culture in Feature1.SupportedCultures)
{
Expand Down Expand Up @@ -48,4 +50,17 @@ void ShowLocales()
Console.WriteLine(formattingBloc.TimeSpanExample(TimeSpan.FromMinutes(13)));
Console.WriteLine(formattingBloc.ObjectExample2("Hello World!"));
Console.WriteLine(formattingBloc.FloatExample(123.31414f));

var formattingBloc2 = Feature2.Instance.Root.Formatting;

string price2 = formattingBloc2.DecimalExample(12.23123M);

Console.WriteLine(price2);
Console.WriteLine(formattingBloc2.LongExample(124214));
Console.WriteLine(formattingBloc2.IntExample(123));
Console.WriteLine(formattingBloc2.TimeSpanExample(TimeSpan.FromMinutes(13)));
Console.WriteLine(formattingBloc2.ObjectExample2("Hello World!"));
Console.WriteLine(formattingBloc2.FloatExample(123.31414f));

Console.WriteLine(Feature3.Instance.Root.SomeExt(3));
}
5 changes: 3 additions & 2 deletions Examples/Slang.Console/Slang.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="i18n\*.i18n.json" />
<AdditionalFiles Include="slang.json" />
<AdditionalFiles Include="i18n\*.i18n.json"/>
<AdditionalFiles Include="i18n\*.i18n.yaml"/>
<AdditionalFiles Include="slang.json"/>
</ItemGroup>

<ItemGroup>
Expand Down
82 changes: 82 additions & 0 deletions Examples/Slang.Console/i18n/feature2_en-US.i18n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
formatting:
doubleExample: 'Double formatting: {value}'
'@doubleExample':
placeholders:
value:
type: double
format: F2
doubleExample2: 'Double formatting: {value:double}'
'@doubleExample2':
placeholders:
value:
format: F3
objectExample2: 'Object formatting: {value}'
'@objectExample2':
placeholders:
value:
format: Qwerty {0} qwerty
intExample: 'Int formatting: {value}'
'@intExample':
placeholders:
value:
type: int
format: X
longExample: 'Long formatting: {value}'
'@longExample':
placeholders:
value:
type: long
format: N
decimalExample: 'Decimal formatting: {value}'
'@decimalExample':
placeholders:
value:
type: decimal
format: C2
floatExample: 'Float formatting: {value}'
'@floatExample':
placeholders:
value:
type: float
format: F2
dateExample: Date {date}
'@dateExample':
placeholders:
date:
type: DateTime
format: dd MMMM HH:mm
dateOnlyExample: Date only {date}
'@dateOnlyExample':
placeholders:
date:
type: DateOnly
format: dd MMMM
timeOnlyExample: Time only {time}
'@timeOnlyExample':
placeholders:
time:
type: TimeOnly
format: HH:mm
timeSpanExample: Timespan only {time}
'@timeSpanExample':
placeholders:
time:
type: TimeSpan
format: t
'@formatting':
description: Formatting bloc comment
screen:
data: Date {date}
'@data':
description: Title of the day in the schedule of visits
placeholders:
date:
type: DateOnly
format: dd MMMM
test_param: 'param: {param:double}'
profile_rating: 'My rating: {rating}'
'@profile_rating':
placeholders:
rating:
type: double
format: F2
82 changes: 82 additions & 0 deletions Examples/Slang.Console/i18n/feature2_ru-RU.i18n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
formatting:
doubleExample: 'Double formatting: {value}'
'@doubleExample':
placeholders:
value:
type: double
format: F2
doubleExample2: 'Double formatting: {value:double}'
'@doubleExample2':
placeholders:
value:
format: F3
objectExample2: 'Object formatting: {value}'
'@objectExample2':
placeholders:
value:
format: Qwerty {0} qwerty
intExample: 'Int formatting: {value}'
'@intExample':
placeholders:
value:
type: int
format: X
longExample: 'Long formatting: {value}'
'@longExample':
placeholders:
value:
type: long
format: N
decimalExample: 'Decimal formatting: {value}'
'@decimalExample':
placeholders:
value:
type: decimal
format: C2
floatExample: 'Float formatting: {value}'
'@floatExample':
placeholders:
value:
type: float
format: F2
dateExample: Date {date}
'@dateExample':
placeholders:
date:
type: DateTime
format: dd MMMM HH:mm
dateOnlyExample: Date only {date}
'@dateOnlyExample':
placeholders:
date:
type: DateOnly
format: dd MMMM
timeOnlyExample: Time only {time}
'@timeOnlyExample':
placeholders:
time:
type: TimeOnly
format: HH:mm
timeSpanExample: Timespan only {time}
'@timeSpanExample':
placeholders:
time:
type: TimeSpan
format: t
'@formatting':
description: Formatting bloc comment
screen:
data: Дата {date}
'@data':
description: Title of the day in the schedule of visits
placeholders:
date:
type: DateOnly
format: dd MMMM
test_param: 'param: {param:double}'
profile_rating: 'Мой рейтинг: {rating}'
'@profile_rating':
placeholders:
rating:
type: double
format: F2
2 changes: 2 additions & 0 deletions Examples/Slang.Console/i18n/feature3_ru-RU.i18n.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SomeJson: '[{"aaa": "bbb"}]'
SomeExt: 'asd {{some: int}}'
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />

<PackageReference Include="Slang.Net" Version="9.7.0" />
<PackageReference Include="Slang.Net" Version="10.0.0" />

</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion Examples/Slang.WebApi/Slang.WebApi.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<ItemGroup>
<PackageReference Include="Slang.Net" Version="9.7.0" />
<PackageReference Include="Slang.Net" Version="10.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value="Slang.Net/bin/Debug" />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
7 changes: 5 additions & 2 deletions Slang.Generator/TranslateGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
generationInfoWithErrors.Select(static (item, _) => item.Value)!;

var jsonFiles = context.AdditionalTextsProvider
.Where(file => file.Path.EndsWith(Constants.AdditionalFilePattern))
.Where(file =>
file.Path.EndsWith(Constants.AdditionalFileJsonPattern) ||
file.Path.EndsWith(Constants.AdditionalFileYamlPattern))
.Select((file, cancellationToken) => new
{
FileName = Path.GetFileName(file.Path),
Expand Down Expand Up @@ -141,5 +143,6 @@ private static TranslationsParam ValidateTargetTypeAndGetInfo(AttributeData attr

internal record GlobalConfigDto
{
[JsonPropertyName("base_culture")] public string? BaseCulture { get; set; }
[JsonPropertyName("base_culture")]
public string? BaseCulture { get; set; }
}
4 changes: 4 additions & 0 deletions Slang.Net/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class TranslationsAttribute: Attribute
public PluralAuto PluralAuto { get; set; } = PluralAuto.Cardinal;

public string? PluralParameter { get; set; }

public string StartCharacter { get; set; } = "{";

public string EndCharacter { get; set; } = "}";
}

#if SLANG_ATTRIBUTES_PACKAGE
Expand Down
1 change: 1 addition & 0 deletions Slang.Net/CodeBuilder/TranslationsCodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static async Task Generate(
RawConfig config,
SlangFileCollection fileCollection)
{
//Debugger.Launch();
// STEP 2: scan translations
var translationMap = await TranslationsRepository.Build(config.BaseLocale, fileCollection: fileCollection);

Expand Down
3 changes: 2 additions & 1 deletion Slang.Net/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ namespace Slang.Shared;

public static class Constants
{
public const string AdditionalFilePattern = ".i18n.json";
public const string AdditionalFileJsonPattern = ".i18n.json";
public const string AdditionalFileYamlPattern = ".i18n.yaml";
}
11 changes: 8 additions & 3 deletions Slang.Net/Data/ConfigRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ namespace Slang.Generator.Core.Data;

public static class ConfigRepository
{
public static RawConfig Create(string inputFileName,
public static RawConfig Create(
string inputFileName,
string @namespace,
string className,
string baseLocale = "en",
PluralAutoEntity pluralAutoEntity = PluralAutoEntity.Cardinal,
string rootPropertyName = "Root",
string pluralParameter = "n")
string pluralParameter = "n",
string startCharacter = "{",
string endCharacter = "}")
{
return new RawConfig(
Namespace: @namespace,
Expand All @@ -20,7 +23,9 @@ public static RawConfig Create(string inputFileName,
InputFileName: inputFileName,
PluralAutoEntity: pluralAutoEntity,
PluralParameter: pluralParameter,
RootPropertyName: rootPropertyName
RootPropertyName: rootPropertyName,
StartCharacter: startCharacter,
EndCharacter: endCharacter
);
}
}
14 changes: 10 additions & 4 deletions Slang.Net/Data/FilesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public record struct SlangFileCollection(List<TranslationFile> Files);
/// <param name="Locale">The inferred locale of this file (by file name, directory name, or config)</param>
public record struct TranslationFile(
Func<Task<string>> Read,
CultureInfo Locale
CultureInfo Locale,
string Type
);

public static class FilesRepository
Expand Down Expand Up @@ -54,10 +55,13 @@ public static SlangFileCollection GetFileCollection(
return GetTranslationFile(baseCulture, fileName, () => Task.FromResult(content));
}

internal static TranslationFile? GetTranslationFile(CultureInfo baseCulture, string fileName,
internal static TranslationFile? GetTranslationFile(
CultureInfo baseCulture,
string fileName,
Func<Task<string>> contentFactory)
{
string fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName).Split('.').First();
string fileNameExtension = Path.GetExtension(fileName).Split('.').Last();

var baseFileMatch = Regexes.BaseFileRegex.Match(fileNameNoExtension);

Expand All @@ -68,7 +72,8 @@ public static SlangFileCollection GetFileCollection(

return new TranslationFile(
Locale: baseCulture,
Read: contentFactory);
Read: contentFactory,
Type: fileNameExtension);
}

// secondary files (strings_x)
Expand All @@ -89,7 +94,8 @@ public static SlangFileCollection GetFileCollection(

return new TranslationFile(
Locale: locale,
Read: contentFactory);
Read: contentFactory,
Type: fileNameExtension);
}

return null;
Expand Down
Loading