How do I find all available locales in Python












5















How can I (on a GNU/Linux system) find all available locales to use with the module locale?



The only thing I find that is close in the the module is the dictionary locale_alias with aliases for locales.
That is sometimes mentioned as where to look what locales you have, but it doesn't contain all aliases. On my system this program



#! /usr/bin/python3
import locale
for k, v in sorted(locale.locale_alias.items()):
if k.startswith('fr_') or v.startswith('fr_'):
print('{:20}{}'.format(k, v))


prints



c-french            fr_CA.ISO8859-1
fr fr_FR.ISO8859-1
fr_be fr_BE.ISO8859-1
fr_ca fr_CA.ISO8859-1
fr_ch fr_CH.ISO8859-1
fr_fr fr_FR.ISO8859-1
fr_lu fr_LU.ISO8859-1
français fr_FR.ISO8859-1
fre_fr fr_FR.ISO8859-1
french fr_FR.ISO8859-1
french.iso88591 fr_CH.ISO8859-1
french_france fr_FR.ISO8859-1


ignoring all utf-8 locales, like 'fr_FR.utf8', which can indeed be used as argument for locale.setlocale. From the shell, locale -a | grep "^fr_.*utf8" gives



fr_BE.utf8
fr_CA.utf8
fr_CH.utf8
fr_FR.utf8
fr_LU.utf8


showing lots of options. (One way is of course to run this shell command from Python, but I would have thought there is a way to do this directly from Python.)










share|improve this question























  • Oddly, when I look for endswith(.utf8) I find more than the shell.

    – kabanus
    Nov 15 '18 at 13:21













  • locale_alias apparently sucks - stackoverflow.com/questions/19709026/… - you are not alone.

    – kabanus
    Nov 15 '18 at 13:27


















5















How can I (on a GNU/Linux system) find all available locales to use with the module locale?



The only thing I find that is close in the the module is the dictionary locale_alias with aliases for locales.
That is sometimes mentioned as where to look what locales you have, but it doesn't contain all aliases. On my system this program



#! /usr/bin/python3
import locale
for k, v in sorted(locale.locale_alias.items()):
if k.startswith('fr_') or v.startswith('fr_'):
print('{:20}{}'.format(k, v))


prints



c-french            fr_CA.ISO8859-1
fr fr_FR.ISO8859-1
fr_be fr_BE.ISO8859-1
fr_ca fr_CA.ISO8859-1
fr_ch fr_CH.ISO8859-1
fr_fr fr_FR.ISO8859-1
fr_lu fr_LU.ISO8859-1
français fr_FR.ISO8859-1
fre_fr fr_FR.ISO8859-1
french fr_FR.ISO8859-1
french.iso88591 fr_CH.ISO8859-1
french_france fr_FR.ISO8859-1


ignoring all utf-8 locales, like 'fr_FR.utf8', which can indeed be used as argument for locale.setlocale. From the shell, locale -a | grep "^fr_.*utf8" gives



fr_BE.utf8
fr_CA.utf8
fr_CH.utf8
fr_FR.utf8
fr_LU.utf8


showing lots of options. (One way is of course to run this shell command from Python, but I would have thought there is a way to do this directly from Python.)










share|improve this question























  • Oddly, when I look for endswith(.utf8) I find more than the shell.

    – kabanus
    Nov 15 '18 at 13:21













  • locale_alias apparently sucks - stackoverflow.com/questions/19709026/… - you are not alone.

    – kabanus
    Nov 15 '18 at 13:27
















5












5








5








How can I (on a GNU/Linux system) find all available locales to use with the module locale?



The only thing I find that is close in the the module is the dictionary locale_alias with aliases for locales.
That is sometimes mentioned as where to look what locales you have, but it doesn't contain all aliases. On my system this program



#! /usr/bin/python3
import locale
for k, v in sorted(locale.locale_alias.items()):
if k.startswith('fr_') or v.startswith('fr_'):
print('{:20}{}'.format(k, v))


prints



c-french            fr_CA.ISO8859-1
fr fr_FR.ISO8859-1
fr_be fr_BE.ISO8859-1
fr_ca fr_CA.ISO8859-1
fr_ch fr_CH.ISO8859-1
fr_fr fr_FR.ISO8859-1
fr_lu fr_LU.ISO8859-1
français fr_FR.ISO8859-1
fre_fr fr_FR.ISO8859-1
french fr_FR.ISO8859-1
french.iso88591 fr_CH.ISO8859-1
french_france fr_FR.ISO8859-1


ignoring all utf-8 locales, like 'fr_FR.utf8', which can indeed be used as argument for locale.setlocale. From the shell, locale -a | grep "^fr_.*utf8" gives



fr_BE.utf8
fr_CA.utf8
fr_CH.utf8
fr_FR.utf8
fr_LU.utf8


showing lots of options. (One way is of course to run this shell command from Python, but I would have thought there is a way to do this directly from Python.)










share|improve this question














How can I (on a GNU/Linux system) find all available locales to use with the module locale?



The only thing I find that is close in the the module is the dictionary locale_alias with aliases for locales.
That is sometimes mentioned as where to look what locales you have, but it doesn't contain all aliases. On my system this program



#! /usr/bin/python3
import locale
for k, v in sorted(locale.locale_alias.items()):
if k.startswith('fr_') or v.startswith('fr_'):
print('{:20}{}'.format(k, v))


prints



c-french            fr_CA.ISO8859-1
fr fr_FR.ISO8859-1
fr_be fr_BE.ISO8859-1
fr_ca fr_CA.ISO8859-1
fr_ch fr_CH.ISO8859-1
fr_fr fr_FR.ISO8859-1
fr_lu fr_LU.ISO8859-1
français fr_FR.ISO8859-1
fre_fr fr_FR.ISO8859-1
french fr_FR.ISO8859-1
french.iso88591 fr_CH.ISO8859-1
french_france fr_FR.ISO8859-1


ignoring all utf-8 locales, like 'fr_FR.utf8', which can indeed be used as argument for locale.setlocale. From the shell, locale -a | grep "^fr_.*utf8" gives



fr_BE.utf8
fr_CA.utf8
fr_CH.utf8
fr_FR.utf8
fr_LU.utf8


showing lots of options. (One way is of course to run this shell command from Python, but I would have thought there is a way to do this directly from Python.)







python locale






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 13:13









pstpst

1857




1857













  • Oddly, when I look for endswith(.utf8) I find more than the shell.

    – kabanus
    Nov 15 '18 at 13:21













  • locale_alias apparently sucks - stackoverflow.com/questions/19709026/… - you are not alone.

    – kabanus
    Nov 15 '18 at 13:27





















  • Oddly, when I look for endswith(.utf8) I find more than the shell.

    – kabanus
    Nov 15 '18 at 13:21













  • locale_alias apparently sucks - stackoverflow.com/questions/19709026/… - you are not alone.

    – kabanus
    Nov 15 '18 at 13:27



















Oddly, when I look for endswith(.utf8) I find more than the shell.

– kabanus
Nov 15 '18 at 13:21







Oddly, when I look for endswith(.utf8) I find more than the shell.

– kabanus
Nov 15 '18 at 13:21















locale_alias apparently sucks - stackoverflow.com/questions/19709026/… - you are not alone.

– kabanus
Nov 15 '18 at 13:27







locale_alias apparently sucks - stackoverflow.com/questions/19709026/… - you are not alone.

– kabanus
Nov 15 '18 at 13:27














1 Answer
1






active

oldest

votes


















0














It seems like there is no good way to to this directly from Python, so I'll answer how to run this shell command from Python.



#! /usr/bin/python3
import subprocess

def find_locales():
out = subprocess.run(['locale', '-a'], stdout=subprocess.PIPE).stdout
try:
# Even though I use utf8 on my system output from "locale -a"
# included "bokmål" in Latin-1. Then this won't work, but the
# exception will.
res = out.decode('utf-8')
except:
res = out.decode('latin-1')
return res.rstrip('n').splitlines()

if __name__ == "__main__":
for loc in find_locales():
print(loc)


Note that subprocess.run is new in Python 3.5. For earlier Python versions, see this question on alternative ways to run shell commands.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320311%2fhow-do-i-find-all-available-locales-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It seems like there is no good way to to this directly from Python, so I'll answer how to run this shell command from Python.



    #! /usr/bin/python3
    import subprocess

    def find_locales():
    out = subprocess.run(['locale', '-a'], stdout=subprocess.PIPE).stdout
    try:
    # Even though I use utf8 on my system output from "locale -a"
    # included "bokmål" in Latin-1. Then this won't work, but the
    # exception will.
    res = out.decode('utf-8')
    except:
    res = out.decode('latin-1')
    return res.rstrip('n').splitlines()

    if __name__ == "__main__":
    for loc in find_locales():
    print(loc)


    Note that subprocess.run is new in Python 3.5. For earlier Python versions, see this question on alternative ways to run shell commands.






    share|improve this answer




























      0














      It seems like there is no good way to to this directly from Python, so I'll answer how to run this shell command from Python.



      #! /usr/bin/python3
      import subprocess

      def find_locales():
      out = subprocess.run(['locale', '-a'], stdout=subprocess.PIPE).stdout
      try:
      # Even though I use utf8 on my system output from "locale -a"
      # included "bokmål" in Latin-1. Then this won't work, but the
      # exception will.
      res = out.decode('utf-8')
      except:
      res = out.decode('latin-1')
      return res.rstrip('n').splitlines()

      if __name__ == "__main__":
      for loc in find_locales():
      print(loc)


      Note that subprocess.run is new in Python 3.5. For earlier Python versions, see this question on alternative ways to run shell commands.






      share|improve this answer


























        0












        0








        0







        It seems like there is no good way to to this directly from Python, so I'll answer how to run this shell command from Python.



        #! /usr/bin/python3
        import subprocess

        def find_locales():
        out = subprocess.run(['locale', '-a'], stdout=subprocess.PIPE).stdout
        try:
        # Even though I use utf8 on my system output from "locale -a"
        # included "bokmål" in Latin-1. Then this won't work, but the
        # exception will.
        res = out.decode('utf-8')
        except:
        res = out.decode('latin-1')
        return res.rstrip('n').splitlines()

        if __name__ == "__main__":
        for loc in find_locales():
        print(loc)


        Note that subprocess.run is new in Python 3.5. For earlier Python versions, see this question on alternative ways to run shell commands.






        share|improve this answer













        It seems like there is no good way to to this directly from Python, so I'll answer how to run this shell command from Python.



        #! /usr/bin/python3
        import subprocess

        def find_locales():
        out = subprocess.run(['locale', '-a'], stdout=subprocess.PIPE).stdout
        try:
        # Even though I use utf8 on my system output from "locale -a"
        # included "bokmål" in Latin-1. Then this won't work, but the
        # exception will.
        res = out.decode('utf-8')
        except:
        res = out.decode('latin-1')
        return res.rstrip('n').splitlines()

        if __name__ == "__main__":
        for loc in find_locales():
        print(loc)


        Note that subprocess.run is new in Python 3.5. For earlier Python versions, see this question on alternative ways to run shell commands.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 20 at 13:51









        pstpst

        1857




        1857
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320311%2fhow-do-i-find-all-available-locales-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine