Giter VIP home page Giter VIP logo

Comments (6)

ysakasin avatar ysakasin commented on June 11, 2024

https://github.com/ruby-i18n/i18n

from bcdice.

ysakasin avatar ysakasin commented on June 11, 2024

ruby-i18nは全てがモジュールで管理されており、ロケールをモジュールの特異メソッドで指定することでロケールを変更する。

一つのアプリケーションで複数のロケールを同時に扱いたいことを加味すると、BCDiceで利用するのは適切でないように思える。

from bcdice.

ochaochaocha3 avatar ochaochaocha3 commented on June 11, 2024

ruby-i18nは全てがモジュールで管理されており、ロケールをモジュールの特異メソッドで指定することでロケールを変更する。

それでは対応できない場合もあることから、I18n.locale では、キーワード引数 locale: でロケールを明示できます。

https://railsguides.jp/i18n.html#%E3%83%AD%E3%82%B1%E3%83%BC%E3%83%AB%E3%81%AE%E8%A8%AD%E5%AE%9A%E3%81%A8%E5%8F%97%E3%81%91%E6%B8%A1%E3%81%97

from bcdice.

ysakasin avatar ysakasin commented on June 11, 2024

いちいちロケールを指定するのは正直面倒なので、wapperメソッドを作ればいけそうですかね

from bcdice.

ochaochaocha3 avatar ochaochaocha3 commented on June 11, 2024

いちいちロケールを指定するのは正直面倒なので、wapperメソッドを作ればいけそうですかね

ですね。Railsのコントローラ/ビュー風だと、例えばこんな雰囲気になりそうです。

class DiceBot
  # @return [Symbol]
  attr_accessor :lang

  private

  # t('karidice.nanika') のように使える
  def t(key, **options)
    options_with_locale = { locale: @lang }.merge(options)
    I18n.translate(key, **options_with_locale)
  end
end

from bcdice.

ysakasin avatar ysakasin commented on June 11, 2024

メモ

以下のようにしてYAMLの内部構造を配列にしても、配列のまま取り出せる。
途中のkeyを取り出すと、Hashが帰ってくる

jp_ja:
  ほげ表:
    name: 'ホゲ今日'
    rows: [
      'hououin',
      'shiina',
      'hashida',
      'makise',
    ]
require 'i18n'
  
I18n.load_path << Dir["./*.yaml"]
I18n.default_locale = :jp_ja

p I18n.t('ほげ表.rows') #=> ["hououin", "shiina", "hashida", "makise"]
p I18n.t('ほげ表') #=> {:name=>"ホゲ今日", :rows=>["hououin", "shiina", "hashida", "makise"]}

from bcdice.

Related Issues (20)

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.