{"id":16965035,"url":"https://github.com/propensive/cosmopolite","last_synced_at":"2025-10-18T01:06:32.114Z","repository":{"id":54682801,"uuid":"334423710","full_name":"propensive/cosmopolite","owner":"propensive","description":"Typesafe internationalization of strings in Scala","archived":false,"fork":false,"pushed_at":"2025-02-11T20:45:20.000Z","size":1107,"stargazers_count":18,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T01:43:04.875Z","etag":null,"topics":["i18n","internationalization","scala"],"latest_commit_sha":null,"homepage":"https://propensive.com/cosmopolite/","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/propensive.png","metadata":{"files":{"readme":".github/readme.md","changelog":null,"contributing":".github/contributing.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-30T13:50:27.000Z","updated_at":"2025-02-11T20:45:23.000Z","dependencies_parsed_at":"2023-10-11T11:43:26.695Z","dependency_job_id":"60494592-9777-48da-8c27-2bee77d792d4","html_url":"https://github.com/propensive/cosmopolite","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/propensive/cosmopolite","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fcosmopolite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fcosmopolite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fcosmopolite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fcosmopolite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/propensive","download_url":"https://codeload.github.com/propensive/cosmopolite/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/propensive%2Fcosmopolite/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279445921,"owners_count":26171619,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-17T02:00:07.504Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["i18n","internationalization","scala"],"created_at":"2024-10-13T23:44:52.723Z","updated_at":"2025-10-18T01:06:32.087Z","avatar_url":"https://github.com/propensive.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"[\u003cimg alt=\"GitHub Workflow\" src=\"https://img.shields.io/github/actions/workflow/status/propensive/cosmopolite/main.yml?style=for-the-badge\" height=\"24\"\u003e](https://github.com/propensive/cosmopolite/actions)\n[\u003cimg src=\"https://img.shields.io/discord/633198088311537684?color=8899f7\u0026label=DISCORD\u0026style=for-the-badge\" height=\"24\"\u003e](https://discord.com/invite/MBUrkTgMnA)\n\u003cimg src=\"/doc/images/github.png\" valign=\"middle\"\u003e\n\n# Cosmopolite\n\n__Typesafe internationalization for Scala__\n\n_Cosmopolite_ provides a typesafe representation for working with multilingual strings, `Messages`, with\nconvenient constructors which guarantee through their types that translations for a specific set of languages\nexist in each instance. Furthermore, a `Language` type provides a coproduct representation of a language from\nthe same set which can safely select one string from the set.\n\n## Features\n\n- provides a representation for multilingual strings\n- provides a representation of a language\n- interpolated string constructors for common languages\n- support for all [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) languages\n- users choose their exact set of languages with a union type\n- support for all language guaranteed by type for all static strings\n- additions to the language set produce compile errors for every incomplete multilingual string\n- checks for duplicate languages in construction of multilingual strings\n\n## Availability\n\n\n\n\n\n\n\n## Getting Started\n\nWhen working with front-end applications that must be presented to different users in different languages, it's\ncommon that the vast majority of the code which provides the user interface will be identical for every\nlanguage, with the exception of the strings which provide the text to be used in that user interface.\n\nIt is therefore useful to abstract over just the parts which differ, and to minimise repetition of the parts\nwhich are the same.\n\n### Language types\n\nLanguages are represented by types named after their ISO 639-1 code, for example `En` (English), `La` (Latin) or\n`De` (German). These are found in the `cosmopolite.languages` object, and each of these would typically be\nimported for use in a particular project, for example,\n```scala\nimport language.{En, La, De}\n```\n\nAdditionally, ten \"common\" languages are also made available in the `cosmopolite.languages.common` object,\nwhich can be conveniently imported with a wildcard import. These languages are:\n- German (`De`)\n- English (`En`)\n- Spanish (`Es`)\n- French (`Fr`)\n- Italian (`It`)\n- Japanese (`Ja`)\n- Polish (`Pl`)\n- Portuguese (`Pt`)\n- Russian (`Ru`)\n- Chinese (`Zh`)\n\nEach of these is an alias for the language type defined in the `cosmopolite.languages` object.\n\nWhere more than one language needs to be specified in Cosmopolite, this is done as a union of language types,\nfor example, the languages English, Polish and Russian would be represented as `En | Pl | Ru`.\n\nThe order of the language types in the union is not significant, as Scala will consider `Pl | En | Ru` an\nidentical type to `En | Pl | Ru`, or any other permutation.\n\n### Multilingual strings\n\nA multilingual string is represented by an instance of `Messages[L]` where `L` is the set of languages that\ninstance has, as a union type, and which is guaranteed by its construction.\n\nConstructors exist for single-language `Messages` instances of all common languages as prefixed strings, for\nexample, `en\"English text\"` or `fr\"Texte français\" will create instances of `Messages[En]` and `Messages[Fr]`\nrespectively.\n\nAny pair of messages may be combined with the `\u0026` operator to construct a `Messages` instance of all their\nlanguages combined. For example, `en\"English text\" \u0026 fr\"Texte français\"` will create an instance of\n`Messages[En | Fr]`.\n\nUsing these construction methods makes it possible to define instances of `Messages` whose type is\nguaranteed to reflect their content.\n\nThe `\u0026` combinator adds the further guarantee that every additional language combined with an existing\nmultilingual string must not already be defined, so it should be impossible to accidentally overwrite a string\nfor one language with another when constructing a `Messages` instance.\n\n#### Defining constructors for other languages\n\nConvenient constructors for languages that are not in the `common` object can be provided as extensions on\nScala's built-in `StringContext` type, and follow this pattern,\n```scala\nextension (ctx: StringContext)\n  def la(msgs: Messages[La]*): Messages[La] = Messages(ctx.parts, msgs)\n```\nwhich defines a constructor for the `La` language (Latin) on strings starting with the prefix `la\"\"`.\n\n### The `Language` type\n\nIn addition to representing product types for a collection of languages, we need a type that represents a single\nlanguage chosen from a collection of languages.\n\nThis `Language` instance will typically be constructed from a value that is only known at runtime, as it will be\nused to pick one particular language from a multilingual string. To construct a new `Language[L]` instance from\na set of languages, `L`, we can parse it from a `String` with the `Languages.parse` method.\n\nAs long as `L` is concretely known, Cosmopolite will build a parser that checks its input string against each of\nlanguages in the union, and returns an `Option[Language[L]]` instance.\n\nFor example,\n```scala\nLanguage.parse[En | Fr](\"fr\")\n```\nwould return `Some(Language[Fr])` while,\n```scala\nLanguage.parse[En | Fr](\"es\")\n```\nwould return `None`.\n\n### Type Aliases\n\nIt's likely that an application will use the same set of languages globally, so it's useful to define a global\ntype alias for these languages, for example,\n```scala\ntype Langs = En | Fr | De | Es\n```\nand to use the `Langs` type everywhere instead of any specific language types. The type will always dealias to\nthe union of language types, and the Scala compiler can continue to perform all necessary checks as before.\n\nConsequently, adding a language to such an alias can be a useful way to statically identify every multilingual\nstring that needs to be adapted to include the messages.\n\nFor example, changing the `Langs` definition above to,\n```scala\ntype Langs = En | Fr | De | Es | It\n```\nwould suddenly result in multiple compile errors: one for each usage of a multilingual string that fails to\nprovide an Italian string.\n\nThis offers an additional level of safety when developing for multilingual applications, as it becomes\nimpossible to compile code which does not provide language strings for every language required.\n\n### Abstract Languages\n\nIt is also largely possible to define methods and classes which are language-independent. It is possible to\ndefine a method which takes both `Messages[L]` instances and a `Language[L]` instance, and combines them without\never concretely knowing the language `L`.\n\nAs long as the type parameter of the `Messages` instance is a supertype of the type parameter of the `Language`\ninstance—and most likely the types would be equal—they may be combined to produce a string.\n\nAs one example use case, we could write a language-independent login form (for whatever framework we like) that\ntakes field and button labels as multilingual strings, and a corresponding `Language` instance. In combining\nthe `Language` coproduct with each of the `Messages` products, their type parameter would be eliminated.\n\nNote, however, that the `Language.parse` method, however, can only be invoked on a concrete type, as it needs\nto build a concrete parser for the languages in the union type.\n\n\n\n\n\n## Status\n\nCosmopolite is classified as __fledgling__. For reference, Soundness projects are\ncategorized into one of the following five stability levels:\n\n- _embryonic_: for experimental or demonstrative purposes only, without any guarantees of longevity\n- _fledgling_: of proven utility, seeking contributions, but liable to significant redesigns\n- _maturescent_: major design decisions broady settled, seeking probatory adoption and refinement\n- _dependable_: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version `1.0.0` or later\n- _adamantine_: proven, reliable and production-ready, with no further breaking changes ever anticipated\n\nProjects at any stability level, even _embryonic_ projects, can still be used,\nas long as caution is taken to avoid a mismatch between the project's stability\nlevel and the required stability and maintainability of your own project.\n\nCosmopolite is designed to be _small_. Its entire source code currently consists\nof 64 lines of code.\n\n## Building\n\nCosmopolite will ultimately be built by Fury, when it is published. In the\nmeantime, two possibilities are offered, however they are acknowledged to be\nfragile, inadequately tested, and unsuitable for anything more than\nexperimentation. They are provided only for the necessity of providing _some_\nanswer to the question, \"how can I try Cosmopolite?\".\n\n1. *Copy the sources into your own project*\n   \n   Read the `fury` file in the repository root to understand Cosmopolite's build\n   structure, dependencies and source location; the file format should be short\n   and quite intuitive. Copy the sources into a source directory in your own\n   project, then repeat (recursively) for each of the dependencies.\n\n   The sources are compiled against the latest nightly release of Scala 3.\n   There should be no problem to compile the project together with all of its\n   dependencies in a single compilation.\n\n2. *Build with [Wrath](https://github.com/propensive/wrath/)*\n\n   Wrath is a bootstrapping script for building Cosmopolite and other projects in\n   the absence of a fully-featured build tool. It is designed to read the `fury`\n   file in the project directory, and produce a collection of JAR files which can\n   be added to a classpath, by compiling the project and all of its dependencies,\n   including the Scala compiler itself.\n   \n   Download the latest version of\n   [`wrath`](https://github.com/propensive/wrath/releases/latest), make it\n   executable, and add it to your path, for example by copying it to\n   `/usr/local/bin/`.\n\n   Clone this repository inside an empty directory, so that the build can\n   safely make clones of repositories it depends on as _peers_ of `cosmopolite`.\n   Run `wrath -F` in the repository root. This will download and compile the\n   latest version of Scala, as well as all of Cosmopolite's dependencies.\n\n   If the build was successful, the compiled JAR files can be found in the\n   `.wrath/dist` directory.\n\n## Contributing\n\nContributors to Cosmopolite are welcome and encouraged. New contributors may like\nto look for issues marked\n[beginner](https://github.com/propensive/cosmopolite/labels/beginner).\n\nWe suggest that all contributors read the [Contributing\nGuide](/contributing.md) to make the process of contributing to Cosmopolite\neasier.\n\nPlease __do not__ contact project maintainers privately with questions unless\nthere is a good reason to keep them private. While it can be tempting to\nrepsond to such questions, private answers cannot be shared with a wider\naudience, and it can result in duplication of effort.\n\n## Author\n\nCosmopolite was designed and developed by Jon Pretty, and commercial support and\ntraining on all aspects of Scala 3 is available from [Propensive\nO\u0026Uuml;](https://propensive.com/).\n\n\n\n## Name\n\nCosmopolite is so-named because it provides multilingual strings, which are free to travel, and are not associated with any particular nation.\n\nIn general, Soundness project names are always chosen with some rationale,\nhowever it is usually frivolous. Each name is chosen for more for its\n_uniqueness_ and _intrigue_ than its concision or catchiness, and there is no\nbias towards names with positive or \"nice\" meanings—since many of the libraries\nperform some quite unpleasant tasks.\n\nNames should be English words, though many are obscure or archaic, and it\nshould be noted how willingly English adopts foreign words. Names are generally\nof Greek or Latin origin, and have often arrived in English via a romance\nlanguage.\n\n## Logo\n\nThe logo shows two overlapping speech bubbles, highlighting Cosmopolite's focus on communication (in particular, between groups speaking different languages).\n\n## License\n\nCosmopolite is copyright \u0026copy; 2025 Jon Pretty \u0026 Propensive O\u0026Uuml;, and\nis made available under the [Apache 2.0 License](/license.md).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fcosmopolite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpropensive%2Fcosmopolite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpropensive%2Fcosmopolite/lists"}