Skip to main content

Localizer

This library can be used to localize date-time-strings based on the given locales and corresponding localization-data stored next to this library's file. Files with all translations are in localizer/date_translation_data and may be modified / extended. To start localizing strings, intialize the Localizer-class with a list of desired locales and call the method get_localized_string with the string, that should be localized.

It is based on the python module: dateparser by scrapinghub. Since comet implements its own method for importing modules / scripts, this is a script, that contains the bare necessaties to support localizing strings.

info
Copyright (c) 2014, Scrapinghub

Copyright (c) 2014, Scrapinghub All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of DateParser nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Class: Localizer​

Localizer(...)​

(locales = None)​

This class can be used to localize date-time-strings based on the given locales and corresponding localization-data stored next to this library's file.

Parameters: locales (list)
Returns:Localizer instance
Return Type:Localizer

get_localized_string(...)​

(input_string, target_locales = None, string_variant = WordVariant.FULL, keep_capital = True, fallback_input = True)​

Returns a dictionary of localized versions of the provided input string for a given list of target locales. This method uses the internal _Localizer to transform the string according to language-specific rules and formatting.

Parameters: input_string (str)
The original string to be localized.
target_locales (list)
A list (or other iterable) of locale identifiers to which the string should be localized. If None, defaults to the instance's configured locales.
Raises a ValueError if neither this argument nor instance locales are provided.
string_variant (WordVariant)
Specifies the variant of the word to use in localization. Defaults to WordVariant.FULL.
keep_capital (bool)
If True, retains the title-casing of the input string in the localized result. Defaults to True.
fallback_input (bool)
If True, returns the original input string as a fallback if no localization is available for a locale. If False, returns an empty string for those cases. Defaults to True.
Returns:A dictionary mapping each applicable locale's short name to its corresponding localized string.
Return Type:dict

Class: LocaleDataLoader​

LocaleDataLoader()​

This class is responsible for loading and managing locale-specific language data. It provides an interface for retrieving Locale instances based on locale codes (e.g., "fr-PF", "qu-EC"), and caches loaded language and locale data to optimize performance across repeated calls.

get_locales(...)​

(locales=None)​

Yields an iterator of Locale instances corresponding to the given locale codes..

Parameters:locales (list)
A list of codes of locales which are to be loaded, e.g. ['fr-PF', 'qu-EC', 'af-NA']
Returns:locale instances
Return Type:Generator[Locale]

Class: Locale​

Locale(...)​

(shortname, language_info)​

This class represents a specific locale (e.g., fr-FR, es-ES) and encapsulates the translation data and logic associated with that locale. It combines general language data with locale-specific overrides and provides translation capabilities for date-related words. |Parameters: | shortname (str)
The locale's identifier (e.g., 'fr-FR'). | | | language_info (dict)
A dictionary containing general and optional locale-specific translation data. | |Returns:| locale instance | |Return Type:| Locale |

translate(...)​

(word)​

Translates a word using the locale's translation dictionary. The result is a dictionary mapping WordVariant values to localized strings.||| |---|---| |Parameters: | word (str)
A string that exists in the translation-data. | |Returns:| translated string. | |Return Type:| str |

Class: Dictionary​

Dictionary(...)​

(locale_info)​

Class that modifies and stores translations and handles splitting of date string.

Parameters:locale_info (dict)
Locale info (translation data) of the locale.
Returns:A Dictionary instance
Return Type:dict