jsakamoto / Toolbelt.Blazor.I18nText

The class library that provides the ability to localize texts on your Blazor app!
Mozilla Public License 2.0
246 stars 23 forks source link

FormatException: Format String can be only "G", "g", "X", "x", "F", "f", "D" or "d". #59

Closed ayfie-arkadiusz-mazur closed 1 year ago

ayfie-arkadiusz-mazur commented 1 year ago

I'm getting the following exception when trying to build my project using the latest version of the library (12.0.0):

AggregateException: One or more errors occurred.
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body)
   at Toolbelt.Blazor.I18nText.I18nTextSourceFile.Parse(IEnumerable`1 srcFiles, I18nTextCompilerOptions options, CancellationToken canceleationToken) in C:\Projects\My\Blazor\I18nText\Toolbelt.Blazor.I18nText.Compiler.Shared\I18nTextSourceFile.cs:line 58
   at Toolbelt.Blazor.I18nText.I18nTextCompiler.Compile(IEnumerable`1 srcFiles, I18nTextCompilerOptions options, Action`2 beforeCompile, Action`3 saveCode, CancellationToken cancellationToken) in C:\Projects\My\Blazor\I18nText\Toolbelt.Blazor.I18nText.Compiler.Shared\I18nTextCompiler.cs:line 43
   at Toolbelt.Blazor.I18nText.I18nTextCompiler.Compile(IEnumerable`1 srcFiles, I18nTextCompilerOptions options) in C:\Projects\My\Blazor\I18nText\Toolbelt.Blazor.I18nText.Compiler.Shared\I18nTextCompiler.cs:line 22
   at Toolbelt.Blazor.I18nText.CompileI18nText.Execute() in C:\Projects\My\Blazor\I18nText\Toolbelt.Blazor.I18nText.CompileTask\CompileI18nText.cs:line 67
FormatException: Format String can be only "G", "g", "X", "x", "F", "f", "D" or "d".
   at System.Enum.ToString(String format)
   at System.Text.StringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.Format(String format, Object arg0, Object arg1)
   at Toolbelt.Blazor.I18nText.CompileI18nText.<Execute>b__32_1(Exception ex) in C:\Projects\My\Blazor\I18nText\Toolbelt.Blazor.I18nText.CompileTask\CompileI18nText.cs:line 51
   at Toolbelt.Blazor.I18nText.I18nTextSourceFile.DeserializeSrcText(I18nTextSourceFile srcFile, I18nTextCompilerOptions options) in C:\Projects\My\Blazor\I18nText\Toolbelt.Blazor.I18nText.Compiler.Shared\I18nTextSourceFile.cs:line 111
   at Toolbelt.Blazor.I18nText.I18nTextSourceFile.<>c__DisplayClass9_0.<Parse>b__2(I18nTextSourceFile srcFile) in C:\Projects\My\Blazor\I18nText\Toolbelt.Blazor.I18nText.Compiler.Shared\I18nTextSourceFile.cs:line 60
   at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()
   at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
   at System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object <p0>)

I have an impression that problematic is the line no. 51 from the Toolbelt.Blazor.I18nText/Toolbelt.Blazor.I18nText.CompileTask/CompileI18nText.cs file, namely the $"IN{compilerEx.Code:D3}: {ex.Message}" part. Imho, casting enum to int would do the job, i.e. $"IN{(int)compilerEx.Code:D3}: {ex.Message}"

jsakamoto commented 1 year ago

@ayfie-arkadiusz-mazur Thank you for reporting! Could you share your project with me? Or it might be enough to attach your i18n localized text jon files.

ayfie-arkadiusz-mazur commented 1 year ago

@jsakamoto Attached. It is though enough to provide wrongly constructed json files to reproduce the issue (in my case the json files generator was not adding comma characters between consecutive "key": "value" pairs.

Could you please also expand the error description with the name of the problematic file? Currently developer is left with the message like:

error : IN002: After parsing a value an unexpected character was encountered: ". Path 'key1', line 3, position 2.

without any indication about the file that is broken (you may have several localized json files in the project sharing the same keys).

Localized.en.json.zip

jsakamoto commented 1 year ago

@ayfie-arkadiusz-mazur

I fixed those problems and published the new version today.

Please check it out.

Again, thank you for your contributions!

ayfie-arkadiusz-mazur commented 1 year ago

Wow! Errors handling enhanced much more than just to fix the issue I've reported. Thanks!

I'm wondering though if moving the code (here: IN002) out of the message and storing it in the errorCode field (instsead of the textual value of enum, here: SourceTextIsInvalidFormat) wouldn't be better :-)

Localization-error

jsakamoto commented 1 year ago

@ayfie-arkadiusz-mazur Thank you for your good suggestion! Yeah, you are right. I'll try to do that.

jsakamoto commented 1 year ago

@ayfie-arkadiusz-mazur

Could you check the v.12.0.2 out?

ayfie-arkadiusz-mazur commented 1 year ago

@jsakamoto Works like a charm. Thanks!