From dd256ff3bcef8c77a59c93d85c09117d38a9f891 Mon Sep 17 00:00:00 2001 From: Allen Date: Tue, 21 May 2019 01:50:12 +1000 Subject: [PATCH 1/5] Added LanguageService for I18N --- PumaShared/I18N/ILanguageService.cs | 12 +++++ PumaShared/I18N/LanguageService.cs | 70 +++++++++++++++++++++++++++++ PumaShared/PumaShared.projitems | 2 + 3 files changed, 84 insertions(+) create mode 100644 PumaShared/I18N/ILanguageService.cs create mode 100644 PumaShared/I18N/LanguageService.cs diff --git a/PumaShared/I18N/ILanguageService.cs b/PumaShared/I18N/ILanguageService.cs new file mode 100644 index 0000000..3a81b9f --- /dev/null +++ b/PumaShared/I18N/ILanguageService.cs @@ -0,0 +1,12 @@ +namespace PumaFramework.Shared.I18N +{ + +public interface ILanguageService +{ + string Get(string path); + + string Format(string path, params object[] args); +} + + +} \ No newline at end of file diff --git a/PumaShared/I18N/LanguageService.cs b/PumaShared/I18N/LanguageService.cs new file mode 100644 index 0000000..389327e --- /dev/null +++ b/PumaShared/I18N/LanguageService.cs @@ -0,0 +1,70 @@ +using System.Collections.Generic; +using System.Linq; +using CitizenFX.Core.Native; + +namespace PumaFramework.Shared.I18N +{ + +public class LanguageService : ILanguageService +{ + readonly Dictionary _gameLanguageIdDict = new Dictionary + { + {0, Language.English}, + {1, Language.French}, + {2, Language.German}, + {3, Language.Italian}, + {4, Language.Spanish}, + {5, Language.Portuguese}, + {6, Language.Polish}, + {7, Language.Russian}, + {8, Language.Korean}, + {9, Language.ChineseTraditional}, + {10, Language.Japanese}, + // Mexico + {11, Language.Spanish}, + {12, Language.ChineseSimplified} + }; + + readonly LocalizedStringSet _localizedStringSet; + + readonly List _fallbackStringSets; + + #if CLIENT + public LanguageService(string feature) + { + _localizedStringSet = new LocalizedStringSet(feature, _gameLanguageIdDict[API.GetCurrentLanguageId()]); + var fallbacks = LanguageDescription.Get(_gameLanguageIdDict[API.GetCurrentLanguageId()]).Fallbacks; + _fallbackStringSets = fallbacks.Select(fallback => new LocalizedStringSet(feature, fallback)).ToList(); + } + #endif + + public string Get(string path) + { + var str = _localizedStringSet.Get(path); + if (!path.Equals(str)) return str; + foreach (var fallbackStringSet in _fallbackStringSets) + { + str = fallbackStringSet.Get(path); + if (!path.Equals(str)) return str; + } + + // return #+path+# if both are empty + return $"#{str}#"; + } + + public string Format(string path, params object[] args) + { + var str = _localizedStringSet.Get(path); + if (!path.Equals(str)) return string.Format(str, args); + foreach (var fallbackStringSet in _fallbackStringSets) + { + str = fallbackStringSet.Get(path); + if (!path.Equals(str)) return string.Format(str, args); + } + + // return #+path+# if both are empty + return $"#{str}#"; + } +} + +} \ No newline at end of file diff --git a/PumaShared/PumaShared.projitems b/PumaShared/PumaShared.projitems index 78f7fe8..c72e671 100644 --- a/PumaShared/PumaShared.projitems +++ b/PumaShared/PumaShared.projitems @@ -15,8 +15,10 @@ + + From 43c14695041501ea64f736a6a0b6010524409c45 Mon Sep 17 00:00:00 2001 From: Allen Date: Tue, 21 May 2019 01:56:09 +1000 Subject: [PATCH 2/5] Updated Format to utilize existing function --- PumaShared/I18N/LanguageService.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PumaShared/I18N/LanguageService.cs b/PumaShared/I18N/LanguageService.cs index 389327e..4223ae3 100644 --- a/PumaShared/I18N/LanguageService.cs +++ b/PumaShared/I18N/LanguageService.cs @@ -54,12 +54,12 @@ public string Get(string path) public string Format(string path, params object[] args) { - var str = _localizedStringSet.Get(path); - if (!path.Equals(str)) return string.Format(str, args); + var str = _localizedStringSet.Format(path, args); + if (!path.Equals(str)) return str; foreach (var fallbackStringSet in _fallbackStringSets) { - str = fallbackStringSet.Get(path); - if (!path.Equals(str)) return string.Format(str, args); + str = fallbackStringSet.Format(path, args); + if (!path.Equals(str)) return str; } // return #+path+# if both are empty From 2466cae4073afa2a3edd5b8c1db595f83b490e2b Mon Sep 17 00:00:00 2001 From: allen Date: Tue, 21 May 2019 13:49:23 +1000 Subject: [PATCH 3/5] Added #if for CLIENT side function --- PumaShared/I18N/ILanguageService.cs | 4 ++++ PumaShared/I18N/LanguageService.cs | 27 +++++++++++++-------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/PumaShared/I18N/ILanguageService.cs b/PumaShared/I18N/ILanguageService.cs index 3a81b9f..cc0530b 100644 --- a/PumaShared/I18N/ILanguageService.cs +++ b/PumaShared/I18N/ILanguageService.cs @@ -3,6 +3,10 @@ namespace PumaFramework.Shared.I18N public interface ILanguageService { + #if CLIENT + Language GetCurrentGameLanguage(); + #endif + string Get(string path); string Format(string path, params object[] args); diff --git a/PumaShared/I18N/LanguageService.cs b/PumaShared/I18N/LanguageService.cs index 4223ae3..1d0ee09 100644 --- a/PumaShared/I18N/LanguageService.cs +++ b/PumaShared/I18N/LanguageService.cs @@ -29,41 +29,40 @@ public class LanguageService : ILanguageService readonly List _fallbackStringSets; - #if CLIENT - public LanguageService(string feature) + public LanguageService(string feature, Language language) { - _localizedStringSet = new LocalizedStringSet(feature, _gameLanguageIdDict[API.GetCurrentLanguageId()]); - var fallbacks = LanguageDescription.Get(_gameLanguageIdDict[API.GetCurrentLanguageId()]).Fallbacks; + _localizedStringSet = new LocalizedStringSet(feature, language); + var fallbacks = LanguageDescription.Get(language).Fallbacks; _fallbackStringSets = fallbacks.Select(fallback => new LocalizedStringSet(feature, fallback)).ToList(); } + + #if CLIENT + public Language GetCurrentGameLanguage() + { + return _gameLanguageIdDict[API.GetCurrentLanguageId()]; + } #endif public string Get(string path) { var str = _localizedStringSet.Get(path); - if (!path.Equals(str)) return str; foreach (var fallbackStringSet in _fallbackStringSets) { - str = fallbackStringSet.Get(path); - if (!path.Equals(str)) return str; + if (fallbackStringSet.Get(path) != str) return fallbackStringSet.Get(path); } - // return #+path+# if both are empty - return $"#{str}#"; + return str; } public string Format(string path, params object[] args) { var str = _localizedStringSet.Format(path, args); - if (!path.Equals(str)) return str; foreach (var fallbackStringSet in _fallbackStringSets) { - str = fallbackStringSet.Format(path, args); - if (!path.Equals(str)) return str; + if (fallbackStringSet.Format(path, args) != str) return fallbackStringSet.Format(path, args); } - // return #+path+# if both are empty - return $"#{str}#"; + return str; } } From 91926d7800ee6fc921296ff8587c28e6e26a4b50 Mon Sep 17 00:00:00 2001 From: allen Date: Tue, 21 May 2019 16:00:46 +1000 Subject: [PATCH 4/5] Updated client functions --- PumaShared/I18N/ILanguageService.cs | 4 ---- PumaShared/I18N/LanguageService.cs | 11 ++++------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/PumaShared/I18N/ILanguageService.cs b/PumaShared/I18N/ILanguageService.cs index cc0530b..3a81b9f 100644 --- a/PumaShared/I18N/ILanguageService.cs +++ b/PumaShared/I18N/ILanguageService.cs @@ -3,10 +3,6 @@ namespace PumaFramework.Shared.I18N public interface ILanguageService { - #if CLIENT - Language GetCurrentGameLanguage(); - #endif - string Get(string path); string Format(string path, params object[] args); diff --git a/PumaShared/I18N/LanguageService.cs b/PumaShared/I18N/LanguageService.cs index 1d0ee09..1c88222 100644 --- a/PumaShared/I18N/LanguageService.cs +++ b/PumaShared/I18N/LanguageService.cs @@ -29,19 +29,16 @@ public class LanguageService : ILanguageService readonly List _fallbackStringSets; - public LanguageService(string feature, Language language) + public LanguageService(string feature, Language language = Language.English ) { + #if CLIENT + language = _gameLanguageIdDict[API.GetCurrentLanguageId()]; + #endif _localizedStringSet = new LocalizedStringSet(feature, language); var fallbacks = LanguageDescription.Get(language).Fallbacks; _fallbackStringSets = fallbacks.Select(fallback => new LocalizedStringSet(feature, fallback)).ToList(); } - #if CLIENT - public Language GetCurrentGameLanguage() - { - return _gameLanguageIdDict[API.GetCurrentLanguageId()]; - } - #endif public string Get(string path) { From 633134e16262ffd67b997700a3f3222c291a837b Mon Sep 17 00:00:00 2001 From: allen Date: Tue, 21 May 2019 16:28:04 +1000 Subject: [PATCH 5/5] Added comments to user use interface impl --- PumaShared/I18N/ILanguageService.cs | 17 +++++++++++++++++ PumaShared/I18N/LanguageService.cs | 26 +++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/PumaShared/I18N/ILanguageService.cs b/PumaShared/I18N/ILanguageService.cs index 3a81b9f..bdbfe53 100644 --- a/PumaShared/I18N/ILanguageService.cs +++ b/PumaShared/I18N/ILanguageService.cs @@ -1,3 +1,20 @@ +/* + * This file is part of PumaFramework. + * + * PumaFramework is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PumaFramework 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with PumaFramework. If not, see . + */ + namespace PumaFramework.Shared.I18N { diff --git a/PumaShared/I18N/LanguageService.cs b/PumaShared/I18N/LanguageService.cs index 1c88222..bfc9bfa 100644 --- a/PumaShared/I18N/LanguageService.cs +++ b/PumaShared/I18N/LanguageService.cs @@ -1,3 +1,20 @@ +/* + * This file is part of PumaFramework. + * + * PumaFramework is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * PumaFramework 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with PumaFramework. If not, see . + */ + using System.Collections.Generic; using System.Linq; using CitizenFX.Core.Native; @@ -29,6 +46,7 @@ public class LanguageService : ILanguageService readonly List _fallbackStringSets; + // if it is client side code, auto detect game language public LanguageService(string feature, Language language = Language.English ) { #if CLIENT @@ -39,7 +57,12 @@ public LanguageService(string feature, Language language = Language.English ) _fallbackStringSets = fallbacks.Select(fallback => new LocalizedStringSet(feature, fallback)).ToList(); } - + /* + since LocalizedStringSet.Get returns path if not found + if fallback strings also returns path, it means either: + 1) value is the key 2) not defined in anyway + hence default to path(key) + */ public string Get(string path) { var str = _localizedStringSet.Get(path); @@ -51,6 +74,7 @@ public string Get(string path) return str; } + // same logic as Get() public string Format(string path, params object[] args) { var str = _localizedStringSet.Format(path, args);