Skip to content

Commit 71079b1

Browse files
committed
Optimize null check logic
1 parent 1e8c92c commit 71079b1

File tree

12 files changed

+84
-58
lines changed

12 files changed

+84
-58
lines changed

.github/workflows/create-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Setup .NET
1717
uses: actions/setup-dotnet@v4
1818
with:
19-
dotnet-version: 8.0.x
19+
dotnet-version: 9.0.x
2020
- name: Install project dependencies
2121
working-directory: ./src
2222
run: dotnet restore

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Setup .NET
2222
uses: actions/setup-dotnet@v4
2323
with:
24-
dotnet-version: 8.0.x
24+
dotnet-version: 9.0.x
2525
- name: Restore dependencies
2626
working-directory: ./src
2727
run: dotnet restore

src/Nager.Country.Translation/AssemblieExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Nager.Country.Translation
77
{
88
internal static class AssemblieExtensions
99
{
10-
public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
10+
public static IEnumerable<Type?> GetLoadableTypes(this Assembly assembly)
1111
{
12-
if (assembly == null)
12+
if (assembly is null)
1313
{
1414
throw new ArgumentNullException(nameof(assembly));
1515
}
@@ -20,7 +20,7 @@ public static IEnumerable<Type> GetLoadableTypes(this Assembly assembly)
2020
}
2121
catch (ReflectionTypeLoadException e)
2222
{
23-
return e.Types.Where(t => t != null);
23+
return e.Types.Where(t => t is not null);
2424
}
2525
}
2626
}

src/Nager.Country.Translation/CountryProviderExtension.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class CountryProviderExtension
1111
private static readonly Lazy<TranslationProvider> TranslationProvider = new Lazy<TranslationProvider>();
1212

1313
/// <inheritdoc/>
14-
public static ICountryInfo GetCountryByNameConsiderTranslation(
14+
public static ICountryInfo? GetCountryByNameConsiderTranslation(
1515
this ICountryProvider countryProvider,
1616
string countryName)
1717
{
@@ -30,6 +30,11 @@ public static ICountryInfo GetCountryByNameConsiderTranslation(
3030
}
3131

3232
var countryTanslation = TranslationProvider.Value.GetCountryTranslation(country.Alpha2Code);
33+
if (countryTanslation is null)
34+
{
35+
continue;
36+
}
37+
3338
if (countryTanslation.Translations.Any(translation => translation.Name.Equals(countryName, StringComparison.OrdinalIgnoreCase)))
3439
{
3540
return country;

src/Nager.Country.Translation/ITranslationProvider.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,93 +19,93 @@ public interface ITranslationProvider
1919
/// </summary>
2020
/// <param name="languageCode">The ISO language code (e.g., "en").</param>
2121
/// <returns>The corresponding language translation.</returns>
22-
ILanguageTranslation GetLanguage(string languageCode);
22+
ILanguageTranslation? GetLanguage(string languageCode);
2323

2424
/// <summary>
2525
/// Gets the language translation for the specified <see cref="LanguageCode"/>.
2626
/// </summary>
2727
/// <param name="languageCode">The language code enumeration value.</param>
2828
/// <returns>The corresponding language translation.</returns>
29-
ILanguageTranslation GetLanguage(LanguageCode languageCode);
29+
ILanguageTranslation? GetLanguage(LanguageCode languageCode);
3030

3131
/// <summary>
3232
/// Gets the country translation for the specified ISO Alpha-2 country code.
3333
/// </summary>
3434
/// <param name="alpha2Code">The ISO Alpha-2 country code.</param>
3535
/// <returns>The corresponding country translation.</returns>
36-
ICountryTranslation GetCountryTranslation(Alpha2Code alpha2Code);
36+
ICountryTranslation? GetCountryTranslation(Alpha2Code alpha2Code);
3737

3838
/// <summary>
3939
/// Gets the localized country name for the specified country and language code.
4040
/// </summary>
4141
/// <param name="alpha2or3Code">The ISO Alpha-2 or Alpha-3 country code.</param>
4242
/// <param name="languageCode">The target language code.</param>
4343
/// <returns>The localized country name.</returns>
44-
string GetCountryTranslatedName(string alpha2or3Code, LanguageCode languageCode);
44+
string? GetCountryTranslatedName(string alpha2or3Code, LanguageCode languageCode);
4545

4646
/// <summary>
4747
/// Gets the localized country name for the specified Alpha-2 code and language code.
4848
/// </summary>
4949
/// <param name="alpha2Code">The ISO Alpha-2 country code.</param>
5050
/// <param name="languageCode">The target language code.</param>
5151
/// <returns>The localized country name.</returns>
52-
string GetCountryTranslatedName(Alpha2Code alpha2Code, LanguageCode languageCode);
52+
string? GetCountryTranslatedName(Alpha2Code alpha2Code, LanguageCode languageCode);
5353

5454
/// <summary>
5555
/// Gets the localized country name for the specified Alpha-3 code and language code.
5656
/// </summary>
5757
/// <param name="alpha3Code">The ISO Alpha-3 country code.</param>
5858
/// <param name="languageCode">The target language code.</param>
5959
/// <returns>The localized country name.</returns>
60-
string GetCountryTranslatedName(Alpha3Code alpha3Code, LanguageCode languageCode);
60+
string? GetCountryTranslatedName(Alpha3Code alpha3Code, LanguageCode languageCode);
6161

6262
/// <summary>
6363
/// Gets the localized country name for the specified country and language code as strings.
6464
/// </summary>
6565
/// <param name="alpha2or3Code">The ISO Alpha-2 or Alpha-3 country code.</param>
6666
/// <param name="languageCode">The target ISO language code as a string.</param>
6767
/// <returns>The localized country name.</returns>
68-
string GetCountryTranslatedName(string alpha2or3Code, string languageCode);
68+
string? GetCountryTranslatedName(string alpha2or3Code, string languageCode);
6969

7070
/// <summary>
7171
/// Gets the localized country name for the specified Alpha-2 code and language code as a string.
7272
/// </summary>
7373
/// <param name="alpha2Code">The ISO Alpha-2 country code.</param>
7474
/// <param name="languageCode">The target ISO language code as a string.</param>
7575
/// <returns>The localized country name.</returns>
76-
string GetCountryTranslatedName(Alpha2Code alpha2Code, string languageCode);
76+
string? GetCountryTranslatedName(Alpha2Code alpha2Code, string languageCode);
7777

7878
/// <summary>
7979
/// Gets the localized country name for the specified Alpha-3 code and language code as a string.
8080
/// </summary>
8181
/// <param name="alpha3Code">The ISO Alpha-3 country code.</param>
8282
/// <param name="languageCode">The target ISO language code as a string.</param>
8383
/// <returns>The localized country name.</returns>
84-
string GetCountryTranslatedName(Alpha3Code alpha3Code, string languageCode);
84+
string? GetCountryTranslatedName(Alpha3Code alpha3Code, string languageCode);
8585

8686
/// <summary>
8787
/// Gets the localized country name using culture information.
8888
/// </summary>
8989
/// <param name="alpha2or3Code">The ISO Alpha-2 or Alpha-3 country code.</param>
9090
/// <param name="culture">The target culture (e.g., CultureInfo("de-DE")).</param>
9191
/// <returns>The localized country name.</returns>
92-
string GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture);
92+
string? GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture);
9393

9494
/// <summary>
9595
/// Gets the localized country name using culture information.
9696
/// </summary>
9797
/// <param name="alpha2Code">The ISO Alpha-2 country code.</param>
9898
/// <param name="culture">The target culture.</param>
9999
/// <returns>The localized country name.</returns>
100-
string GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture);
100+
string? GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture);
101101

102102
/// <summary>
103103
/// Gets the localized country name using culture information.
104104
/// </summary>
105105
/// <param name="alpha3Code">The ISO Alpha-3 country code.</param>
106106
/// <param name="culture">The target culture.</param>
107107
/// <returns>The localized country name.</returns>
108-
string GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture);
108+
string? GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture);
109109

110110
/// <summary>
111111
/// Gets the localized country name using culture information, falling back to a default language if necessary.
@@ -114,7 +114,7 @@ public interface ITranslationProvider
114114
/// <param name="culture">The target culture.</param>
115115
/// <param name="defaultLanguageCode">The fallback language code if no translation is available for the culture.</param>
116116
/// <returns>The localized country name.</returns>
117-
string GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture, LanguageCode defaultLanguageCode);
117+
string? GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture, LanguageCode defaultLanguageCode);
118118

119119
/// <summary>
120120
/// Gets the localized country name using culture information, falling back to a default language if necessary.
@@ -123,7 +123,7 @@ public interface ITranslationProvider
123123
/// <param name="culture">The target culture.</param>
124124
/// <param name="defaultLanguageCode">The fallback language code if no translation is available for the culture.</param>
125125
/// <returns>The localized country name.</returns>
126-
string GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture, LanguageCode defaultLanguageCode);
126+
string? GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture, LanguageCode defaultLanguageCode);
127127

128128
/// <summary>
129129
/// Gets the localized country name using culture information, falling back to a default language if necessary.
@@ -132,6 +132,6 @@ public interface ITranslationProvider
132132
/// <param name="culture">The target culture.</param>
133133
/// <param name="defaultLanguageCode">The fallback language code if no translation is available for the culture.</param>
134134
/// <returns>The localized country name.</returns>
135-
string GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture, LanguageCode defaultLanguageCode);
135+
string? GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture, LanguageCode defaultLanguageCode);
136136
}
137137
}

src/Nager.Country.Translation/Nager.Country.Translation.csproj

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net48;netstandard2.0;netstandard2.1;net6;net8</TargetFrameworks>
54
<Authors>Tino Hager</Authors>
65
<Company>nager.at</Company>
76
<Description>Get country translations and language translations for Nager.Country</Description>
8-
<PackageProjectUrl>https://github.com/nager/Nager.Country</PackageProjectUrl>
9-
<RepositoryUrl>https://github.com/nager/Nager.Country</RepositoryUrl>
10-
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11-
<PackageReleaseNotes></PackageReleaseNotes>
12-
<PackageTags>Country CountryInfo ISO3166 ISO-3166-1_Alpha2 ISO-3166-1_Alpha3 ISO639 ISO-639-1 globalization localization</PackageTags>
7+
138
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
149
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10+
11+
<LangVersion>12.0</LangVersion>
12+
13+
<Nullable>enable</Nullable>
14+
1515
<PackageIcon>icon.png</PackageIcon>
16+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
17+
<PackageReleaseNotes></PackageReleaseNotes>
18+
<PackageProjectUrl>https://github.com/nager/Nager.Country</PackageProjectUrl>
19+
<PackageTags>Country CountryInfo ISO3166 ISO-3166-1_Alpha2 ISO-3166-1_Alpha3 ISO639 ISO-639-1 globalization localization</PackageTags>
20+
21+
<RepositoryUrl>https://github.com/nager/Nager.Country</RepositoryUrl>
22+
23+
<TargetFrameworks>net48;netstandard2.0;netstandard2.1;net6;net8;net9</TargetFrameworks>
24+
1625
<Version>4.0.0</Version>
1726
</PropertyGroup>
1827

src/Nager.Country.Translation/TranslationProvider.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public TranslationProvider()
3030

3131
foreach (var type in types)
3232
{
33-
var languageTranslation = (ILanguageTranslation)Activator.CreateInstance(type);
33+
var languageTranslation = (ILanguageTranslation?)Activator.CreateInstance(type);
34+
if (languageTranslation is null)
35+
{
36+
continue;
37+
}
38+
3439
this._languageCode2LanguageTranslation.Add(languageTranslation.LanguageCode, languageTranslation);
3540
}
3641

@@ -296,7 +301,7 @@ public IEnumerable<ILanguageTranslation> GetLanguages()
296301
}
297302

298303
/// <inheritdoc/>
299-
public ILanguageTranslation GetLanguage(string languageCode)
304+
public ILanguageTranslation? GetLanguage(string languageCode)
300305
{
301306
if (Enum.TryParse(languageCode, true, out LanguageCode languageCodeParsed))
302307
{
@@ -307,9 +312,9 @@ public ILanguageTranslation GetLanguage(string languageCode)
307312
}
308313

309314
/// <inheritdoc/>
310-
public ILanguageTranslation GetLanguage(LanguageCode languageCode)
315+
public ILanguageTranslation? GetLanguage(LanguageCode languageCode)
311316
{
312-
if (this._languageCode2LanguageTranslation.TryGetValue(languageCode, out ILanguageTranslation languageInfo))
317+
if (this._languageCode2LanguageTranslation.TryGetValue(languageCode, out ILanguageTranslation? languageInfo))
313318
{
314319
return languageInfo;
315320
}
@@ -318,9 +323,9 @@ public ILanguageTranslation GetLanguage(LanguageCode languageCode)
318323
}
319324

320325
/// <inheritdoc/>
321-
public ICountryTranslation GetCountryTranslation(Alpha2Code alpha2Code)
326+
public ICountryTranslation? GetCountryTranslation(Alpha2Code alpha2Code)
322327
{
323-
if (this._alpha2Code2CountryTranslation.TryGetValue(alpha2Code, out ICountryTranslation countryTranslation))
328+
if (this._alpha2Code2CountryTranslation.TryGetValue(alpha2Code, out ICountryTranslation? countryTranslation))
324329
{
325330
return countryTranslation;
326331
}
@@ -329,14 +334,14 @@ public ICountryTranslation GetCountryTranslation(Alpha2Code alpha2Code)
329334
}
330335

331336
/// <inheritdoc/>
332-
public string GetCountryTranslatedName(Alpha2Code alpha2Code, LanguageCode languageCode)
337+
public string? GetCountryTranslatedName(Alpha2Code alpha2Code, LanguageCode languageCode)
333338
{
334339
var countryInfo = this._countryProvider.GetCountry(alpha2Code);
335340
return this.GetCountryTranslatedName(countryInfo, languageCode);
336341
}
337342

338343
/// <inheritdoc/>
339-
public string GetCountryTranslatedName(Alpha2Code alpha2Code, string languageCode)
344+
public string? GetCountryTranslatedName(Alpha2Code alpha2Code, string languageCode)
340345
{
341346
if (Enum.TryParse(languageCode, true, out LanguageCode code))
342347
{
@@ -347,14 +352,14 @@ public string GetCountryTranslatedName(Alpha2Code alpha2Code, string languageCod
347352
}
348353

349354
/// <inheritdoc/>
350-
public string GetCountryTranslatedName(Alpha3Code alpha3Code, LanguageCode languageCode)
355+
public string? GetCountryTranslatedName(Alpha3Code alpha3Code, LanguageCode languageCode)
351356
{
352357
var countryInfo = this._countryProvider.GetCountry(alpha3Code);
353358
return this.GetCountryTranslatedName(countryInfo, languageCode);
354359
}
355360

356361
/// <inheritdoc/>
357-
public string GetCountryTranslatedName(Alpha3Code alpha3Code, string languageCode)
362+
public string? GetCountryTranslatedName(Alpha3Code alpha3Code, string languageCode)
358363
{
359364
if (Enum.TryParse(languageCode, true, out LanguageCode code))
360365
{
@@ -365,14 +370,14 @@ public string GetCountryTranslatedName(Alpha3Code alpha3Code, string languageCod
365370
}
366371

367372
/// <inheritdoc/>
368-
public string GetCountryTranslatedName(string alpha2or3Code, LanguageCode languageCode)
373+
public string? GetCountryTranslatedName(string alpha2or3Code, LanguageCode languageCode)
369374
{
370375
var countryInfo = this._countryProvider.GetCountry(alpha2or3Code);
371376
return this.GetCountryTranslatedName(countryInfo, languageCode);
372377
}
373378

374379
/// <inheritdoc/>
375-
public string GetCountryTranslatedName(string alpha2or3Code, string languageCode)
380+
public string? GetCountryTranslatedName(string alpha2or3Code, string languageCode)
376381
{
377382
if (Enum.TryParse(languageCode, true, out LanguageCode code))
378383
{
@@ -382,11 +387,14 @@ public string GetCountryTranslatedName(string alpha2or3Code, string languageCode
382387
return null;
383388
}
384389

385-
private string GetCountryTranslatedName(ICountryInfo countryInfo, LanguageCode languageCode)
390+
private string? GetCountryTranslatedName(ICountryInfo countryInfo, LanguageCode languageCode)
386391
{
387-
this._alpha2Code2CountryTranslation.TryGetValue(countryInfo.Alpha2Code, out var countryTranslation);
392+
if (!this._alpha2Code2CountryTranslation.TryGetValue(countryInfo.Alpha2Code, out var countryTranslation))
393+
{
394+
return null;
395+
}
388396

389-
if (countryTranslation.Translations != null && countryTranslation.Translations.Length > 0)
397+
if (countryTranslation.Translations is not null && countryTranslation.Translations.Length > 0)
390398
{
391399
return countryTranslation.Translations.Where(x => x.LanguageCode == languageCode).Select(x => x.Name).FirstOrDefault();
392400
}
@@ -395,25 +403,25 @@ private string GetCountryTranslatedName(ICountryInfo countryInfo, LanguageCode l
395403
}
396404

397405
/// <inheritdoc/>
398-
public string GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture)
406+
public string? GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture)
399407
{
400408
return this.GetCountryTranslatedName(alpha2or3Code, culture.TwoLetterISOLanguageName);
401409
}
402410

403411
/// <inheritdoc/>
404-
public string GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture)
412+
public string? GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture)
405413
{
406414
return this.GetCountryTranslatedName(alpha2Code, culture.TwoLetterISOLanguageName);
407415
}
408416

409417
/// <inheritdoc/>
410-
public string GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture)
418+
public string? GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture)
411419
{
412420
return this.GetCountryTranslatedName(alpha3Code, culture.TwoLetterISOLanguageName);
413421
}
414422

415423
/// <inheritdoc/>
416-
public string GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture, LanguageCode defaultLanguageCode)
424+
public string? GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture, LanguageCode defaultLanguageCode)
417425
{
418426
var name = this.GetCountryTranslatedName(alpha2or3Code, culture);
419427
if (string.IsNullOrWhiteSpace(name))
@@ -424,7 +432,7 @@ public string GetCountryTranslatedName(string alpha2or3Code, CultureInfo culture
424432
}
425433

426434
/// <inheritdoc/>
427-
public string GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture, LanguageCode defaultLanguageCode)
435+
public string? GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo culture, LanguageCode defaultLanguageCode)
428436
{
429437
var name = this.GetCountryTranslatedName(alpha2Code, culture);
430438
if (string.IsNullOrWhiteSpace(name))
@@ -435,7 +443,7 @@ public string GetCountryTranslatedName(Alpha2Code alpha2Code, CultureInfo cultur
435443
}
436444

437445
/// <inheritdoc/>
438-
public string GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture, LanguageCode defaultLanguageCode)
446+
public string? GetCountryTranslatedName(Alpha3Code alpha3Code, CultureInfo culture, LanguageCode defaultLanguageCode)
439447
{
440448
var name = this.GetCountryTranslatedName(alpha3Code, culture);
441449
if (string.IsNullOrWhiteSpace(name))

0 commit comments

Comments
 (0)