GithubHelp home page GithubHelp logo

isabella232 / friendly_id-globalize Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ouvrages/friendly_id-globalize

0.0 0.0 0.0 22 KB

Globalize support for FriendlyId

License: MIT License

Ruby 100.00%

friendly_id-globalize's Introduction

FriendlyId Globalize

Globalize support for FriendlyId.

Installation

gem 'friendly_id-globalize'
rails generate friendly_id_globalize

Translating Slugs Using Globalize

The FriendlyId::Globalize Globalize module lets you use Globalize to translate slugs. This module is most suitable for applications that need to be localized to many languages. If your application only needs to be localized to one or two languages, you may wish to consider the FriendlyId::SimpleI18n SimpleI18n module.

In order to use this module, your model's table and translation table must both have a slug column, and your model must set the slug field as translatable with Globalize:

class Post < ActiveRecord::Base
  translates :title, :slug
  extend FriendlyId
  friendly_id :title, :use => :globalize
end

Note that call to translates must be made before calling friendly_id.

Finds

Finds will take the current locale into consideration:

I18n.locale = :it
Post.find("guerre-stellari")
I18n.locale = :en
Post.find("star-wars")

Additionally, finds will fall back to the default locale:

I18n.locale = :it
Post.find("star-wars")

To find a slug by an explicit locale, perform the find inside a block passed to I18n's with_locale method:

I18n.with_locale(:it) { Post.find("guerre-stellari") }

Creating Records

When new records are created, the slug is generated for the current locale only.

Translating Slugs

To translate an existing record's friendly_id, use FriendlyId::Globalize::Model#set_friendly_id. This will ensure that the slug you add is properly escaped, transliterated and sequenced:

post = Post.create :name => "Star Wars"
post.set_friendly_id("Guerre stellari", :it)

If you don't pass in a locale argument, FriendlyId::Globalize will just use the current locale:

I18n.with_locale(:it) { post.set_friendly_id("Guerre stellari") }

friendly_id-globalize's People

Contributors

alepore avatar kevin-jj avatar laurynas avatar lenart avatar norman avatar parndt avatar sasselin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.