Search Unity

Question Store `Locale` variable as `string` ?

Discussion in 'Scripting' started by AerionXI, Jan 27, 2023.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    How can I turn

    Code (CSharp):
    1. public static `Locale` GetLocale ( ) {
    2.     return LocalizationSettings.SelectedLocale;
    3. }
    into

    Code (CSharp):
    1. public static `string` GetLocale ( ) {
    2.     return LocalizationSettings.SelectedLocale.`ToString ( )`;
    3. }
    ?

    I need the device' current locale as a string.
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,420
    I don't know why you're trying to put in weird ` marks in your code, is that just to highlight portions for us?

    Have you tried the code you posted? Anything has ToString(), and the class Locale seems to have an override. What's actually not working for you?
     
  3. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @halley yes, that is just an attempt to highlight things for you.
     
  4. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @halley I am simply trying to turn return LocalizationSettings.SelectedLocale into a string so i can check a list with it.
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,420
    But you gave the desired code. Use it. Print the results to the console. If that's not working, tell us what results you are getting and what is not sufficient.
     
    Bunny83 likes this.
  6. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @halley so when I try to do

    Code (CSharp):
    1. if ( GetLocale ( ) == "English" ) {
    2.     // ...
    3. }
    I get

    Code (CSharp):
    1. Error CS0019 Operator '==' cannot be applied to operands of type 'Locale' and 'string'
     
  7. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Nevermind. Figured it out. had to add ".name" to the end of it and remove .ToString ( )! Thanks anyway!
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,979
    Well, that of course depends on what exactly you're looking for. The Locale class is derived from ScriptableObject and as such it does have a "name" property which returns the actual asset name. Though the class has other strings as well like Identifier (which has a string property "Code") or LocaleName.
     
    karl_jones likes this.