mesterlum

python [PYTHON] Get Initial Lang

7 posts in this topic

Hello, i maked a simple script for get initial lang... the proposite is automate the choice of the languaje (for servers internationals).

Unknown if there will be something similar

Source:

import requests
 
class CurrentPositionForLang:

    # Code langs
    __LANGS_AVAILABLES = ("ES", "EN", "FR", "TR",)

    # County with lang
    __COUNTRY_CODES_WITH_LANG = (
        ("MX", __LANGS_AVAILABLES[0]),
        ("US", __LANGS_AVAILABLES[1]),
        ("FR", __LANGS_AVAILABLES[2]),
        ("ES", __LANGS_AVAILABLES[0]),
        ("TR", __LANGS_AVAILABLES[3]) 
    )

    @staticmethod
    def get_lang():
        lang = "US" # Default lang for errors
        try:
            r = requests.get("http://ip-api.com/json/")

            if r.status_code != 200:
                return lang

            countryCode = r.json()["countryCode"]

            for country, langCountry in CurrentPositionForLang.__COUNTRY_CODES_WITH_LANG:
                if countryCode == country:
                    return langCountry

            # If not finded return default
            return lang
        except requests.exceptions.ConnectionError:
            return lang
            

For utilicy this class only call this method:

CurrentPositionForLang.get_lang()

this return only String "US, ES..."

 

If you need add more langs, in this fields add the country and the code.

    __LANGS_AVAILABLES = ("ES", "EN", "FR", "TR",)

    # County with lang
    __COUNTRY_CODES_WITH_LANG = (
        ("MX", __LANGS_AVAILABLES[0]),
        ("US", __LANGS_AVAILABLES[1]),
        ("FR", __LANGS_AVAILABLES[2]),
        ("ES", __LANGS_AVAILABLES[0]),
        ("TR", __LANGS_AVAILABLES[3])
    )

This is the standar for code countrys:

https://www.ncbi.nlm.nih.gov/books/NBK7249/

current_position.py

Share this post


Link to post
Share on other sites

Who is the request.py for the libs? But nice idea 

 

import locale 

pprint({"Default locale": locale.getdefaultlocale(), "Default locale data": locale.localeconv()['int_curr_symbol']})
print("")
print({"Current locale": locale.getlocale(), "Current locale data": locale.localeconv()['int_curr_symbol']})
print("")

 

Share this post


Link to post
Share on other sites
11 hours ago, mesterlum said:

i do not understand buddy, if you refer for "requests" is a module from python API. But, my idea is establish initial lang automatic.

Yes I have a Compiler for my phone and the app have many modules but not this sorry for my mistake :)

Share this post


Link to post
Share on other sites
Quote

If you need add more langs, in this fields add the country and the code.


    __LANGS_AVAILABLES = ("ES", "EN", "FR", "TR",)

    # County with lang
    __COUNTRY_CODES_WITH_LANG = (
        ("MX", __LANGS_AVAILABLES[0]),
        ("US", __LANGS_AVAILABLES[1]),
        ("FR", __LANGS_AVAILABLES[2]),
        ("ES", __LANGS_AVAILABLES[0]),
        ("TR", __LANGS_AVAILABLES[3])
    )

 

You can do something like that.

  • root/localeInfo.py
Spoiler

#Search for:
MODE_NAME_LIST = ( PVP_OPTION_NORMAL, PVP_OPTION_REVENGE, PVP_OPTION_KILL, PVP_OPTION_PROTECT, )
#Add after:
LOCALE_REGION_DICT = {
	'af' : ('za',),
	'am' : ('et',),
	'ar' : ('ae', 'bh', 'dz', 'eg', 'iq', 'jo', 'kw', 'lb', 'ly', 'ma', 'om', 'qa', 'sa', 'sy', 'tn', 'ye'),
	'be' : ('by',),
	'bg' : ('bg',),
	'bn' : ('bd',),
	'cs' : ('cz',),
	'da' : ('dk',),
	'de' : ('at', 'ch', 'de', 'li', 'lu'),
	'dv' : ('mv',),
	'el' : ('gr',),
	'en' : ('au', 'bz', 'ca', 'gb', 'ie', 'jm', 'my', 'nz', 'sg', 'tt', 'us', 'za', 'zw'),
	'es' : ('ar', 'bo', 'cl', 'co', 'cr', 'do', 'ec', 'es', 'gt', 'hn', 'mx', 'ni', 'pa', 'pe', 'pr', 'py', 'sv', 'us', 'uy', 've'),
	'et' : ('ee',),
	'fa' : ('ir',),
	'fi' : ('fi',),
	'fil' : ('ph',),
	'fo' : ('fo',),
	'fr' : ('be', 'ca', 'ch', 'fr', 'lu', 'mc'),
	'he' : ('il',),
	'hi' : ('in',),
	'hr' : ('ba', 'hr'),
	'hu' : ('hu',),
	'hy' : ('am',),
	'id' : ('id',),
	'ig' : ('ng',),
	'is' : ('is',),
	'it' : ('ch', 'it'),
	'ja' : ('jp',),
	'ka' : ('ge',),
	'kk' : ('kz',),
	'kl' : ('gl',),
	'km' : ('kh',),
	'ko' : ('kr',),
	'ky' : ('kg',),
	'lb' : ('lu',),
	'lo' : ('la',),
	'lt' : ('lt',),
	'lv' : ('lv',),
	'mi' : ('nz',),
	'mk' : ('mk',),
	'mn' : ('mn',),
	'ms' : ('bn', 'my'),
	'mt' : ('mt',),
	'nb' : ('no',),
	'ne' : ('np',),
	'nl' : ('be', 'nl'),
	'pl' : ('pl',),
	'prs' : ('af',),
	'ps' : ('af',),
	'pt' : ('br', 'pt'),
	'ro' : ('ro',),
	'ru' : ('ru',),
	'rw' : ('rw',),
	'si' : ('lk',),
	'sk' : ('sk',),
	'sl' : ('si',),
	'sq' : ('al',),
	'sv' : ('se',),
	'sw' : ('ke',),
	'th' : ('th',),
	'tk' : ('tm',),
	'tr' : ('tr',),
	'uk' : ('ua',),
	'ur' : ('pk',),
	'vi' : ('vn',),
	'wo' : ('sn',),
	'yo' : ('ng',),
	'zh' : ('cn', 'hk', 'mo', 'sg', 'tw'),
}

 

  • Add at end of line:
import requests
def GetLanguage(locale_region_default = 'en'):
	language = locale_region_default
	try:
		request = requests.get('http://ip-api.com/json/', params=None)
		if request.status_code is 200:
			for locale_region, country_code_tuple in LOCALE_REGION_DICT.iteritems():
				if request.json().get('countryCode').lower() in country_code_tuple:
					language = locale_region

		return language
	except requests.exceptions.ConnectionError:
		return language

# Initialized just one time
LANGUAGE = GetLanguage()

How to use it:

import localeInfo
print localeInfo.LANGUAGE

 

  • Like 2

Share this post


Link to post
Share on other sites
hace 3 horas, VegaS™ dijo:

You can do something like that.

  • root/localeInfo.py
  Reveal hidden contents


#Search for:
MODE_NAME_LIST = ( PVP_OPTION_NORMAL, PVP_OPTION_REVENGE, PVP_OPTION_KILL, PVP_OPTION_PROTECT, )
#Add after:
LOCALE_REGION_DICT = {
	'af' : ('za',),
	'am' : ('et',),
	'ar' : ('ae', 'bh', 'dz', 'eg', 'iq', 'jo', 'kw', 'lb', 'ly', 'ma', 'om', 'qa', 'sa', 'sy', 'tn', 'ye'),
	'be' : ('by',),
	'bg' : ('bg',),
	'bn' : ('bd',),
	'cs' : ('cz',),
	'da' : ('dk',),
	'de' : ('at', 'ch', 'de', 'li', 'lu'),
	'dv' : ('mv',),
	'el' : ('gr',),
	'en' : ('au', 'bz', 'ca', 'gb', 'ie', 'jm', 'my', 'nz', 'sg', 'tt', 'us', 'za', 'zw'),
	'es' : ('ar', 'bo', 'cl', 'co', 'cr', 'do', 'ec', 'es', 'gt', 'hn', 'mx', 'ni', 'pa', 'pe', 'pr', 'py', 'sv', 'us', 'uy', 've'),
	'et' : ('ee',),
	'fa' : ('ir',),
	'fi' : ('fi',),
	'fil' : ('ph',),
	'fo' : ('fo',),
	'fr' : ('be', 'ca', 'ch', 'fr', 'lu', 'mc'),
	'he' : ('il',),
	'hi' : ('in',),
	'hr' : ('ba', 'hr'),
	'hu' : ('hu',),
	'hy' : ('am',),
	'id' : ('id',),
	'ig' : ('ng',),
	'is' : ('is',),
	'it' : ('ch', 'it'),
	'ja' : ('jp',),
	'ka' : ('ge',),
	'kk' : ('kz',),
	'kl' : ('gl',),
	'km' : ('kh',),
	'ko' : ('kr',),
	'ky' : ('kg',),
	'lb' : ('lu',),
	'lo' : ('la',),
	'lt' : ('lt',),
	'lv' : ('lv',),
	'mi' : ('nz',),
	'mk' : ('mk',),
	'mn' : ('mn',),
	'ms' : ('bn', 'my'),
	'mt' : ('mt',),
	'nb' : ('no',),
	'ne' : ('np',),
	'nl' : ('be', 'nl'),
	'pl' : ('pl',),
	'prs' : ('af',),
	'ps' : ('af',),
	'pt' : ('br', 'pt'),
	'ro' : ('ro',),
	'ru' : ('ru',),
	'rw' : ('rw',),
	'si' : ('lk',),
	'sk' : ('sk',),
	'sl' : ('si',),
	'sq' : ('al',),
	'sv' : ('se',),
	'sw' : ('ke',),
	'th' : ('th',),
	'tk' : ('tm',),
	'tr' : ('tr',),
	'uk' : ('ua',),
	'ur' : ('pk',),
	'vi' : ('vn',),
	'wo' : ('sn',),
	'yo' : ('ng',),
	'zh' : ('cn', 'hk', 'mo', 'sg', 'tw'),
}

 

  • Add at end of line:

import requests
def GetLanguage(locale_region_default = 'en'):
	language = locale_region_default
	try:
		request = requests.get('http://ip-api.com/json/', params=None)
		if request.status_code is 200:
			for locale_region, country_code_tuple in LOCALE_REGION_DICT.iteritems():
				if request.json().get('countryCode').lower() in country_code_tuple:
					language = locale_region

		return language
	except requests.exceptions.ConnectionError:
		return language

# Initialized just one time
LANGUAGE = GetLanguage()

How to use it:


import localeInfo
print localeInfo.LANGUAGE

 

good job my friend,  and thanks for complete the script for clients.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.