region

package
v0.8.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package region resolves a free-text region hint into per-engine search targeting (Google UULE canonical names, Yandex lr IDs, ISO country codes).

It is deliberately dependency-free (standard library only) so lightweight consumers that only need geotargeting can import it without pulling in the headless-browser/scraping stack the rest of github.com/karust/openserp/core depends on. The parent core package re-exports these symbols for backward compatibility.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountryFromRegion

func CountryFromRegion(region string) string

CountryFromRegion extracts a two-letter country/market code from a region hint. It accepts "ru", "RU", "en-RU", and "en_RU"; engine-native numeric region IDs intentionally return empty here.

func EncodeGoogleUULE

func EncodeGoogleUULE(canonical string) string

EncodeGoogleUULE encodes a canonical location name into a UULE v1 value: a fixed prefix, a single length-tag character, then base64(name). Returns "" for an empty name or one too long for the length alphabet.

func GoogleUULE

func GoogleUULE(region string) string

GoogleUULE builds a Google UULE v1 value for a region hint, returning "" when no UULE applies.

UULE only takes effect when the encoded string is an exact Google geotargets canonical name; a bare free-text city is silently ignored by Google. We therefore resolve the region first and encode the resolved canonical name. Country-level hints (e.g. "DE") and Yandex numeric lr IDs intentionally produce no UULE — those are conveyed via gl= or are not applicable to Google.

As a best-effort fallback, a free-text region that resolves to neither a country, a numeric lr, nor a known canonical name is encoded verbatim; Google ignores it if it is not a real canonical name, matching prior behavior.

func YandexLR

func YandexLR(region string) string

YandexLR converts a public region hint to a Yandex lr ID, returning "" when none applies. Numeric input is passed through; a 2-letter country or locale is mapped via the country table.

Types

type Locale

type Locale struct {
	Language string
	Country  string
}

Locale is a parsed language/region pair derived from a BCP47-style code. Language is the lowercase 2-letter language subtag (e.g. "en", "de"). Country is the uppercase 2-letter region subtag (e.g. "US", "DE"); it may be empty when the input had no region and the caller did not request a default.

func ParseLocale

func ParseLocale(code string) Locale

ParseLocale parses a language code such as "en", "EN-us", or "de_AT" into a Locale. Returns the zero value when the input is empty or has no language subtag. Country is uppercased; Language is lowercased.

type RegionTarget

type RegionTarget struct {
	// Raw is the trimmed original input, preserved for engines that pass it
	// through (e.g. Yandex numeric lr IDs).
	Raw string
	// Country is the ISO 3166-1 alpha-2 code (uppercase) when derivable, else "".
	Country string
	// GoogleCanonical is the exact Google geotargets canonical location name
	// (e.g. "Berlin,Berlin,Germany") suitable for UULE v1 encoding, else "".
	GoogleCanonical string
	// YandexLR is the Yandex lr region ID (e.g. "213"), else "".
	YandexLR string
}

RegionTarget is the resolved, per-engine targeting for a user-supplied region hint. Engines read the field relevant to them: Google uses GoogleCanonical to build a UULE, Yandex uses YandexLR. Country is the ISO 3166-1 alpha-2 code when one could be derived, useful as a coarse market signal.

A field left empty means "no better signal than the raw input" — callers should fall back to their previous behavior (e.g. gl= from locale, or dropping the parameter entirely). Resolution never fails: an unrecognized region simply yields empty engine fields rather than an error.

func ResolveRegion

func ResolveRegion(region string) RegionTarget

ResolveRegion turns a free-text region hint into per-engine targeting. It never errors: unrecognized input yields a RegionTarget with empty engine fields, leaving callers to fall back to their defaults.

Accepted inputs, in priority order:

  • Numeric (e.g. "213"): a Yandex-native lr ID. Passed through as YandexLR.
  • A 2-letter country or BCP47-style locale (e.g. "DE", "en-GB"): resolved to a country and its Yandex lr. No Google canonical — country targeting rides on gl=, not UULE.
  • A bare curated city name (e.g. "Berlin"): resolved to its canonical name.
  • A full "City,Region,Country" canonical name typed verbatim (>=2 commas): passed through to Google as-is.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL