# Item names in other languages

The [offline catalogue](catalogue.md) (`sona-catalogue.bin`) names its items as the Xbox 360 marketplace did in each of its
markets. All 52 locales the marketplace answers in were sampled; a table is kept for each one whose names were its
own:

| Tag | Market | Items named in it |
|---|---|---|
| `cs` | Czech (cs-CZ) | 1,170 |
| `da` | Danish (da-DK) | 1,359 |
| `de` | German (de-DE; de-AT and de-CH read it) | 7,432 |
| `el` | Greek (el-GR) | 872 |
| `en-GB` | the UK store (where it named an item differently) | 416 |
| `es` | Spain (es-ES) | 7,670 |
| `es-AR` | Argentina | 2,876 |
| `es-MX` | Mexico and Latin America (es-CL, es-CO read it) | 7,641 |
| `fi` | Finnish (fi-FI) | 1,349 |
| `fr` | French (fr-FR; fr-BE, fr-CA, fr-CH read it) | 7,655 |
| `hu` | Hungarian (hu-HU) | 946 |
| `it` | Italian (it-IT) | 7,265 |
| `ja` | Japanese (ja-JP) | 6,852 |
| `ko` | Korean (ko-KR) | 5,569 |
| `nb` | Norwegian (nb-NO) | 1,374 |
| `nl` | Dutch (nl-NL; nl-BE reads it) | 2,065 |
| `pl` | Polish (pl-PL) | 2,574 |
| `pt-BR` | Brazil | 6,903 |
| `pt-PT` | Portugal | 6,874 |
| `ru` | Russian (ru-RU) | 2,805 |
| `sk` | Slovak (sk-SK) | 859 |
| `sv` | Swedish (sv-SE) | 1,433 |
| `tr` | Turkish (tr-TR) | 539 |
| `zh-HK` | Hong Kong (Traditional Chinese) | 5,674 |
| `zh-TW` | Taiwan (Traditional Chinese) | 5,661 |

With English, 26 tags. The other locales matched one of these on a sample of 299 items: de-AT and de-CH had
de-DE's names, fr-BE, fr-CA and fr-CH fr-FR's, es-CL and es-CO es-MX's, nl-BE nl-NL's; the other English markets,
zh-CN, zh-SG, he-IL, ar-SA and ar-AE had the English names. A count is the items whose name there differs from the
item's own; `en-GB` counts the UK store's names that differ from the catalogue's English one. Most items kept their English name in most markets: a table
holds a name only where it differs from the item's own.

```c
uint32_t n = sona_catalogue_language_count(cat);          /* "en" among them */
const char *tag = sona_catalogue_language(cat, i);        /* "cs", "da", "de", "el", "en", "en-GB", "es", ... */
const char *name;
if (sona_catalogue_item_name(cat, index, "pt-PT", &name) == SONA_OK) show(name);
else show(info.name);                                     /* SONA_E_NOT_FOUND: that market used the English name */
```

```cpp
uint32_t n = cat.language_count();                     // "en" among them
const char *tag = cat.language(i);                     // "cs", "da", "de", "el", "en", "en-GB", "es", ...
const char *name;
if (cat.item_name(index, "pt-PT", name) == SONA_OK) show(name);
else show(info.name);                                  // SONA_E_NOT_FOUND: that market used the English name
```

```csharp
// the binding has no wrapper for the names in other languages: the calls of its raw Native layer
using var cat = SonaCatalogue.Load("data/sona-catalogue.bin", ctx);
uint n = Native.sona_catalogue_language_count(cat.Handle);          // "en" among them
string tag = Native.FromUtf8(Native.sona_catalogue_language(cat.Handle, 0));   // "cs", "da", "de", "el", "en", ...
IntPtr name;
fixed (byte* lang = System.Text.Encoding.UTF8.GetBytes("pt-PT\0"))
    if (Native.sona_catalogue_item_name(cat.Handle, (uint)index, lang, &name) == SonaResult.Ok) Show(Native.FromUtf8(name));
    // else SONA_E_NOT_FOUND: that market used the English name
```

```rust
let tags = cat.languages();                                  // "cs", "da", "de", "el", "en", "en-GB", "es", ...
let english = cat.get(index).map(|it| it.name).unwrap_or("");
show(cat.item_name(index, "pt-PT")?.unwrap_or(english));     // None: that market used the English name
```

```python
import ctypes
from sona import _lib   # the package's ctypes layer: it has no wrapper for the names in other languages

cat = sona.Catalogue("data/sona-catalogue.bin", ctx)
n = _lib.sona_catalogue_language_count(cat._c())            # "en" among them
name = ctypes.c_char_p()
if _lib.sona_catalogue_item_name(cat._c(), index, b"pt-PT", ctypes.byref(name)) == 0:
    print(name.value.decode())
# else SONA_E_NOT_FOUND: that market used the English name
```

## Which table a tag reads

`lang` is a BCP 47 tag, any case, `_` or `-`. The table of that exact tag answers first. A tag with no table of its
own reads the market the marketplace served it from, then its language alone:

- `es-ES` reads `es`; any other Spanish region (`es-CL`, `es-419`, `es-US`) reads `es-MX`, Latin America's.
- `pt` alone reads `pt-BR` (CLDR's likely region); any other Portuguese region (`pt-AO`, `pt-MZ`) reads `pt-PT`,
  never Brazil's names.
- `zh-HK` and `zh-MO` read `zh-HK`, then `zh-TW`; `zh`, `zh-Hant` and `zh-Hant-TW` read `zh-TW`; `zh-CN`, `zh-SG`
  and `zh-Hans` have no table (the marketplace showed them the English names) and are `SONA_E_NOT_FOUND`.
- `no`, `nn` and their regions read `nb`.
- Anything else with a region reads its language: `de-AT` reads `de`, `fr-CA` reads `fr`.
- `en` and every English region other than `en-GB` are the item's own name; `en-GB` is its own name too except where
  the UK store named it differently.

The first table found decides: an item it has no name for is `SONA_E_NOT_FOUND` (that market showed the English
name), even if a later table has one. A language the catalogue lacks is `SONA_E_NOT_FOUND`. The names live as long
as the catalogue.

## The format

Catalogue format 1.1 adds two tables after the strings, found from header bytes 48 to 63: the language tags (string
offsets, sorted) and the names (12-byte entries: item index, language index, two bytes of padding, string offset;
sorted by item, then language). Strings are shared, so a region's table costs a fixed 12 bytes a name. A 1.0 reader skips
them; this reader takes a 1.0 file as having English only.
