locale_gen: works with C.UTF-8 (#6774)

* locale_gen: fix

* test working with C.UTF-8

* working with locale eo

* handle C.UTF-8 edge cases

* grammatic pedantism

* add changelog frag

* add doc about specific OS support

* update changelog frag
This commit is contained in:
Alexei Znamensky 2023-06-30 16:39:11 +12:00 committed by GitHub
parent 89ad18d1a7
commit 3fd4cdb119
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 137 additions and 119 deletions

View file

@ -35,6 +35,8 @@ options:
- Whether the locale shall be present.
choices: [ absent, present ]
default: present
notes:
- This module does not support RHEL-based systems.
'''
EXAMPLES = '''
@ -74,11 +76,10 @@ def is_available(name, ubuntuMode):
checking either :
* if the locale is present in /etc/locales.gen
* or if the locale is present in /usr/share/i18n/SUPPORTED"""
__regexp = r'^#?\s*(?P<locale>\S+[\._\S]*) (?P<charset>\S+)\s*$'
if ubuntuMode:
__regexp = r'^(?P<locale>\S+_\S+) (?P<charset>\S+)\s*$'
__locales_available = '/usr/share/i18n/SUPPORTED'
else:
__regexp = r'^#{0,1}\s*(?P<locale>\S+_\S+) (?P<charset>\S+)\s*$'
__locales_available = '/etc/locale.gen'
re_compiled = re.compile(__regexp)
@ -88,7 +89,8 @@ def is_available(name, ubuntuMode):
if result and result.group('locale') == name:
return True
fd.close()
return False
# locale may be installed but not listed in the file, for example C.UTF-8 in some systems
return is_present(name)
def is_present(name):
@ -106,20 +108,6 @@ def fix_case(name):
return name
def replace_line(existing_line, new_line):
"""Replaces lines in /etc/locale.gen"""
try:
f = open("/etc/locale.gen", "r")
lines = [line.replace(existing_line, new_line) for line in f]
finally:
f.close()
try:
f = open("/etc/locale.gen", "w")
f.write("".join(lines))
finally:
f.close()
def set_locale(name, enabled=True):
""" Sets the state of the locale. Defaults to enabled. """
search_string = r'#{0,1}\s*%s (?P<charset>.+)' % name
@ -209,7 +197,7 @@ def main():
# We found the common way to manage locales.
ubuntuMode = False
else:
module.fail_json(msg="/etc/locale.gen and /var/lib/locales/supported.d/local are missing. Is the package \"locales\" installed?")
module.fail_json(msg="/etc/locale.gen and /var/lib/locales/supported.d/local are missing. Is the package 'locales' installed?")
else:
# Ubuntu created its own system to manage locales.
ubuntuMode = True