Skip to content

Commit

Permalink
added section about fallback languages
Browse files Browse the repository at this point in the history
  • Loading branch information
valdisiljuconoks committed Mar 8, 2020
1 parent 8c19089 commit 1e0c379
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -35,6 +35,7 @@ Whole package of libraries is split into multiple git repos (with submodule link
* [Synchronization Process](docs/sync-net.md)
* [MSSQL Storage Configuration](docs/mssql.md)
* [Working with Resources](docs/working-with-resources-net.md)
* [Working with Languages](docs/working-with-languages-net.md)
* [Translating System.Enum Types](docs/translate-enum-net.md)
* [Mark Required Fields](docs/required-fields.md)
* [Foreign Resources](docs/foreign-resources.md)
Expand Down
36 changes: 36 additions & 0 deletions docs/working-with-languages-net.md
@@ -0,0 +1,36 @@
# Working with Languages

## Fallback Languages

LocalizationProvider gives you option to configure fallback languages for translation lookup.
It means that provider will try to get translation in requested language. And if it does not exist in that language, fallback language list is used to decide which language to try next until either succeeds or fails with no translation found.

To configure fallback languages use code below:

```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbLocalizationProvider(_ =>
{
...

// try "sv" -> "no" -> "en"
_.FallbackCultures
.Try(new CultureInfo("sv"))
.Then(new CultureInfo("no"))
.Then(new CultureInfo("en"));

_.EnableInvariantCultureFallback = true;
});
}
}
```

This means that following logic will be used during translation lookup:

1) Developer requests translation in Swedish culture (`"sv"`) using `ILocalizationProvider.GetString(() => ...)` method.
2) If translation does not exist -> provider is looking for translation in Norwegian language (`"no"` - second language in the fallback list).
3) If translation is found - one is returned; if not - provider continues process and is looking for translation in English (`"en"`).
4) If there is no translation in English -> depending on `ConfigurationContext.EnableInvariantCultureFallback` setting -> translation in InvariantCulture may be returned.

0 comments on commit 1e0c379

Please sign in to comment.