Parsing a Language Key into its Components

Problem

You want to see how a language key expands into its components.

Solution

Use the Lang::parseKey() method.

This method will return an array with three elements:

  1. The first element is the key's namespace
  2. The second is the key's group (aka file)
  3. The third element is the actual item in the group

Consider the following PHP code.

print_r(Lang::parseKey('message.welcome'));
print_r(Lang::parseKey('message.two.part'));
print_r(Lang::parseKey('mypackage::message.two.part'));

The above code would output the following.

Array
(
    [0] => *
    [1] => message
    [2] => welcome
)
Array
(
    [0] => *
    [1] => message
    [2] => two.part
)
Array
(
    [0] => mypackage
    [1] => message
    [2] => two.part
)

Discussion

This is a low-level method.

It's used internally by the Lang facade. But it is handy to know when debugging and trying to determine why a translation string isn't finding the message. Knowing how the key is broken apart helps track down where Laravel is looking for the translation.

comments powered by Disqus