Jump to content

PY Get Initial Lang


Recommended Posts

M2 Download Center

This is the hidden content, please
( Internal )

 

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/

  • Metin2 Dev 1
  • Confused 1
  • Love 2
Link to comment
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("")

 

Link to comment
Share on other sites

  • Forum Moderator
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

 

  • Love 2
Link to comment
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.

Link to comment
Share on other sites

Announcements



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.