Skip to content

Commit

Permalink
update the usage and add a menu
Browse files Browse the repository at this point in the history
throw exceptions if required
  • Loading branch information
jmsalcido committed May 4, 2021
1 parent 1d16225 commit f40dd4d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
26 changes: 6 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,12 @@ Simply localize text based on the `Resources/i18n/` folder in your Unity project
sample.text = This is a sample text that will appear in the text area.
```

3. Create a `I18NText.cs` script that can be used within Unity to use this, probably will be removed later.
3. Add a `Text` component and add the component `I18NText` from the `UI` context menu.

```c#
using UnityEngine;
using UnityEngine.UI;
using UnityTextI18n;

public class I18NText : MonoBehaviour
{
[SerializeField] private string textId = default;

private void Start()
{
var text = GetComponent<Text>();
if (text == null) return;

text.text = textId == "ISOCode" ? I18N.GetLanguageCode() : I18N.Language[textId];
}
}
```
![i18ntext component][component_image]

### Future of this package:
One of the ideas is to have a `.dll` for each version after version `2019.4.8f1` (LTS only).
- One of the ideas is to have a `.dll` for each version after version `2019.4.8f1` (LTS only).
- Add an editor in unity for all the values

[component_image]: https://i.imgur.com/qieCZ4b.png
5 changes: 1 addition & 4 deletions src/UnityTextI18n/I18N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace UnityTextI18n
{
public class I18N
public static class I18N
{
// ReSharper disable once MemberCanBePrivate.Global
// ReSharper disable once CollectionNeverQueried.Global
public static Dictionary<string, string> Language { get; private set; }

static I18N()
Expand Down Expand Up @@ -41,7 +39,6 @@ private static void LoadLanguage()
}
}

// ReSharper disable once MemberCanBePrivate.Global
public static string GetLanguageCode()
{
return TwoLetterIsoCodeFromSystemLanguage().ToLower();
Expand Down
20 changes: 20 additions & 0 deletions src/UnityTextI18n/I18NEditorWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using UnityEditor;

namespace UnityTextI18n
{
public class I18NEditorWindow : EditorWindow
{
[MenuItem("I18N/Editor")]
private static void Init()
{
var editorWindow = GetWindow(typeof(I18NEditorWindow));
editorWindow.Show();
}

private void OnGUI()
{
throw new NotImplementedException("Tepache Labs is working on this...");
}
}
}
33 changes: 33 additions & 0 deletions src/UnityTextI18n/I18NText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using UnityEngine;
using UnityEngine.UI;

namespace UnityTextI18n
{
[RequireComponent(typeof(Text))]
[AddComponentMenu("UI/I18NText", 10)]
public class I18NText : MonoBehaviour
{
private const string TranslationIdNotFoundInTheI18NOptions = "translationId not found in the i18n resource file";

[SerializeField] protected string translationId = string.Empty;
private void Start()
{
var text = GetComponent<Text>();
if (text == null) return;

if (!I18N.Language.ContainsKey(translationId))
{
throw new NotSupportedException(TranslationIdNotFoundInTheI18NOptions);
}

Translate(text);
}

private void Translate(Text textComponent)
{
textComponent.text = translationId == string.Empty ?
I18N.GetLanguageCode() : I18N.Language[translationId];
}
}
}
1 change: 1 addition & 0 deletions src/UnityTextI18n/UnityTextI18n.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Unity3D.SDK" Version="2019.4.9.1" />
<PackageReference Include="Unity3D.UnityEngine.UI" Version="2018.3.5.1" />
</ItemGroup>

</Project>

0 comments on commit f40dd4d

Please sign in to comment.