/*
TShock, a server mod for Terraria
Copyright (C) 2022 Janet Blackquill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
global using static TShockAPI.I18n;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using GetText;
using Terraria.Initializers;
using Terraria.Localization;
namespace TShockAPI
{
static class I18n
{
static string TranslationsDirectory => Path.Combine(AppContext.BaseDirectory, "i18n");
static CultureInfo TranslationCultureInfo
{
get {
// cross-platform mapping of cultureinfos can be a bit screwy, so give our users
// the chance to explicitly spell out which translation they would like to use.
// this is an environment variable instead of a flag because this needs to be
// valid whether the passed flags are in a sane state or not.
if (Environment.GetEnvironmentVariable("TSHOCK_LANGUAGE") is string overrideLang)
{
return new CultureInfo(overrideLang);
}
static CultureInfo Redirect(CultureInfo cultureInfo)
=> cultureInfo.Name == "zh-Hans" ? new CultureInfo("zh-CN") : cultureInfo;
if (Terraria.Program.LaunchParameters.TryGetValue("-lang", out var langArg)
&& int.TryParse(langArg, out var langId)) {
if (GameCulture._legacyCultures.TryGetValue(langId, out var culture)) {
return Redirect(culture.CultureInfo);
}
}
if (Terraria.Program.LaunchParameters.TryGetValue("-language", out var languageArg)) {
var culture = GameCulture._legacyCultures.Values.SingleOrDefault(c => c.Name == languageArg);
if (culture != null) {
return Redirect(culture.CultureInfo);
}
}
return CultureInfo.CurrentUICulture;
}
}
/// Instance of a GetText.Catalog loaded with TShockAPI translations for user's specified language
public static Catalog C = new Catalog("TShockAPI", TranslationsDirectory, TranslationCultureInfo);
#region ICatalog forwarding methods
///
/// Returns translated into the selected language.
/// Similar to gettext function.
///
/// Text to translate.
/// Translated text.
static string GetString(FormattableStringAdapter text)
{
return C.GetString(text);
}
///
/// Returns translated into the selected language.
/// Similar to gettext function.
///
/// Text to translate.
/// Translated text.
public static string GetString(FormattableString text)
{
return C.GetString(text);
}
///
/// Returns translated into the selected language.
/// Similar to gettext function.
///
/// Text to translate.
/// Optional arguments for method.
/// Translated text.
public static string GetString(FormattableStringAdapter text, params object[] args)
{
return C.GetString(text, args);
}
///
/// Returns the plural form for of the translation of .
/// Similar to gettext function.
///
/// Singular form of message to translate.
/// Plural form of message to translate.
/// Value that determines the plural form.
/// Translated text.
public static string GetPluralString(FormattableStringAdapter text, FormattableStringAdapter pluralText, long n)
{
return C.GetPluralString(text, pluralText, n);
}
///
/// Returns the plural form for of the translation of .
/// Similar to gettext function.
///
/// Singular form of message to translate.
/// Plural form of message to translate.
/// Value that determines the plural form.
/// Translated text.
public static string GetPluralString(FormattableString text, FormattableString pluralText, long n)
{
return C.GetPluralString(text, pluralText, n);
}
///
/// Returns the plural form for of the translation of .
/// Similar to gettext function.
///
/// Singular form of message to translate.
/// Plural form of message to translate.
/// Value that determines the plural form.
/// Optional arguments for method.
/// Translated text.
public static string GetPluralString(FormattableStringAdapter text, FormattableStringAdapter pluralText, long n, params object[] args)
{
return C.GetPluralString(text, pluralText, n, args);
}
///
/// Returns translated into the selected language using given .
/// Similar to pgettext function.
///
/// Context.
/// Text to translate.
/// Translated text.
public static string GetParticularString(string context, FormattableStringAdapter text)
{
return C.GetParticularString(context, text);
}
///
/// Returns translated into the selected language using given .
/// Similar to pgettext function.
///
/// Context.
/// Text to translate.
/// Translated text.
public static string GetParticularString(string context, FormattableString text)
{
return C.GetParticularString(context, text);
}
///
/// Returns translated into the selected language using given .
/// Similar to pgettext function.
///
/// Context.
/// Text to translate.
/// Optional arguments for method.
/// Translated text.
public static string GetParticularString(string context, FormattableStringAdapter text, params object[] args)
{
return C.GetParticularString(context, text, args);
}
///
/// Returns the plural form for of the translation of using given .
/// Similar to npgettext function.
///
/// Context.
/// Singular form of message to translate.
/// Plural form of message to translate.
/// Value that determines the plural form.
/// Translated text.
public static string GetParticularPluralString(string context, FormattableStringAdapter text, FormattableStringAdapter pluralText, long n)
{
return C.GetParticularPluralString(context, text, pluralText, n);
}
///
/// Returns the plural form for of the translation of using given .
/// Similar to npgettext function.
///
/// Context.
/// Singular form of message to translate.
/// Plural form of message to translate.
/// Value that determines the plural form.
/// Translated text.
public static string GetParticularPluralString(string context, FormattableString text, FormattableString pluralText, long n)
{
return C.GetParticularPluralString(context, text, pluralText, n);
}
///
/// Returns the plural form for of the translation of using given .
/// Similar to npgettext function.
///
/// Context.
/// Singular form of message to translate.
/// Plural form of message to translate.
/// Value that determines the plural form.
/// Optional arguments for method.
/// Translated text.
public static string GetParticularPluralString(string context, FormattableStringAdapter text, FormattableStringAdapter pluralText, long n, params object[] args)
{
return C.GetParticularPluralString(context, text, pluralText, n, args);
}
#endregion
}
}