All Projects → vapor-community → Lingo-Vapor

vapor-community / Lingo-Vapor

Licence: MIT license
Vapor provider for Lingo - the Swift localization library

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Lingo-Vapor

wkhtmltopdf
Generate and return PDFs from Vapor views
Stars: ✭ 53 (+17.78%)
Mutual labels:  vapor, vapor-provider
leaf-markdown
Markdown renderer for Vapor
Stars: ✭ 51 (+13.33%)
Mutual labels:  vapor, vapor-provider
mysql-provider
MySQL provider for the Vapor web framework.
Stars: ✭ 31 (-31.11%)
Mutual labels:  vapor, vapor-provider
Lingo
Powerful Swift string localization library with support for pluralization and string interpolation.
Stars: ✭ 55 (+22.22%)
Mutual labels:  localization, vapor
fluent-provider
A provider for including Fluent in Vapor applications
Stars: ✭ 13 (-71.11%)
Mutual labels:  vapor, vapor-provider
VaporGCM
A simple Android GCM/FCM library for Swift/Vapor
Stars: ✭ 25 (-44.44%)
Mutual labels:  vapor, vapor-provider
sendgrid
SendGrid-powered mail backend for Vapor
Stars: ✭ 66 (+46.67%)
Mutual labels:  vapor, vapor-provider
react-i18next-phraseapp
Library support to use react-i18next with the Phrase In-Context Editor - DEPRECATED
Stars: ✭ 14 (-68.89%)
Mutual labels:  localization
ros-vrep-slam
ROS and V-REP for Robot Mapping and Localization
Stars: ✭ 39 (-13.33%)
Mutual labels:  localization
LocalizationService
Tools for localization Unity's UIText components
Stars: ✭ 22 (-51.11%)
Mutual labels:  localization
i18n-command
Provides internationalization tools for WordPress projects.
Stars: ✭ 76 (+68.89%)
Mutual labels:  localization
android-studio-plugin
Integrate your Android project with Crowdin
Stars: ✭ 52 (+15.56%)
Mutual labels:  localization
go-localize
i18n (Internationalization and localization) engine written in Go, used for translating locale strings.
Stars: ✭ 45 (+0%)
Mutual labels:  localization
HomeKitty
A Vapor 3 website to easily browse HomeKit accessories.
Stars: ✭ 75 (+66.67%)
Mutual labels:  vapor
GA SLAM
🚀 SLAM for autonomous planetary rovers with global localization
Stars: ✭ 40 (-11.11%)
Mutual labels:  localization
My.Extensions.Localization.Json
JSON Localization Resources
Stars: ✭ 50 (+11.11%)
Mutual labels:  localization
dart.cn
Dart docs localization, get started from the wiki page here: https://github.com/cfug/dart.cn/wiki
Stars: ✭ 64 (+42.22%)
Mutual labels:  localization
lingua
A PHP-7 language codes converter, from and to the most common formats (ISO or not)
Stars: ✭ 35 (-22.22%)
Mutual labels:  localization
ferno
Vapor Firebase Realtime database provider
Stars: ✭ 52 (+15.56%)
Mutual labels:  vapor
contiki-uwb
Contiki OS, Glossy and Crystal port for the DecaWave EVB1000 and DWM1001 platforms featuring the DW1000 UWB transceiver
Stars: ✭ 22 (-51.11%)
Mutual labels:  localization

Lingo Provider

Language GitHub license

A Vapor provider for Lingo - a pure Swift localization library ready to be used in Server Side Swift projects.

Setup

Add a dependancy

Add LingoProvider as a dependancy in your Package.swift file:

dependencies: [
	...,
	.package(name: "LingoVapor", url: "https://github.com/vapor-community/lingo-vapor.git", from: "4.2.0")]
],
targets: [
    .target(name: "App", dependencies: [
        .product(name: "LingoVapor", package: "LingoVapor")

Upgrading from version 4.1.0 to version 4.2.0

The version 4.1.0 uses the new version of Lingo where the format of locale identifiers was changed to match RFC 5646. Prior to 4.2.0 _ was used to separate language code and country code in the locale identifier, and now the library uses - as per RFC.

If you were using any locales which include a country code, you would need to rename related translation files to match the new format.

Add the Provider

In the configure.swift simply initialize the LingoVapor with a default locale:

import LingoVapor
...
public func configure(_ app: Application) throws {
	...
	app.lingoVapor.configuration = .init(defaultLocale: "en", localizationsDir: "Localizations")
}

The localizationsDir can be omitted, as the Localizations is also the default path. Note that this folder should exist under the workDir.

Use

After you have configured the provider, you can use lingoVapor service to create Lingo:

let lingo = try app.lingoVapor.lingo()
...
let localizedTitle = lingo.localize("welcome.title", locale: "en")

To get the locale of a user out of the request, you can use request.locale. This uses a language, which is in the HTTP header and which is in your available locales, if that exists. Otherwise it falls back to the default locale. Now you can use different locales dynamically:

let localizedTitle = lingo.localize("welcome.title", locale: request.locale)

When overwriting the requested locale, just write the new locale into the session, e.g. like that:

session.data["locale"] = locale

Use the following syntax for defining localizations in a JSON file:

{
	"title": "Hello Swift!",
	"greeting.message": "Hi %{full-name}!",
	"unread.messages": {
		"one": "You have one unread message.",
		"other": "You have %{count} unread messages."
	}
}

Locale redirection middleware

In case you want to serv different locales on different subfolders, you can use the LocaleRedirectMiddleware.

Add in configure.swift:

import LingoVapor

// Inside `configure(_ app: Application)`:
app.middleware.use(LocaleRedirectMiddleware())

Add in routes.swift:

import LingoVapor

// Inside `routes(_ app: Application)`:
app.get("home") { /* ... */ }
app.get(":locale", "home") { /* ... */ } // For each route, add the one prefixed by the `locale` parameter

That way, going to /home/ will redirect you to /<locale>/home/ (with <locale> corresponding to your browser locale), and going to /fr/home/ will display homepage in french whatever the browser locale is.

Inside Leaf templates

When using Leaf as templating engine, you can use LocalizeTag, LocaleTag and LocaleLinksTag from LingoVaporLeaf for localization inside the templates.

Add in configure.swift:

import LingoVaporLeaf

// Inside `configure(_ app: Application)`:
app.leaf.tags["localize"] = LocalizeTag()
app.leaf.tags["locale"] = LocaleTag()
app.leaf.tags["localeLinks"] = LocaleLinksTag()

Afterwards you can call them inside the Leaf templates:

<!-- String localization -->
#localize("thisisthelingokey")
#localize("lingokeywithvariable", "{\"foo\":\"bar\"}")

<!-- Get current locale -->
<html lang="#locale()">

<!-- Generate link canonical and alternate tags -->
#localeLinks("http://example.com/", "/canonical/path/")

Learn more

  • Lingo - learn more about the localization file format, pluralization support, and see how you can get the most out of the Lingo.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].