Skip to content

Commit

Permalink
add lightweight cultureinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Jan 26, 2024
1 parent 604bda5 commit 1a1b12f
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 68 deletions.
6 changes: 0 additions & 6 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
"version": 1,
"isRoot": true,
"tools": {
"dotnet-format": {
"version": "5.1.250801",
"commands": [
"dotnet-format"
]
},
"dotnet-reportgenerator-globaltool": {
"version": "5.1.9",
"commands": [
Expand Down
17 changes: 10 additions & 7 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<Project Condition="'$(MSBuildProjectExtension)' == '.csproj' and '$(MSBuildProjectFile)' != '_build.csproj'">

<Project>
<PropertyGroup>
<PublishRelease>true</PublishRelease>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.csproj'">
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8603;CS8604;CS8625;CS8618;CS8620;CS0219</WarningsAsErrors>
<ImplicitUsings>enable</ImplicitUsings>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<WarningsAsErrors>CS8600;CS8602;CS8603;CS8604;CS8625;CS8618;CS8620</WarningsAsErrors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591;CS1573</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Roslynator.CodeAnalysis.Analyzers" Version="4.1.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.43.0.51858">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
185 changes: 185 additions & 0 deletions src/BrazilCulture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
using System;
using System.Globalization;

namespace BrazilModels;

/// <summary>
/// Lightweight brazilian culture helpers
/// </summary>
public static class BrazilCulture
{
/// <summary>
/// NumberFormatInfo using ',' for decimal separators and '.' for group separators
/// </summary>
/// {}
public static readonly NumberFormatInfo NumberFormat =
new()
{
CurrencyDecimalDigits = 2,
CurrencyDecimalSeparator = ",",
CurrencyGroupSeparator = ".",
CurrencyNegativePattern = 9,
CurrencyPositivePattern = 2,
CurrencySymbol = "R$",
NaNSymbol = "NaN",
NegativeInfinitySymbol = "-\u221E",
NegativeSign = "-",
NumberDecimalDigits = 3,
NumberDecimalSeparator = ",",
NumberGroupSeparator = ".",
NumberNegativePattern = 1,
PerMilleSymbol = "\u2030",
PercentDecimalDigits = 3,
PercentDecimalSeparator = ",",
PercentGroupSeparator = ".",
PercentNegativePattern = 1,
PercentPositivePattern = 1,
PercentSymbol = "%",
PositiveInfinitySymbol = "\u221E",
PositiveSign = "\u002B",
NativeDigits = new[]
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
},
DigitSubstitution = DigitShapes.None,
CurrencyGroupSizes = new[]
{
3,
},
NumberGroupSizes = new[]
{
3,
},
PercentGroupSizes = new[]
{
3,
},
};

/// <summary>
/// Format Dates in pt-BR
/// </summary>
public static readonly DateTimeFormatInfo DateTimeFormat =
new()
{
AMDesignator = "AM",
PMDesignator = "PM",
DateSeparator = "/",
TimeSeparator = ":",
Calendar = new GregorianCalendar(),
CalendarWeekRule = CalendarWeekRule.FirstDay,
FirstDayOfWeek = DayOfWeek.Sunday,
FullDateTimePattern = "dddd, d 'de' MMMM 'de' yyyy HH:mm:ss",
MonthDayPattern = "d 'de' MMMM",
YearMonthPattern = "MMMM 'de' yyyy",
LongDatePattern = "dddd, d 'de' MMMM 'de' yyyy",
LongTimePattern = "HH:mm:ss",
ShortDatePattern = "dd/MM/yyyy",
ShortTimePattern = "HH:mm",
AbbreviatedDayNames = new[]
{
"dom.", "seg.", "ter.", "qua.", "qui.", "sex.", "sáb.",
},
AbbreviatedMonthGenitiveNames = new[]
{
"jan.",
"fev.",
"mar.",
"abr.",
"mai.",
"jun.",
"jul.",
"ago.",
"set.",
"out.",
"nov.",
"dez.",
"",
},
AbbreviatedMonthNames = new[]
{
"jan.",
"fev.",
"mar.",
"abr.",
"mai.",
"jun.",
"jul.",
"ago.",
"set.",
"out.",
"nov.",
"dez.",
"",
},
DayNames = new[]
{
"domingo",
"segunda-feira",
"terça-feira",
"quarta-feira",
"quinta-feira",
"sexta-feira",
"sábado",
},
MonthGenitiveNames = new[]
{
"janeiro",
"fevereiro",
"março",
"abril",
"maio",
"junho",
"julho",
"agosto",
"setembro",
"outubro",
"novembro",
"dezembro",
"",
},
MonthNames = new[]
{
"janeiro",
"fevereiro",
"março",
"abril",
"maio",
"junho",
"julho",
"agosto",
"setembro",
"outubro",
"novembro",
"dezembro",
"",
},
ShortestDayNames = new[]
{
"D", "S", "T", "Q", "Q", "S", "S",
},
};

/// <summary>
/// Lightweight brazil culture info
/// </summary>
public static readonly CultureInfo CultureInfo = new BrazilCultureInfo();

sealed class BrazilCultureInfo : CultureInfo
{
public BrazilCultureInfo() : base(string.Empty)
{
this.NumberFormat = BrazilCulture.NumberFormat;
this.DateTimeFormat = BrazilCulture.DateTimeFormat;
}

public override string Name { get; } = "pt-BR";
public override string EnglishName { get; } = "Portuguese";
public override string DisplayName { get; } = "Portuguese (Brazil)";
public override string NativeName { get; } = "português (Brasil)";
public override string TwoLetterISOLanguageName { get; } = "pt";
public override string ThreeLetterISOLanguageName { get; } = "por";
public override string ThreeLetterWindowsLanguageName { get; } = "PTB";
public override CultureInfo Parent { get; } = InvariantCulture;
}
}
7 changes: 4 additions & 3 deletions src/BrazilModels.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
<TargetFramework>net6.0</TargetFramework>
<PackageId>BrazilModels</PackageId>
<Authors>Lucas Teles - [email protected]</Authors>
<Company />
<Company/>
<Description>CPF/CNPJ models and validators</Description>
<RepositoryType>GitHub</RepositoryType>
<PackageProjectUrl>https://github.com/lucasteles/BrazilModels</PackageProjectUrl>
<RepositoryUrl>https://github.com/lucasteles/BrazilModels</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<InvariantGlobalization>true</InvariantGlobalization>

<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
Expand All @@ -24,7 +25,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\README.md" Pack="true" PackagePath="\"/>
</ItemGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand All @@ -38,7 +39,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
</ItemGroup>

</Project>
56 changes: 5 additions & 51 deletions src/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,26 @@ public static class BrazilExtensions
/// <param name="value"></param>
/// <returns></returns>
public static decimal? TryParseDecimalBrazil(this string value) =>
decimal.TryParse(value, NumberStyles.Number, NumberFormat, out var dec) ? dec : null;
decimal.TryParse(value, NumberStyles.Number, BrazilCulture.NumberFormat, out var dec)
? dec
: null;

/// <summary>
/// Return brazilian number formatted text
/// Use ',' for decimal separators and '.' for group separators
/// </summary>
public static string ToBrazilString(this decimal value, string? format = null) =>
value.ToString(format, NumberFormat);
value.ToString(format, BrazilCulture.NumberFormat);

/// <summary>
/// Return brazilian number as money formatted text
/// Use ',' for decimal separators and '.' for group separators
/// </summary>
public static string ToBrazilMoneyString(this decimal value, bool moneySuffix = true)
{
var result = value.ToString("C", NumberFormat);
var result = value.ToString("C", BrazilCulture.NumberFormat);
return moneySuffix ? result : result.Replace("R$ ", "");
}

/// <summary>
/// NumberFormatInfo using ',' for decimal separators and '.' for group separators
/// </summary>
/// {}
public static readonly NumberFormatInfo NumberFormat =
new()
{
CurrencyDecimalDigits = 2,
CurrencyDecimalSeparator = ",",
CurrencyGroupSeparator = ".",
CurrencyNegativePattern = 9,
CurrencyPositivePattern = 2,
CurrencySymbol = "R$",
NaNSymbol = "NaN",
NegativeInfinitySymbol = "-\u221E",
NegativeSign = "-",
NumberDecimalDigits = 3,
NumberDecimalSeparator = ",",
NumberGroupSeparator = ".",
NumberNegativePattern = 1,
PerMilleSymbol = "\u2030",
PercentDecimalDigits = 3,
PercentDecimalSeparator = ",",
PercentGroupSeparator = ".",
PercentNegativePattern = 1,
PercentPositivePattern = 1,
PercentSymbol = "%",
PositiveInfinitySymbol = "\u221E",
PositiveSign = "\u002B",
NativeDigits = new[]
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
},
DigitSubstitution = DigitShapes.None,
CurrencyGroupSizes = new[]
{
3
},
NumberGroupSizes = new[]
{
3
},
PercentGroupSizes = new[]
{
3
},
};
}

static class Extensions
Expand Down
1 change: 0 additions & 1 deletion tests/BrazilModels.Tests/GlobalFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using BrazilModels.Tests.Utils;
using FsCheck;

namespace BrazilModels.Tests;
Expand Down

0 comments on commit 1a1b12f

Please sign in to comment.