Skip to content

Commit

Permalink
Merge pull request #18 from LazZiya/vNext
Browse files Browse the repository at this point in the history
V next
  • Loading branch information
LazZiya authored Jan 6, 2021
2 parents 278f8ad + e4135ce commit ee0e600
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
12 changes: 5 additions & 7 deletions XLocalizer/XLocalizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
<PackageTags>asp.net,core,razor,mvc,localization,globalization,client side,validation,translation,autokeyadding</PackageTags>
<PackageReleaseNotes>
Improvements
- Create localization folder if not exists (XmlResourceProvider)
Fixes:
- Fixed #9 - Failed to find out ResourcesPath when hosted in linux
- Fixed #13 - Similar keys that are stored in different resources caching the same value
- Performance imrovements
- Bypass localizing if current culture is same as TranslateFromCulture
See all docs: https://docs.ziyad.info/en/XLocalizer/v1.0/index.md

Release notes: https://github.com/LazZiya/XLocalizer/releases
</PackageReleaseNotes>
<VersionPrefix>1.0.1</VersionPrefix>
<VersionPrefix>1.0.2</VersionPrefix>
<VersionSuffix></VersionSuffix>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
<AssemblyVersion>1.0.2.0</AssemblyVersion>
<FileVersion>1.0.2.0</FileVersion>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageIconUrl>https://github.com/LazZiya/XLocalizer/raw/master/XLocalizer/files/icon.png</PackageIconUrl>
Expand Down
62 changes: 31 additions & 31 deletions XLocalizer/XStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,50 +87,50 @@ public IStringLocalizer WithCulture(CultureInfo culture)

private LocalizedString GetLocalizedString(string name, params object[] arguments)
{
var availableInTranslate = false;
// Option 0: If current culture is same as translation culture just return the key back
if (_transCulture.Equals(CultureInfo.CurrentCulture.Name, StringComparison.OrdinalIgnoreCase))
{
return new LocalizedString(name, string.Format(name, arguments), true, string.Empty);
}

// Option 1: Look in the cache
bool availableInCache = _cache.TryGetValue<TResource>(name, out string value);

if (!availableInCache)
if (availableInCache)
{
var culture = CultureInfo.CurrentCulture.Name;

// Option 2: Look in resource
bool availableInSource = _provider.TryGetValue<TResource>(name, out value);

if (!availableInSource && _options.AutoTranslate)
{
return new LocalizedString(name, string.Format(value, arguments), false, string.Empty);
}

if (_transCulture != culture)
// Option 3: Online translate
availableInTranslate = _translator.TryTranslate(_transCulture, culture, name, out value);
}
// Option 2: Look in source file
bool availableInSource = _provider.TryGetValue<TResource>(name, out value);
if (availableInSource)
{
// Add to cache
_cache.Set<TResource>(name, value);

// add a resource if it is not available in source and auto add is enabled
if (!availableInSource && _options.AutoAddKeys && _transCulture != culture)
{
// Add a resource only if auto translate is off or if available in translation
if (!_options.AutoTranslate || availableInTranslate)
{
bool savedToResource = _provider.TrySetValue<TResource>(name, value ?? name, "Created by XLocalizer");
_logger.LogInformation($"Save resource to file, status: '{savedToResource}', key: '{name}', value: '{value ?? name}'");
}
}
return new LocalizedString(name, string.Format(value, arguments), false, string.Empty);
}

if (availableInSource || availableInTranslate)
// Option 3: Try online translation service
var availableInTranslate = false;
if (_options.AutoTranslate)
{
availableInTranslate = _translator.TryTranslate(_transCulture, CultureInfo.CurrentCulture.Name, name, out value);
if (availableInTranslate)
{
// Save to cache
// Add to cache
_cache.Set<TResource>(name, value);

// Set availability to true
availableInCache = true;
}
}

var val = string.Format(value ?? name, arguments);
// Save to source file when AutoAdd is anebled and
// translation success or AutoTranslate is off
if (_options.AutoAddKeys && (availableInTranslate || !_options.AutoTranslate))
{
bool savedToResource = _provider.TrySetValue<TResource>(name, value ?? name, "Created by XLocalizer");
_logger.LogInformation($"Save resource to file, status: '{savedToResource}', key: '{name}', value: '{value ?? name}'");
}

return new LocalizedString(name, val, resourceNotFound: !availableInCache, searchedLocation: typeof(TResource).FullName);
return new LocalizedString(name, string.Format(value ?? name, arguments), !availableInTranslate, typeof(TResource).FullName);
}
}
}

0 comments on commit ee0e600

Please sign in to comment.