Repository: ViewComponent/view_component (Ruby)

Issue:
Relative translations don't work in initializer
This is similar to #74

If I have an initializer and I use the translation helper, then I get the error:
```
Cannot use t(".link_text") shortcut because path is not available
```
This happens even after calling `super`

If I move the translation into an instance method, it works as expected.

Using view_component-2.75.0, rails-7.0.4, ruby 3.1.0


Discussion: @jcoyne thanks for filing this issue. Might you be up for writing a PR with a failing test? Perhaps @elia could have a look, then.
This is expected, although I agree would be great to have them working in the initializer as well.

The reason it works as an instance method is that the method is most likely called after initialization and compilation of the component. Happy to review any proposal to make it work from the get go tho 🙌 
@jcoyne given @elia's response, are there any documentation changes you think we should make?
I suppose some warning in the documentation would be nice. I'm not at all clear why this timing concern exists. Maybe an explanation about that?
@jcoyne perhaps we could just add a warning for now? Like this test case: https://github.com/ViewComponent/view_component/blob/c9b7d36e5dd2a043b20756cf5a6bdef3d3463902/test/sandbox/test/base_test.rb#L37. We could also add something to the docs.
@joelhawksley that would be really helpful
@jcoyne cool. Would you be up for writing a PR? I can help clean up language if you get something started ❤️ 
No, because I don't understand the order problem were seeing. I don't understand what the compile step is or what that does.



Fix (patch):
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index bc003451b..2b18249cc 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -10,6 +10,10 @@ nav_order: 5
 
 ## main
 
+* Raise error if translations are used in initializer.
+
+    *Joel Hawksley*
+
 ## v3.0.0.rc5
 
 * Fix bug where `mkdir_p` failed due to incorrect permissions.
diff --git a/lib/view_component/base.rb b/lib/view_component/base.rb
index d2bf04b42..327764384 100644
--- a/lib/view_component/base.rb
+++ b/lib/view_component/base.rb
@@ -28,12 +28,12 @@ def config
       attr_writer :config
     end
 
+    class ViewContextCalledBeforeRenderError < StandardError; end
+
     include ViewComponent::Slotable
     include ViewComponent::Translatable
     include ViewComponent::WithContentHelper
 
-    ViewContextCalledBeforeRenderError = Class.new(StandardError)
-
     RESERVED_PARAMETER = :content
 
     # For CSRF authenticity tokens in forms
@@ -197,7 +197,7 @@ def helpers
       if view_context.nil?
         raise(
           ViewContextCalledBeforeRenderError,
-          "`#helpers` can't be used during initialization, as it depends " \
+          "`#helpers` can't be used during initialization as it depends " \
           "on the view context that only exists once a ViewComponent is passed to " \
           "the Rails render pipeline.\n\n" \
           "It's sometimes possible to fix this issue by moving code dependent on " \
diff --git a/lib/view_component/translatable.rb b/lib/view_component/translatable.rb
index 32c5a1579..72cbbeadf 100644
--- a/lib/view_component/translatable.rb
+++ b/lib/view_component/translatable.rb
@@ -81,6 +81,17 @@ def store_translations(locale, data, options = EMPTY_HASH)
     end
 
     def translate(key = nil, **options)
+      if view_context.nil?
+        raise(
+          ViewComponent::Base::ViewContextCalledBeforeRenderError,
+          "`#translate` can't be used during initialization as it depends " \
+          "on the view context that only exists once a ViewComponent is passed to " \
+          "the Rails render pipeline.\n\n" \
+          "It's sometimes possible to fix this issue by moving code dependent on " \
+          "`#translate` to a `#before_render` method: https://viewcomponent.org/api.html#before_render--void."
+        )
+      end
+
       return super unless i18n_backend
       return key.map { |k| translate(k, **options) } if key.is_a?(Array)
 
diff --git a/performance/components/translatable_component.rb b/performance/components/translatable_component.rb
index f6635411f..1e541e01f 100644
--- a/performance/components/translatable_component.rb
+++ b/performance/components/translatable_component.rb
@@ -1,8 +1,6 @@
 # frozen_string_literal: true
 
 class Performance::TranslatableComponent < ViewComponent::Base
-  include ViewComponent::Translatable
-
   def initialize(key)
     @key = key
   end