BerylliumLocalizationManager 1.5.0

Suggested Alternatives

Beryllium.LocalizationManager

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package BerylliumLocalizationManager --version 1.5.0
                    
NuGet\Install-Package BerylliumLocalizationManager -Version 1.5.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BerylliumLocalizationManager" Version="1.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BerylliumLocalizationManager" Version="1.5.0" />
                    
Directory.Packages.props
<PackageReference Include="BerylliumLocalizationManager" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BerylliumLocalizationManager --version 1.5.0
                    
#r "nuget: BerylliumLocalizationManager, 1.5.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BerylliumLocalizationManager@1.5.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BerylliumLocalizationManager&version=1.5.0
                    
Install as a Cake Addin
#tool nuget:?package=BerylliumLocalizationManager&version=1.5.0
                    
Install as a Cake Tool

BerylliumLocalizationManager

Localization manager for simple and efficient switching between languages. It reads the localization folders produced by Beryllium Localization Helper and serves translated texts by string ID: pick a current language, look strings up, keep translation files in memory, and bind callbacks that follow every language switch.

  • Targets .NET 10, no dependencies.
  • Nothing throws for a bad folder or a bad lookup: every operation is a Try... method with an error message.

Installation

dotnet add package BerylliumLocalizationManager

Localization folder layout

The folder is written by Beryllium Localization Helper and looks like this:

<localization folder>/
  Languages.manifest        languages: english name, local name, reference flag
  FilesStructure.manifest   tree of folders and translation files
  StringIDs.manifest        string IDs and the file each one belongs to
  <language english name>/  one folder per language, mirroring the files structure
    Menus/
      main.json             { "string_id": "translated text", ... }

Translation files are plain JSON objects mapping a string ID to its text. An empty text means "not translated yet". The manifests only store what cannot be derived: paths come from the tree, translation status from the files.

Quick start

using BerylliumLocalizationManager;

var localization = new LocalizationManager();

if (!localization.TryLoadLocalizationFolder(@"D:\Game\Localization", out var error))
    throw new InvalidOperationException(error);

// the reference language is a good default until the player picks one
localization.TrySetCurrentLanguage(localization.ReferenceLanguage!.EnglishName, out _);

// read the whole language into memory once, then lookups never touch the disk
localization.TryCacheAllFiles(out _);

if (localization.TryGetLocalizedStringById("menu_start", out var text, out error))
    Console.WriteLine(text);
else
    Console.WriteLine($"menu_start: {error}");

// a bound callback receives the text now and after every language switch
localization.TryBindToStringId("menu_start", value => startButton.Text = value, out _);

localization.TrySetCurrentLanguage("Ukrainian", out _); // startButton.Text is updated

Behaviour worth knowing

  • Untranslated strings. A string whose text is empty (or null) in the translation file is not translated. A lookup fails with Language doesn't have localized version for this string ID, and a bound callback receives an empty string. To fall back, look the string up in ReferenceLanguage yourself.
  • Caching. Lookups keep the translation file they read in memory (cacheContainingFile defaults to true), so a string costs one file read the first time and a dictionary lookup afterwards. TryCacheAllFiles preloads a whole language, ClearCache drops everything. A cached file is not re-read when it changes on disk; reload the folder or clear the cache.
  • Bindings. A callback is registered once per string ID and callbacks run in the order they were bound. When a language switch runs your OnLanguageChanged handlers and bound callbacks, an exception thrown by one of them does not stop the others and does not leave the manager half-updated: the switch completes, then the exceptions are rethrown together as an AggregateException.
  • Reloading. TryLoadLocalizationFolder can be called again, for the same or another folder. When it fails nothing changes and the previous folder stays usable. When it succeeds the cache is dropped, the current language is kept if the new folder has a language with the same English name (otherwise it becomes null and OnLanguageChanged is raised), and every bound callback receives its text again.
  • Names and paths. Language names and relative file paths are matched ignoring letter case, and a relative path may use either separator (Menus/main.json or Menus\main.json). String IDs are case-sensitive. Paths are always resolved inside the localization folder; a manifest pointing outside of it is rejected.
  • Corrupted folders. A missing or malformed manifest, a files structure that is not a tree, or duplicate names and IDs make the load fail with a message that names the problem.
  • Threading. One instance serves one folder and is meant to be used from one thread at a time.

API overview

Member Purpose
TryLoadLocalizationFolder(path, out error) Loads the manifests. Safe to call again to reload.
IsLoaded, LocalizationFolderPath Whether and what is loaded.
Languages, Files, StringIDs The loaded manifests, as read-only dictionaries.
ReferenceLanguage The language everything else is translated from.
TrySetCurrentLanguage(englishName, out error), CurrentLanguage The language used by the methods without a language parameter.
OnLanguageChanged Raised after the current language changed, with the previous and the new language.
TryGetLocalizedStringById(stringId, out text, out error, cacheContainingFile) Text of a string in the current language.
TryGetLocalizedStringByIdForLanguage(englishName, stringId, ...) Same for any language.
TryCacheFileByRelativePath(relativePath, out error), ...ForLanguage Reads one translation file into memory.
TryCacheAllFiles(out error), ...ForLanguage Reads every translation file of a language into memory.
ClearCache() Drops the cached files.
TryBindToStringId(stringId, callback, out error), TryUnbindFromStringId(...) Callbacks that receive the text now and after every language switch.
ClearStringIDsBindings() Removes every callback.

Compatibility

Version 1.5.0 reads the folders written by Beryllium Localization Helper 1.1 and later (paths derived from the tree, translation status derived from the files) and also the folders written by earlier versions, which stored both in the manifests. Version 1.0.0 only reads the earlier folders.

Changelog

1.5.0

  • Targets .NET 10.
  • Reads the current localization helper format; earlier folders still load.
  • Translation files are cached by default; TryCacheAllFiles preloads a language.
  • Language names and relative paths ignore letter case; relative paths accept either separator.
  • A folder can be reloaded in place; a failed load keeps the previous folder.
  • Exceptions from handlers and bound callbacks no longer interrupt a language switch; they are rethrown together afterwards.
  • Paths are confined to the localization folder; corrupted manifests are reported instead of crashing.
  • ReferenceLanguage, IsLoaded, LocalizationFolderPath and FileEntry.RelativeFilePath added.
  • Newtonsoft.Json dependency removed.
  • Breaking: LanguageEntry.TranslatedStringIDs removed; Languages, Files and StringIDs are read-only; a lookup of an untranslated string fails whether or not its file is cached.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated