docs » cp.strings
Provides strings from (potentially) multiple sources, with support for loading from multiple languages.
local strs = require("cp.strings").new():fromPlist("/Path/To/Resources/${language}.lproj/MYLocalization.strings")
local value = strs:find("en", "AKey")
This will load the file for the specified language (replacing ${language}
with "en"
in the path) and return the value.
Note: This will load the file on each request. To have values cached, use the cp.strings
module and specify a plist
as a source.
- Constructors - API calls which return an object, typically one that offers API methods
- new
- Methods - API calls which can only be made on an object returned by a constructor
- context
- find
- findInSources
- findKeys
- findKeysInSources
- from
- fromPlist
Signature | cp.strings.new(context) -> cp.strings |
---|---|
Type | Constructor |
Description | Creates a new strings instance. You should add sources with the from or fromPlist methods. |
Parameters |
|
Returns |
|
| Signature | cp.strings:context([context]) -> table | self
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Gets or sets a context to be set for the strings. This typically includes a language
, which |
| Parameters |
- context - A table with values which may be used by the source.
- If a new context is provided, the
cp.string.source
is returned, otherwise the current context table is returned.
| Signature | cp.strings:find(key[, context[, quiet]) -> string | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Searches for the specified key, caching the result when found. |
| Parameters |
key
- The key to retrieve from the file.context
- Optional table with additional/alternate context.quiet
- Optional boolean, defaults tofalse
. Iftrue
, no warnings are logged for missing keys.
- The value of the key, or
nil
if not found.
| Signature | cp.strings:findInSources(key[, context[, quiet]]) -> string | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Searches directly in the sources for the specified key. |
| Parameters |
key
- The key to retrieve from the file.context
- Optional table with additional/alternate context.quiet
- Optional boolean, defaults tofalse
. Iftrue
, no warnings are logged for missing keys.
- The value of the key, or
nil
if not found.
| Signature | cp.strings:findKeys(value[, context]) -> string | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Searches for the list of keys with a matching value, in the specified language. |
| Parameters |
value
- The value to search for.context
- The language code to look for (e.g."en"
, or"fr"
).
- The array of keys, or
{}
if not found.
| Signature | cp.strings:findKeysInSources(value[, context]) -> string | nil
|
| -----------------------------------------------------|---------------------------------------------------------------------------------------------------------|
| Type | Method |
| Description | Searches directly in the sources for the specified key value pattern. |
| Parameters |
value
- The value to search for.context
- Optional additional context for the request.
- The array of keys, or
{}
if not found.
Signature | cp.strings:from(source) -> cp.strings |
---|---|
Type | Method |
Description | Adds the source to the strings sources. |
Parameters |
|
Returns |
|
Signature | cp.strings:fromPlist(pathPattern) -> cp.strings |
---|---|
Type | Method |
Description | Convenience method for adding a plist source to the strings instance. |
Parameters |
|
Returns |
|