Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Visuals / VisualObject / AdvancedTextVisual.cs
1 // Copyright (c) 2024 Samsung Electronics Co., Ltd.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15
16 extern alias TizenSystemSettings;
17 using TizenSystemSettings.Tizen.System;
18
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.ComponentModel;
23
24 namespace Tizen.NUI.Visuals
25 {
26     /// <summary>
27     /// The text visual with advanced options.
28     /// </summary>
29     /// <remarks>
30     /// It will be used when we want to control TextVisual with more options.
31     /// This visual allow to translated text with SID.
32     /// </remarks>
33     [EditorBrowsable(EditorBrowsableState.Never)]
34     public class AdvancedTextVisual : Visuals.TextVisual
35     {
36         #region Internal
37         private string textLabelSid = null;
38
39         private static Tizen.NUI.SystemLocaleLanguageChanged systemLocaleLanguageChanged = new Tizen.NUI.SystemLocaleLanguageChanged();
40         private bool hasSystemLanguageChanged = false;
41         #endregion
42
43         #region Constructor
44         public AdvancedTextVisual() : base()
45         {
46         }
47         #endregion
48
49         #region Visual Properties
50         /// <summary>
51         /// The TranslatableText property.<br />
52         /// The text can set the SID value.<br />
53         /// </summary>
54         /// <exception cref='global::System.ArgumentNullException'>
55         /// ResourceManager about multilingual is null.
56         /// </exception>
57         public string TranslatableText
58         {
59             get
60             {
61                 return textLabelSid;
62             }
63             set
64             {
65                 if (NUIApplication.MultilingualResourceManager == null)
66                 {
67                     throw new global::System.ArgumentNullException(null, "ResourceManager about multilingual is null");
68                 }
69                 string translatableText = null;
70                 textLabelSid = value;
71                 translatableText = NUIApplication.MultilingualResourceManager?.GetString(textLabelSid, new global::System.Globalization.CultureInfo(SystemSettings.LocaleLanguage.Replace("_", "-")));
72
73                 if (translatableText != null)
74                 {
75                     Text = translatableText;
76                     if (hasSystemLanguageChanged == false)
77                     {
78                         systemLocaleLanguageChanged.Add(SystemSettingsLocaleLanguageChanged);
79                         hasSystemLanguageChanged = true;
80                     }
81                 }
82                 else
83                 {
84                     Text = value;
85                 }
86             }
87         }
88         #endregion
89
90         #region Internal Method
91         private void SystemSettingsLocaleLanguageChanged(object sender, LocaleLanguageChangedEventArgs e)
92         {
93             string translatableText = null;
94             translatableText = NUIApplication.MultilingualResourceManager?.GetString(textLabelSid, new global::System.Globalization.CultureInfo(e.Value.Replace("_", "-")));
95             if (translatableText != null)
96             {
97                 Text = translatableText;
98             }
99             else
100             {
101                 Tizen.Log.Error("NUI", $"Fail to get translated text : {textLabelSid};");
102                 Text = textLabelSid;
103             }
104         }
105
106         /// <inheritdoc/>
107         [EditorBrowsable(EditorBrowsableState.Never)]
108         protected override void Dispose(DisposeTypes type)
109         {
110             if (Disposed)
111             {
112                 return;
113             }
114
115             if (hasSystemLanguageChanged)
116             {
117                 systemLocaleLanguageChanged.Remove(SystemSettingsLocaleLanguageChanged);
118             }
119
120             base.Dispose(type);
121         }
122         #endregion
123     }
124 }