Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PersonNameExtensions.cs 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #if ((UNITY_IOS || UNITY_TVOS || UNITY_STANDALONE_OSX) && !UNITY_EDITOR)
  2. #define NATIVE_PERSON_NAME_COMPONENTS_AVAILABLE
  3. #endif
  4. using AppleAuth.Enums;
  5. using AppleAuth.Interfaces;
  6. namespace AppleAuth.Extensions
  7. {
  8. public static class PersonNameExtensions
  9. {
  10. public static string ToLocalizedString(
  11. this IPersonName personName,
  12. PersonNameFormatterStyle style = PersonNameFormatterStyle.Default,
  13. bool usePhoneticRepresentation = false)
  14. {
  15. #if NATIVE_PERSON_NAME_COMPONENTS_AVAILABLE
  16. var jsonString = JsonStringForPersonName(personName);
  17. var localizedString = PInvoke.AppleAuth_GetPersonNameUsingFormatter(jsonString, (int) style, usePhoneticRepresentation);
  18. if (localizedString != null)
  19. {
  20. return localizedString;
  21. }
  22. #endif
  23. var orderedParts = new System.Collections.Generic.List<string>();
  24. if (string.IsNullOrEmpty(personName.NamePrefix))
  25. orderedParts.Add(personName.NamePrefix);
  26. if (string.IsNullOrEmpty(personName.GivenName))
  27. orderedParts.Add(personName.GivenName);
  28. if (string.IsNullOrEmpty(personName.MiddleName))
  29. orderedParts.Add(personName.MiddleName);
  30. if (string.IsNullOrEmpty(personName.FamilyName))
  31. orderedParts.Add(personName.FamilyName);
  32. if (string.IsNullOrEmpty(personName.NameSuffix))
  33. orderedParts.Add(personName.NameSuffix);
  34. return string.Join(" ", orderedParts.ToArray());
  35. }
  36. #if NATIVE_PERSON_NAME_COMPONENTS_AVAILABLE
  37. private const string StringDictionaryFormat = "\"{0}\": \"{1}\",";
  38. private const string StringObjectFormat = "\"{0}\": {1},";
  39. private static string JsonStringForPersonName(IPersonName personName)
  40. {
  41. if (personName == null)
  42. return null;
  43. var stringBuilder = new System.Text.StringBuilder();
  44. stringBuilder.Append("{");
  45. TryAddKeyValue(StringDictionaryFormat, "_namePrefix", personName.NamePrefix, stringBuilder);
  46. TryAddKeyValue(StringDictionaryFormat, "_givenName", personName.GivenName, stringBuilder);
  47. TryAddKeyValue(StringDictionaryFormat, "_middleName", personName.MiddleName, stringBuilder);
  48. TryAddKeyValue(StringDictionaryFormat, "_familyName", personName.FamilyName, stringBuilder);
  49. TryAddKeyValue(StringDictionaryFormat, "_nameSuffix", personName.NameSuffix, stringBuilder);
  50. TryAddKeyValue(StringDictionaryFormat, "_nickname", personName.Nickname, stringBuilder);
  51. var phoneticRepresentationJson = JsonStringForPersonName(personName.PhoneticRepresentation);
  52. TryAddKeyValue(StringObjectFormat, "_phoneticRepresentation", phoneticRepresentationJson, stringBuilder);
  53. stringBuilder.Append("}");
  54. return stringBuilder.ToString();
  55. }
  56. private static void TryAddKeyValue(string format, string key, string value, System.Text.StringBuilder stringBuilder)
  57. {
  58. if (string.IsNullOrEmpty(value))
  59. return;
  60. stringBuilder.AppendFormat(format, key, value);
  61. }
  62. private static class PInvoke
  63. {
  64. #if UNITY_IOS || UNITY_TVOS
  65. private const string DllName = "__Internal";
  66. #elif UNITY_STANDALONE_OSX
  67. private const string DllName = "MacOSAppleAuthManager";
  68. #endif
  69. [System.Runtime.InteropServices.DllImport(DllName)]
  70. public static extern string AppleAuth_GetPersonNameUsingFormatter(string payload, int style, bool usePhoneticRepresentation);
  71. }
  72. #endif
  73. }
  74. }