Skip to content

Commit

Permalink
Missing Folder in previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
winfr1 committed Jan 27, 2022
1 parent 4f5a327 commit ce1f59f
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public abstract class AbstractLocalizationObserver : MonoBehaviour
#region Lifecycle
protected virtual void Awake()
{
l10nManager.Instance.LocaleChanged += OnLocaleChanged;
l10nDependencyProvider.Instance.Observable.LocaleChanged += OnLocaleChanged;
}

protected virtual void OnDisable()
{
l10nDependencyProvider.Instance.Observable.LocaleChanged -= OnLocaleChanged;
}

#endregion
Expand Down
4 changes: 3 additions & 1 deletion l10n/Assets/Scripts/Localization/ILocalizationObservable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public interface ILocalizationObservable
/// <summary>
/// The currently selected language setting.
/// </summary>
string CurrentLocale { get; set; }
string CurrentLocale { get; }

void LoadLocale(string newLocale);

}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace l10n.Localization.observables
{
/// <summary>
/// Abstraction for Arguments for the event <see cref="ILocalizationObservable.LocaleChanged"/>
/// </summary>
public interface ILocaleChangedEventArgs
{
/// <summary>
/// New Language that was selected.
/// </summary>
string NewLocale { get; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace l10n.Localization.observables
{
public class LocaleChangedEventArgs : ILocaleChangedEventArgs
{
#region Properties

private readonly string s_newLocale;
public string NewLocale => s_newLocale;

#endregion

public LocaleChangedEventArgs(string newLocale)
{
s_newLocale = newLocale;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions l10n/Assets/Scripts/Localization/Observables/l10nManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using l10n.common;
using l10n.Localization.sources;
using l10n.Localization.translations;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace l10n.Localization.observables
{
/// <summary>
/// Central Manager for localization settings of the application.
/// Does all localization processing and generation of translations during runtime.
/// </summary>

public class l10nManager : Singleton<l10nManager>, ILocalizationObservable
{

#region Properties
[SerializeField]
private ILocalizationLogger s_logger;
public ILocalizationLogger Logger => s_logger ?? (s_logger = new LocalizationLogger());

[SerializeField]
private string s_currentLocale;
public string CurrentLocale
{
get { return s_currentLocale; }
set
{
Logger.Log(string.Format("Localization Changed from {0} to {1}", s_currentLocale, value), LogType.Log);
s_currentLocale = value;
await l10nDependencyProvider.Instance.Provider.LoadTranslationsAsync();
LocaleChangedEventArgs args = new LocaleChangedEventArgs(s_currentLocale);
s_localeChanged.Invoke(this, args);
}
}
private event EventHandler<ILocaleChangedEventArgs> s_localeChanged;

public event EventHandler<ILocaleChangedEventArgs> LocaleChanged
{
add
{
s_localeChanged += value;
}
remove
{
s_localeChanged -= value;
}
}

[SerializeField]
private ILocalizationDataHandler s_dataHandler;

#endregion

/// <summary>
/// Private Constructor to prevent creation of other Instances.
/// </summary>
private l10nManager() { }

}
}
11 changes: 11 additions & 0 deletions l10n/Assets/Scripts/Localization/Observables/l10nManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using l10n.Localization.sources;
using l10n.Localization.translations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;


Expand All @@ -17,14 +19,31 @@ public interface ILocalizationProvider
/// </summary>
IList<AbstractTranslation> Translations { get; }

IList<ILocalizationDataHandler> DataHandlers { get; }

/// <summary>
/// Retrieves a translation for a given key.
/// </summary>
/// <param name="key">The key mapped to the translation.</param>
/// <returns>The translated object.</returns>
AbstractTranslation Translate(string key);

void LoadTranslationsAsync();
/// <summary>
/// Loads all Translations from registered <see cref="ILocalizationDataHandler"/> asynchronously.
/// </summary>
Task LoadTranslationsAsync();

/// <summary>
/// Unregisters <see cref="ILocalizationDataHandler"/> to this LocalizationProvider.
/// </summary>
/// <param name="handler"></param>
void register(ILocalizationDataHandler handler);

/// <summary>
/// Unregisters <see cref="ILocalizationDataHandler"/> to this LocalizationProvider.
/// </summary>
/// <param name="handler"></param>
void unregister(ILocalizationDataHandler handler);

/// <summary>
/// Registers a Translation from a <see cref="ILocalizationDataHandler"/> Object.
Expand Down
48 changes: 48 additions & 0 deletions l10n/Assets/Scripts/Localization/Provider/LocalizationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using l10n.Localization.sources;
using l10n.Localization.translations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace l10n.Localization.provider
{
public class LocalizationProvider : ILocalizationProvider
{

[SerializeField]
private List<AbstractTranslation> s_translations;
public IList<AbstractTranslation> Translations => s_translations ?? throw new Exception();

[SerializeField]
private List<ILocalizationDataHandler> s_dataHandlers;
public IList<ILocalizationDataHandler> DataHandlers => s_dataHandlers;

public async Task LoadTranslationsAsync()
{
//TODO

}

public void register(ILocalizationDataHandler handler)
{
s_dataHandlers.Add(handler);
}

public void unregister(ILocalizationDataHandler handler)
{
s_dataHandlers.Remove(handler);
}

public bool RegisterTranslation(string key, string locale, object value, object owner)
{
throw new System.NotImplementedException();
}

public AbstractTranslation Translate(string key)
{
throw new System.NotImplementedException();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions l10n/Assets/Scripts/Localization/Sources/AbstractDataHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace l10n.Localization.sources
/// <summary>
/// Base class for Data Sources.
/// </summary>
public abstract class AbstractDataHandler : AbstractLocalizationObserver, ILocalizationDataHandler
public abstract class AbstractDataHandler : ILocalizationDataHandler
{
#region AbstractLocalizationObserver

Expand All @@ -21,9 +21,14 @@ protected override void OnLocaleChanged(object sender, ILocaleChangedEventArgs a
#endregion

#region Lifecycle
protected override void Awake()
protected virtual void Awake()
{
base.Awake();
l10nDependencyProvider.Instance.Provider.register(this);
}

protected virtual void OnDisable()
{
l10nDependencyProvider.Instance.Provider.unregister(this);
}

#endregion
Expand Down

0 comments on commit ce1f59f

Please sign in to comment.