Skip to content

Commit

Permalink
GlobalStrings v0.3.3
Browse files Browse the repository at this point in the history
- SetText with formater overload created.
- UpdateMode enum renamed.
  • Loading branch information
LuanRoger committed May 6, 2022
1 parent 3d83326 commit ee543f5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
10 changes: 8 additions & 2 deletions GlobalStrings/EventArguments/UpdateModeEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ namespace GlobalStrings.EventArguments
{
public sealed class UpdateModeEventArgs : EventArgs
{
public UpdateMode mode { get; internal init; }
public dynamic lang {get; internal init;}
public UpdateMode mode { get; }
public dynamic lang { get; }

public UpdateModeEventArgs(UpdateMode mode, dynamic lang)
{
this.mode = mode;
this.lang = lang;
}
}
}
2 changes: 1 addition & 1 deletion GlobalStrings/GlobalStrings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<PackageId>GlobalStrings</PackageId>
<Version>0.2.2</Version>
<PackageVersion>0.3.2</PackageVersion>
<PackageVersion>0.3.3</PackageVersion>
<Title>Global Strings</Title>
<License>MIT</License>
<Authors>Luan Roger</Authors>
Expand Down
56 changes: 53 additions & 3 deletions GlobalStrings/Globalization/Globalization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using GlobalStrings.Types;
using GlobalStrings.Types.Enums;

// ReSharper disable CoVariantArrayConversion

namespace GlobalStrings.Globalization
{
/// <summary>
Expand Down Expand Up @@ -68,6 +70,30 @@ public string SetText(GCollectionCode collectionCode, KTextCode key)
languagesInfo!.First(c => Equals(c.langCode, langCodeNow))
.textBookCollection[collectionCode][key];
}

/// <summary>
/// Set the text according to the current language. Must be used in <c>LangTextObserver</c> event.
/// </summary>
/// <param name="collectionCode">Address where the string is in a collection.</param>
/// <param name="key">String allocation key in a collection.</param>
/// <param name="formatArgs">An string array that contains the text to replace in correspondent place.</param>
/// <returns>A string according to the current language.</returns>
/// <example><code>string text = globalization.SetText(0)</code></example>
/// <exception cref="StopedGlobalizationExeption"></exception>
public string SetText(GCollectionCode collectionCode, KTextCode key, params string[] formatArgs)
{
if(!hasStarted)
throw new StopedGlobalizationExeption();
if(stringsByFile && actualLanguageInfo is null)
throw new NullValueForDependency(nameof(stringsByFile), nameof(actualLanguageInfo));
if(!stringsByFile && languagesInfo is null)
throw new NullValueForDependency(nameof(stringsByFile), nameof(languagesInfo));

var matchLanguageInfo = stringsByFile ? actualLanguageInfo :
languagesInfo!.First(c => Equals(c.langCode, langCodeNow));

return string.Format(matchLanguageInfo!.textBookCollection[collectionCode][key], formatArgs);
}
/// <summary>
/// Set the text according to the current language. Must be used in <c>LangTextObserver</c> event.
/// </summary>
Expand All @@ -90,6 +116,30 @@ public string SetText(KeyValuePair<GCollectionCode, KTextCode> textCode)
languagesInfo!.First(c => Equals(c.langCode, langCodeNow))
.textBookCollection[collectionCode][key];
}
/// <summary>
/// Set the text according to the current language. Must be used in <c>LangTextObserver</c> event.
/// </summary>
/// <param name="textCode">Key pair than identify the text. Where the <c>Key</c> is the <c>GCollectionCode</c> and
/// <c>Value</c> is <c>KTextCode</c>.</param>
/// <param name="formatArgs">An string array that contains the text to replace in correspondent place.</param>
/// <returns>A string according to the current language.</returns>
/// <example><code>string text = globalization.SetText(0)</code></example>
/// <exception cref="StopedGlobalizationExeption"></exception>
public string SetText(KeyValuePair<GCollectionCode, KTextCode> textCode, params string[] formatArgs)
{
if(!hasStarted)
throw new StopedGlobalizationExeption();
if(stringsByFile && actualLanguageInfo is null)
throw new NullValueForDependency(nameof(stringsByFile), nameof(actualLanguageInfo));
if(!stringsByFile && languagesInfo is null)
throw new NullValueForDependency(nameof(stringsByFile), nameof(languagesInfo));

var matchLanguageInfo = stringsByFile ? actualLanguageInfo :
languagesInfo!.First(c => Equals(c.langCode, langCodeNow));

(GCollectionCode collectionCode, KTextCode key) = textCode;
return string.Format(matchLanguageInfo!.textBookCollection[collectionCode][key], formatArgs);
}

/// <summary>
/// Switch from one language to another and update all strings in <c>LangTextObserver</c> event.
Expand All @@ -109,7 +159,7 @@ public void UpdateLang(TLangCode newLangCode)
if(stringsByFile)
this.LoadLanguageInfos(filepath!);

LangTextObserverCall(this, new(){ mode = UpdateMode.Update, lang = newLangCode! });
LangTextObserverCall(this, new(UpdateMode.Update, newLangCode!));
}

/// <summary>
Expand All @@ -118,7 +168,7 @@ public void UpdateLang(TLangCode newLangCode)
public void StartGlobalization()
{
hasStarted = true;
LangTextObserverCall(this, new(){ mode = UpdateMode.Insert, lang = langCodeNow });
LangTextObserverCall(this, new(UpdateMode.Start, langCodeNow));
}

/// <summary>
Expand All @@ -129,7 +179,7 @@ public void SyncStrings()
if(!hasStarted)
throw new StopedGlobalizationExeption();

LangTextObserverCall(this, new(){ mode = UpdateMode.Sync, lang = langCodeNow });
LangTextObserverCall(this, new(UpdateMode.Sync, langCodeNow));
}
}
}
2 changes: 1 addition & 1 deletion GlobalStrings/Globalization/GlobalizationFromFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal void LoadStringsFromFileJson(string filepath)
/// <summary>
/// Get only the strings than the language will use, and dispose <c>languagesInfo</c>
/// </summary>
/// <exception cref="IsNotStringByFileException"></exception>
/// <exception cref="IsNotStringByFileException">There is possible to use this without a json file</exception>
private void OptimizeLangListByLangCode()
{
if(!stringsByFile)
Expand Down
2 changes: 1 addition & 1 deletion GlobalStrings/Types/Enums/UpdateModeEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GlobalStrings.Types.Enums
{
public enum UpdateMode { Update, Insert, Sync }
public enum UpdateMode { Update, Start, Sync }
}

0 comments on commit ee543f5

Please sign in to comment.